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   *    https://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      protected 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( 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         
186         if ( oid != null )
187         {
188             hash = hash * 17 + oid.hashCode();
189         }
190         
191         hash = hash * 17 + super.hashCode();
192 
193         return hash;
194     }
195 
196 
197     /**
198      * Checks to see if an object equals this ExtendedRequest.
199      * 
200      * @param obj the object to be checked for equality
201      * @return true if the obj equals this ExtendedRequest, false otherwise
202      */
203     @Override
204     public boolean equals( Object obj )
205     {
206         if ( obj == this )
207         {
208             return true;
209         }
210 
211         if ( !super.equals( obj ) )
212         {
213             return false;
214         }
215 
216         if ( !( obj instanceof ExtendedRequest ) )
217         {
218             return false;
219         }
220 
221         ExtendedRequest req = ( ExtendedRequest ) obj;
222         
223         String requestName = req.getRequestName();
224 
225         if ( oid == null )
226         {
227             if ( requestName != null )
228             {
229                 return false;
230             }
231         }
232         else
233         {
234             if ( requestName == null )
235             {
236                 return false;
237             }
238             else if ( !oid.equals( requestName ) )
239             {
240                 return false;
241             }
242         }
243 
244         return true;
245     }
246 
247 
248     /**
249      * Get a String representation of an Extended Request
250      * 
251      * @return an Extended Request String
252      */
253     @Override
254     public String toString()
255     {
256         StringBuilder sb = new StringBuilder();
257 
258         sb.append( "    Extended request\n" );
259         sb.append( "        Request name : '" ).append( oid ).append( "'\n" );
260 
261         // The controls
262         sb.append( super.toString() );
263 
264         return super.toString( sb.toString() );
265     }
266 }