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 class ExtendedRequestImpl 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      protected ExtendedResponseImpl response;
37  
38  
39      /**
40       * Creates an ExtendedRequest implementing object used to perform
41       * extended protocol operation on the server.
42       */
43      public ExtendedRequestImpl()
44      {
45          super( -1, MessageTypeEnum.EXTENDED_REQUEST, true );
46      }
47  
48  
49      // -----------------------------------------------------------------------
50      // ExtendedRequest Interface Method Implementations
51      // -----------------------------------------------------------------------
52  
53      /**
54       * Gets the Object Identifier corresponding to the extended request type.
55       * This is the <b>requestName</b> portion of the ext. req. PDU.
56       * 
57       * @return the dotted-decimal representation as a String of the OID
58       */
59      @Override
60      public String getRequestName()
61      {
62          return oid;
63      }
64  
65  
66      /**
67       * Sets the Object Identifier corresponding to the extended request type.
68       * 
69       * @param newOid the dotted-decimal representation as a String of the OID
70       */
71      @Override
72      public ExtendedRequest setRequestName( String newOid )
73      {
74          this.oid = newOid;
75  
76          return this;
77      }
78  
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public ExtendedRequest setMessageId( int messageId )
85      {
86          super.setMessageId( messageId );
87  
88          return this;
89      }
90  
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public ExtendedRequest addControl( Control control )
97      {
98          return ( ExtendedRequest ) super.addControl( control );
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public ExtendedRequest addAllControls( Control[] controls )
107     {
108         return ( ExtendedRequest ) super.addAllControls( controls );
109     }
110 
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public ExtendedRequest removeControl( Control control )
117     {
118         return ( ExtendedRequest ) super.removeControl( control );
119     }
120 
121 
122     // ------------------------------------------------------------------------
123     // SingleReplyRequest Interface Method Implementations
124     // ------------------------------------------------------------------------
125 
126     /**
127      * Gets the protocol response message type for this request which produces
128      * at least one response.
129      * 
130      * @return the message type of the response.
131      */
132     @Override
133     public MessageTypeEnum getResponseType()
134     {
135         return MessageTypeEnum.EXTENDED_RESPONSE;
136     }
137 
138 
139     /**
140      * The result containing response for this request.
141      * 
142      * @return the result containing response for this request
143      */
144     public ExtendedResponse getExtendedResponse()
145     {
146         if ( response == null )
147         {
148             response = new ExtendedResponseImpl( getMessageId() );
149         }
150 
151         return response;
152     }
153 
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public ExtendedResponse getResultResponse()
160     {
161         return getExtendedResponse();
162     }
163 
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public int hashCode()
170     {
171         int hash = 37;
172         if ( oid != null )
173         {
174             hash = hash * 17 + oid.hashCode();
175         }
176         hash = hash * 17 + super.hashCode();
177 
178         return hash;
179     }
180 
181 
182     /**
183      * Checks to see if an object equals this ExtendedRequest.
184      * 
185      * @param obj the object to be checked for equality
186      * @return true if the obj equals this ExtendedRequest, false otherwise
187      */
188     @Override
189     public boolean equals( Object obj )
190     {
191         if ( obj == this )
192         {
193             return true;
194         }
195 
196         if ( !super.equals( obj ) )
197         {
198             return false;
199         }
200 
201         if ( !( obj instanceof ExtendedRequest ) )
202         {
203             return false;
204         }
205 
206         ExtendedRequest req = ( ExtendedRequest ) obj;
207 
208         if ( ( oid != null ) && ( req.getRequestName() == null ) )
209         {
210             return false;
211         }
212 
213         if ( ( oid == null ) && ( req.getRequestName() != null ) )
214         {
215             return false;
216         }
217 
218         if ( ( oid != null ) && ( req.getRequestName() != null ) && !oid.equals( req.getRequestName() ) )
219         {
220             return false;
221         }
222 
223         return true;
224     }
225 
226 
227     /**
228      * Get a String representation of an Extended Request
229      * 
230      * @return an Extended Request String
231      */
232     @Override
233     public String toString()
234     {
235         StringBuilder sb = new StringBuilder();
236 
237         sb.append( "    Extended request\n" );
238         sb.append( "        Request name : '" ).append( oid ).append( "'\n" );
239 
240         // The controls
241         sb.append( super.toString() );
242 
243         return super.toString( sb.toString() );
244     }
245 }