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