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  import java.util.Arrays;
23  
24  import org.apache.directory.api.util.Strings;
25  
26  /**
27   * ExtendedRequest basic implementation.
28   * 
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public class OpaqueExtendedRequest extends AbstractExtendedRequest
32  {
33      static final long serialVersionUID = 7916990159044177480L;
34  
35      /** Extended request value as an opaque byte array */
36      private byte[] requestValue;
37  
38      /** The associated response */
39      protected ExtendedResponse response;
40  
41  
42      /**
43       * Creates an ExtendedRequest implementing object used to perform
44       * extended protocol operation on the server.
45       */
46      public OpaqueExtendedRequest()
47      {
48          super( -1 );
49      }
50  
51  
52      /**
53       * Creates an ExtendedRequest implementing object used to perform
54       * extended protocol operation on the server.
55       * @param id The message ID
56       */
57      public OpaqueExtendedRequest( int id )
58      {
59          super( id );
60      }
61  
62  
63      /**
64       * Creates an ExtendedRequest implementing object used to perform
65       * extended protocol operation on the server.
66       * 
67       * @param requestName the extended request name
68       */
69      public OpaqueExtendedRequest( String requestName )
70      {
71          super( -1 );
72          this.oid = requestName;
73      }
74  
75  
76      /**
77       * Creates an ExtendedRequest implementing object used to perform
78       * extended protocol operation on the server.
79       * 
80       * @param requestValue the embedded value
81       */
82      public OpaqueExtendedRequest( byte[] requestValue )
83      {
84          super( -1 );
85          this.requestValue = requestValue;
86      }
87  
88  
89      /**
90       * Creates an ExtendedRequest implementing object used to perform
91       * extended protocol operation on the server.
92       * 
93       * @param requestName The extended request OID
94       * @param requestValue the embedded value
95       */
96      public OpaqueExtendedRequest( String requestName, byte[] requestValue )
97      {
98          super( -1 );
99          this.oid = requestName;
100         this.requestValue = requestValue;
101     }
102 
103 
104     // -----------------------------------------------------------------------
105     // ExtendedRequest Interface Method Implementations
106     // -----------------------------------------------------------------------
107 
108     /**
109      * Gets the Object Identifier corresponding to the extended request type.
110      * This is the <b>requestName</b> portion of the ext. req. PDU.
111      * 
112      * @return the dotted-decimal representation as a String of the OID
113      */
114     @Override
115     public String getRequestName()
116     {
117         return oid;
118     }
119 
120 
121     /**
122      * Sets the Object Identifier corresponding to the extended request type.
123      * 
124      * @param newOid the dotted-decimal representation as a String of the OID
125      */
126     @Override
127     public ExtendedRequest setRequestName( String newOid )
128     {
129         this.oid = newOid;
130 
131         return this;
132     }
133 
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public ExtendedRequest setMessageId( int messageId )
140     {
141         super.setMessageId( messageId );
142 
143         return this;
144     }
145 
146 
147     /**
148      * {@inheritDoc}
149      */
150     @Override
151     public ExtendedRequest addControl( Control control )
152     {
153         return ( ExtendedRequest ) super.addControl( control );
154     }
155 
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public ExtendedRequest addAllControls( Control[] controls )
162     {
163         return ( ExtendedRequest ) super.addAllControls( controls );
164     }
165 
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public ExtendedRequest removeControl( Control control )
172     {
173         return ( ExtendedRequest ) super.removeControl( control );
174     }
175 
176 
177     // ------------------------------------------------------------------------
178     // SingleReplyRequest Interface Method Implementations
179     // ------------------------------------------------------------------------
180 
181     /**
182      * Gets the protocol response message type for this request which produces
183      * at least one response.
184      * 
185      * @return the message type of the response.
186      */
187     @Override
188     public MessageTypeEnum getResponseType()
189     {
190         return MessageTypeEnum.EXTENDED_RESPONSE;
191     }
192 
193 
194     /**
195      * The result containing response for this request.
196      * 
197      * @return the result containing response for this request
198      */
199     public ExtendedResponse getExtendedResponse()
200     {
201         if ( response == null )
202         {
203             response = new OpaqueExtendedResponse( getMessageId() );
204         }
205 
206         return response;
207     }
208 
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public ExtendedResponse getResultResponse()
215     {
216         return getExtendedResponse();
217     }
218 
219 
220     /**
221      * @return the request value
222      */
223     public byte[] getRequestValue()
224     {
225         return requestValue;
226     }
227 
228 
229     /**
230      * @param requestValue the requestValue to set
231      */
232     public void setRequestValue( byte[] requestValue )
233     {
234         this.requestValue = requestValue;
235     }
236 
237 
238     /**
239      * {@inheritDoc}
240      */
241     @Override
242     public int hashCode()
243     {
244         int hash = 37;
245         
246         hash = hash * 17 + super.hashCode();
247 
248         if ( oid != null )
249         {
250             hash = hash * 17 + oid.hashCode();
251         }
252         
253         if ( requestValue != null )
254         {
255             for ( byte b : requestValue )
256             { 
257                 hash = hash * 17 + b;
258             }
259         }
260 
261         return hash;
262     }
263 
264 
265     /**
266      * Checks to see if an object equals this ExtendedRequest.
267      * 
268      * @param obj the object to be checked for equality
269      * @return true if the obj equals this ExtendedRequest, false otherwise
270      */
271     @Override
272     public boolean equals( Object obj )
273     {
274         if ( obj == this )
275         {
276             return true;
277         }
278 
279         if ( !super.equals( obj ) )
280         {
281             return false;
282         }
283 
284         if ( !( obj instanceof OpaqueExtendedRequest ) )
285         {
286             return false;
287         }
288 
289         OpaqueExtendedRequest extendedRequest = ( OpaqueExtendedRequest ) obj;
290 
291         if ( ( ( oid != null ) && !oid.equals( extendedRequest.oid ) )
292             || ( ( oid == null ) && ( extendedRequest.oid != null ) ) )
293         {
294             return false;
295         }
296 
297         return Arrays.equals( requestValue, extendedRequest.requestValue );
298     }
299 
300 
301     /**
302      * Get a String representation of an Extended Request
303      * 
304      * @return an Extended Request String
305      */
306     @Override
307     public String toString()
308     {
309         StringBuilder sb = new StringBuilder();
310 
311         sb.append( "    Extended request\n" );
312         sb.append( "        Request name :  '" ).append( oid ).append( "'\n" );
313         sb.append( "        Request value : '" ).append( Strings.dumpBytes( requestValue ) ).append( "'\n" );
314 
315         // The controls
316         sb.append( super.toString() );
317 
318         return super.toString( sb.toString() );
319     }
320 }