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 */
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 AbstractRequest implements ExtendedRequest
032{
033    static final long serialVersionUID = 7916990159044177480L;
034
035    /** Extended request's Object Identifier or <b>requestName</b> */
036    private String oid;
037    
038    /** Extended request value as an opaque byte array */
039    private byte[] requestValue;
040
041    /** The associated response */
042    protected ExtendedResponse response;
043
044
045    /**
046     * Creates an ExtendedRequest implementing object used to perform
047     * extended protocol operation on the server.
048     */
049    public OpaqueExtendedRequest()
050    {
051        super( -1, MessageTypeEnum.EXTENDED_REQUEST, true );
052    }
053
054
055    /**
056     * Creates an ExtendedRequest implementing object used to perform
057     * extended protocol operation on the server.
058     * 
059     * @param requestName the extended request name
060     */
061    public OpaqueExtendedRequest( String requestName )
062    {
063        super( -1, MessageTypeEnum.EXTENDED_REQUEST, true );
064        this.oid = requestName;
065    }
066
067
068    /**
069     * Creates an ExtendedRequest implementing object used to perform
070     * extended protocol operation on the server.
071     * 
072     * @param requestValue the embedded value
073     */
074    public OpaqueExtendedRequest( byte[] requestValue )
075    {
076        super( -1, MessageTypeEnum.EXTENDED_REQUEST, true );
077        this.requestValue = requestValue;
078    }
079
080
081    /**
082     * Creates an ExtendedRequest implementing object used to perform
083     * extended protocol operation on the server.
084     * 
085     * @param requestValue the embedded value
086     */
087    public OpaqueExtendedRequest( String requestName, byte[] requestValue )
088    {
089        super( -1, MessageTypeEnum.EXTENDED_REQUEST, true );
090        this.oid = requestName;
091        this.requestValue = requestValue;
092    }
093
094
095    // -----------------------------------------------------------------------
096    // ExtendedRequest Interface Method Implementations
097    // -----------------------------------------------------------------------
098
099    /**
100     * Gets the Object Identifier corresponding to the extended request type.
101     * This is the <b>requestName</b> portion of the ext. req. PDU.
102     * 
103     * @return the dotted-decimal representation as a String of the OID
104     */
105    @Override
106    public String getRequestName()
107    {
108        return oid;
109    }
110
111
112    /**
113     * Sets the Object Identifier corresponding to the extended request type.
114     * 
115     * @param newOid the dotted-decimal representation as a String of the OID
116     */
117    @Override
118    public ExtendedRequest setRequestName( String newOid )
119    {
120        this.oid = newOid;
121
122        return this;
123    }
124
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public ExtendedRequest setMessageId( int messageId )
131    {
132        super.setMessageId( messageId );
133
134        return this;
135    }
136
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public ExtendedRequest addControl( Control control )
143    {
144        return ( ExtendedRequest ) super.addControl( control );
145    }
146
147
148    /**
149     * {@inheritDoc}
150     */
151    @Override
152    public ExtendedRequest addAllControls( Control[] controls )
153    {
154        return ( ExtendedRequest ) super.addAllControls( controls );
155    }
156
157
158    /**
159     * {@inheritDoc}
160     */
161    @Override
162    public ExtendedRequest removeControl( Control control )
163    {
164        return ( ExtendedRequest ) super.removeControl( control );
165    }
166
167
168    // ------------------------------------------------------------------------
169    // SingleReplyRequest Interface Method Implementations
170    // ------------------------------------------------------------------------
171
172    /**
173     * Gets the protocol response message type for this request which produces
174     * at least one response.
175     * 
176     * @return the message type of the response.
177     */
178    @Override
179    public MessageTypeEnum getResponseType()
180    {
181        return MessageTypeEnum.EXTENDED_RESPONSE;
182    }
183
184
185    /**
186     * The result containing response for this request.
187     * 
188     * @return the result containing response for this request
189     */
190    public ExtendedResponse getExtendedResponse()
191    {
192        if ( response == null )
193        {
194            response = new OpaqueExtendedResponse( getMessageId() );
195        }
196
197        return response;
198    }
199
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    public ExtendedResponse getResultResponse()
206    {
207        return getExtendedResponse();
208    }
209
210
211    /**
212     * @return the request value
213     */
214    public byte[] getRequestValue()
215    {
216        return requestValue;
217    }
218
219
220    /**
221     * @param requestValue the requestValue to set
222     */
223    public void setRequestValue( byte[] requestValue )
224    {
225        this.requestValue = requestValue;
226    }
227
228
229    /**
230     * {@inheritDoc}
231     */
232    @Override
233    public int hashCode()
234    {
235        int hash = 37;
236        
237        hash = hash * 17 + super.hashCode();
238
239        if ( oid != null )
240        {
241            hash = hash * 17 + oid.hashCode();
242        }
243        
244        if ( requestValue != null )
245        {
246            for ( byte b : requestValue )
247            { 
248                hash = hash * 17 + b;
249            }
250        }
251
252        return hash;
253    }
254
255
256    /**
257     * Checks to see if an object equals this ExtendedRequest.
258     * 
259     * @param obj the object to be checked for equality
260     * @return true if the obj equals this ExtendedRequest, false otherwise
261     */
262    @Override
263    public boolean equals( Object obj )
264    {
265        if ( obj == this )
266        {
267            return true;
268        }
269
270        if ( !super.equals( obj ) )
271        {
272            return false;
273        }
274
275        if ( !( obj instanceof OpaqueExtendedRequest ) )
276        {
277            return false;
278        }
279
280        OpaqueExtendedRequest extendedRequest = ( OpaqueExtendedRequest ) obj;
281
282        if ( ( ( oid != null ) && !oid.equals( extendedRequest.oid ) )
283            || ( ( oid == null ) && ( extendedRequest.oid != null ) ) )
284        {
285            return false;
286        }
287
288        return Arrays.equals( requestValue, extendedRequest.requestValue );
289    }
290
291
292    /**
293     * Get a String representation of an Extended Request
294     * 
295     * @return an Extended Request String
296     */
297    @Override
298    public String toString()
299    {
300        StringBuilder sb = new StringBuilder();
301
302        sb.append( "    Extended request\n" );
303        sb.append( "        Request name :  '" ).append( oid ).append( "'\n" );
304        sb.append( "        Request value : '" ).append( Strings.dumpBytes( requestValue ) ).append( "'\n" );
305
306        // The controls
307        sb.append( super.toString() );
308
309        return super.toString( sb.toString() );
310    }
311}