001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.message;
021
022
023 import java.util.Arrays;
024
025 import javax.naming.NamingException;
026 import javax.naming.ldap.ExtendedResponse;
027
028 import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
029 import org.apache.directory.shared.ldap.message.internal.InternalAbstractRequest;
030 import org.apache.directory.shared.ldap.message.internal.InternalExtendedRequest;
031 import org.apache.directory.shared.ldap.message.internal.InternalResultResponse;
032 import org.apache.directory.shared.ldap.util.StringTools;
033
034
035 /**
036 * ExtendedRequest implementation.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 * @version $Rev: 910150 $
040 */
041 public class ExtendedRequestImpl extends InternalAbstractRequest implements InternalExtendedRequest
042 {
043 static final long serialVersionUID = 7916990159044177480L;
044
045 /** Extended request's Object Identifier or <b>requestName</b> */
046 private String oid;
047
048 /** Extended request's payload or <b>requestValue</b> */
049 protected byte[] payload;
050
051 protected InternalResultResponse response;
052
053
054 // -----------------------------------------------------------------------
055 // Constructors
056 // -----------------------------------------------------------------------
057
058 /**
059 * Creates a Lockable ExtendedRequest implementing object used to perform
060 * extended protocol operation on the server.
061 *
062 * @param id
063 * the sequential message identifier
064 */
065 public ExtendedRequestImpl(final int id)
066 {
067 super( id, TYPE, true );
068 }
069
070
071 // -----------------------------------------------------------------------
072 // ExtendedRequest Interface Method Implementations
073 // -----------------------------------------------------------------------
074
075 /**
076 * Gets the Object Idendifier corresponding to the extended request type.
077 * This is the <b>requestName</b> portion of the ext. req. PDU.
078 *
079 * @return the dotted-decimal representation as a String of the OID
080 */
081 public String getOid()
082 {
083 return oid;
084 }
085
086
087 /**
088 * Sets the Object Idendifier corresponding to the extended request type.
089 *
090 * @param oid
091 * the dotted-decimal representation as a String of the OID
092 */
093 public void setOid( String oid )
094 {
095 this.oid = oid;
096 }
097
098
099 /**
100 * Gets the extended request's <b>requestValue</b> portion of the PDU. The
101 * form of the data is request specific and is determined by the extended
102 * request OID.
103 *
104 * @return byte array of data
105 */
106 public byte[] getPayload()
107 {
108 if ( payload == null )
109 {
110 return null;
111 }
112
113 final byte[] copy = new byte[ payload.length ];
114 System.arraycopy( payload, 0, copy, 0, payload.length );
115 return copy;
116 }
117
118
119 /**
120 * Sets the extended request's <b>requestValue</b> portion of the PDU.
121 *
122 * @param payload
123 * byte array of data encapsulating ext. req. parameters
124 */
125 public void setPayload( byte[] payload )
126 {
127 if ( payload != null )
128 {
129 this.payload = new byte[ payload.length ];
130 System.arraycopy( payload, 0, this.payload, 0, payload.length );
131 } else {
132 this.payload = null;
133 }
134 }
135
136
137 // ------------------------------------------------------------------------
138 // SingleReplyRequest Interface Method Implementations
139 // ------------------------------------------------------------------------
140
141 /**
142 * Gets the protocol response message type for this request which produces
143 * at least one response.
144 *
145 * @return the message type of the response.
146 */
147 public MessageTypeEnum getResponseType()
148 {
149 return RESP_TYPE;
150 }
151
152
153 /**
154 * The result containing response for this request.
155 *
156 * @return the result containing response for this request
157 */
158 public InternalResultResponse getResultResponse()
159 {
160 if ( response == null )
161 {
162 response = new ExtendedResponseImpl( getMessageId() );
163 }
164
165 return response;
166 }
167
168
169 /**
170 * Checks to see if an object equals this ExtendedRequest.
171 *
172 * @param obj
173 * the object to be checked for equality
174 * @return true if the obj equals this ExtendedRequest, false otherwise
175 */
176 public boolean equals( Object obj )
177 {
178 if ( obj == this )
179 {
180 return true;
181 }
182
183 if ( !super.equals( obj ) )
184 {
185 return false;
186 }
187
188 if ( !( obj instanceof InternalExtendedRequest ) )
189 {
190 return false;
191 }
192
193 InternalExtendedRequest req = ( InternalExtendedRequest ) obj;
194 if ( oid != null && req.getOid() == null )
195 {
196 return false;
197 }
198
199 if ( oid == null && req.getOid() != null )
200 {
201 return false;
202 }
203
204 if ( oid != null && req.getOid() != null )
205 {
206 if ( !oid.equals( req.getOid() ) )
207 {
208 return false;
209 }
210 }
211
212 if ( payload != null && req.getPayload() == null )
213 {
214 return false;
215 }
216
217 if ( payload == null && req.getPayload() != null )
218 {
219 return false;
220 }
221
222 if ( payload != null && req.getPayload() != null )
223 {
224 if ( !Arrays.equals( payload, req.getPayload() ) )
225 {
226 return false;
227 }
228 }
229
230 return true;
231 }
232
233
234 /**
235 * Get a String representation of an Extended Request
236 *
237 * @return an Extended Request String
238 */
239 public String toString()
240 {
241 StringBuffer sb = new StringBuffer();
242
243 sb.append( " Extended request\n" );
244 sb.append( " Request name : '" ).append( oid ).append( "'\n" );
245
246 if ( oid != null )
247 {
248 sb.append( " Request value : '" ).append( StringTools.utf8ToString( payload ) ).append( '/' )
249 .append( StringTools.dumpBytes( payload ) ).append( "'\n" );
250 }
251
252 return sb.toString();
253 }
254
255
256 public String getID()
257 {
258 return getOid();
259 }
260
261
262 public byte[] getEncodedValue()
263 {
264 return getPayload();
265 }
266
267
268 public ExtendedResponse createExtendedResponse( String id, byte[] berValue, int offset, int length )
269 throws NamingException
270 {
271 return null;
272 }
273 }