View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message;
21  
22  
23  /**
24   * ExtendedRequest implementation.
25   * 
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public abstract class AbstractExtendedRequest extends AbstractRequest implements ExtendedRequest
29  {
30      static final long serialVersionUID = 7916990159044177480L;
31  
32      /** Extended request's Object Identifier or <b>requestName</b> */
33      private String oid;
34  
35      /** The associated response */
36      private ExtendedResponse response;
37  
38  
39      /**
40       * Creates an ExtendedRequest implementing object used to perform
41       * extended protocol operation on the server.
42       */
43      public AbstractExtendedRequest()
44      {
45          super( -1, MessageTypeEnum.EXTENDED_REQUEST, true );
46      }
47  
48  
49      /**
50       * Creates an ExtendedRequest implementing object used to perform
51       * extended protocol operation on the server.
52       * 
53       * @param id the sequential message identifier
54       */
55      public AbstractExtendedRequest( final int id )
56      {
57          super( id, MessageTypeEnum.EXTENDED_REQUEST, true );
58      }
59  
60  
61      // -----------------------------------------------------------------------
62      // ExtendedRequest Interface Method Implementations
63      // -----------------------------------------------------------------------
64  
65      /**
66       * Gets the Object Identifier corresponding to the extended request type.
67       * This is the <b>requestName</b> portion of the ext. req. PDU.
68       * 
69       * @return the dotted-decimal representation as a String of the OID
70       */
71      @Override
72      public String getRequestName()
73      {
74          return oid;
75      }
76  
77  
78      /**
79       * Sets the Object Identifier corresponding to the extended request type.
80       * 
81       * @param newOid the dotted-decimal representation as a String of the OID
82       */
83      @Override
84      public ExtendedRequest setRequestName( String newOid )
85      {
86          this.oid = newOid;
87  
88          return this;
89      }
90  
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public ExtendedRequest setMessageId( int messageId )
97      {
98          super.setMessageId( messageId );
99  
100         return this;
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public ExtendedRequest addControl( Control control )
109     {
110         return ( ExtendedRequest ) super.addControl( control );
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public ExtendedRequest addAllControls( Control[] controls )
119     {
120         return ( ExtendedRequest ) super.addAllControls( controls );
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public ExtendedRequest removeControl( Control control )
129     {
130         return ( ExtendedRequest ) super.removeControl( control );
131     }
132 
133 
134     // ------------------------------------------------------------------------
135     // SingleReplyRequest Interface Method Implementations
136     // ------------------------------------------------------------------------
137 
138     /**
139      * Gets the protocol response message type for this request which produces
140      * at least one response.
141      * 
142      * @return the message type of the response.
143      */
144     @Override
145     public MessageTypeEnum getResponseType()
146     {
147         return MessageTypeEnum.EXTENDED_RESPONSE;
148     }
149 
150 
151     /**
152      * The result containing response for this request.
153      * 
154      * @return the result containing response for this request
155      */
156     @Override
157     public abstract ExtendedResponse getResultResponse();
158 
159 
160     /**
161      * @return the response
162      */
163     public ExtendedResponse getResponse()
164     {
165         return response;
166     }
167 
168 
169     /**
170      * @param response the response to set
171      */
172     public void setResponse( ExtendedResponse response )
173     {
174         this.response = response;
175     }
176 
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public int hashCode()
183     {
184         int hash = 37;
185         if ( oid != null )
186         {
187             hash = hash * 17 + oid.hashCode();
188         }
189         hash = hash * 17 + super.hashCode();
190 
191         return hash;
192     }
193 
194 
195     /**
196      * Checks to see if an object equals this ExtendedRequest.
197      * 
198      * @param obj the object to be checked for equality
199      * @return true if the obj equals this ExtendedRequest, false otherwise
200      */
201     @Override
202     public boolean equals( Object obj )
203     {
204         if ( obj == this )
205         {
206             return true;
207         }
208 
209         if ( !super.equals( obj ) )
210         {
211             return false;
212         }
213 
214         if ( !( obj instanceof ExtendedRequest ) )
215         {
216             return false;
217         }
218 
219         ExtendedRequest req = ( ExtendedRequest ) obj;
220 
221         if ( ( oid != null ) && ( req.getRequestName() == null ) )
222         {
223             return false;
224         }
225 
226         if ( ( oid == null ) && ( req.getRequestName() != null ) )
227         {
228             return false;
229         }
230 
231         if ( ( oid != null ) && ( req.getRequestName() != null ) && !oid.equals( req.getRequestName() ) )
232         {
233             return false;
234         }
235 
236         return true;
237     }
238 
239 
240     /**
241      * Get a String representation of an Extended Request
242      * 
243      * @return an Extended Request String
244      */
245     @Override
246     public String toString()
247     {
248         StringBuilder sb = new StringBuilder();
249 
250         sb.append( "    Extended request\n" );
251         sb.append( "        Request name : '" ).append( oid ).append( "'\n" );
252 
253         // The controls
254         sb.append( super.toString() );
255 
256         return super.toString( sb.toString() );
257     }
258 }