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