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 *    https://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 */
020package org.apache.directory.api.ldap.model.message;
021
022import java.util.Arrays;
023
024import org.apache.directory.api.util.Strings;
025
026/**
027 * ExtendedRequest basic implementation.
028 * 
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class OpaqueExtendedRequest extends AbstractExtendedRequest
032{
033    static final long serialVersionUID = 7916990159044177480L;
034
035    /** Extended request value as an opaque byte array */
036    private byte[] requestValue;
037
038    /** The associated response */
039    protected ExtendedResponse response;
040
041
042    /**
043     * Creates an ExtendedRequest implementing object used to perform
044     * extended protocol operation on the server.
045     */
046    public OpaqueExtendedRequest()
047    {
048        super( -1 );
049    }
050
051
052    /**
053     * Creates an ExtendedRequest implementing object used to perform
054     * extended protocol operation on the server.
055     * @param id The message ID
056     */
057    public OpaqueExtendedRequest( int id )
058    {
059        super( id );
060    }
061
062
063    /**
064     * Creates an ExtendedRequest implementing object used to perform
065     * extended protocol operation on the server.
066     * 
067     * @param requestName the extended request name
068     */
069    public OpaqueExtendedRequest( String requestName )
070    {
071        super( -1 );
072        this.oid = requestName;
073    }
074
075
076    /**
077     * Creates an ExtendedRequest implementing object used to perform
078     * extended protocol operation on the server.
079     * 
080     * @param requestValue the embedded value
081     */
082    public OpaqueExtendedRequest( byte[] requestValue )
083    {
084        super( -1 );
085        this.requestValue = requestValue;
086    }
087
088
089    /**
090     * Creates an ExtendedRequest implementing object used to perform
091     * extended protocol operation on the server.
092     * 
093     * @param requestName The extended request OID
094     * @param requestValue the embedded value
095     */
096    public OpaqueExtendedRequest( String requestName, byte[] requestValue )
097    {
098        super( -1 );
099        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}