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