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.util.StringTools;
029    
030    
031    /**
032     * ExtendedRequest implementation.
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev: 764131 $
036     */
037    public class ExtendedRequestImpl extends InternalAbstractRequest implements InternalExtendedRequest
038    {
039        static final long serialVersionUID = 7916990159044177480L;
040    
041        /** Extended request's Object Identifier or <b>requestName</b> */
042        private String oid;
043    
044        /** Extended request's payload or <b>requestValue</b> */
045        protected byte[] payload;
046    
047        protected InternalResultResponse response;
048    
049    
050        // -----------------------------------------------------------------------
051        // Constructors
052        // -----------------------------------------------------------------------
053    
054        /**
055         * Creates a Lockable ExtendedRequest implementing object used to perform
056         * extended protocol operation on the server.
057         * 
058         * @param id
059         *            the sequential message identifier
060         */
061        public ExtendedRequestImpl(final int id)
062        {
063            super( id, TYPE, true );
064        }
065    
066    
067        // -----------------------------------------------------------------------
068        // ExtendedRequest Interface Method Implementations
069        // -----------------------------------------------------------------------
070    
071        /**
072         * Gets the Object Idendifier corresponding to the extended request type.
073         * This is the <b>requestName</b> portion of the ext. req. PDU.
074         * 
075         * @return the dotted-decimal representation as a String of the OID
076         */
077        public String getOid()
078        {
079            return oid;
080        }
081    
082    
083        /**
084         * Sets the Object Idendifier corresponding to the extended request type.
085         * 
086         * @param oid
087         *            the dotted-decimal representation as a String of the OID
088         */
089        public void setOid( String oid )
090        {
091            this.oid = oid;
092        }
093    
094    
095        /**
096         * Gets the extended request's <b>requestValue</b> portion of the PDU. The
097         * form of the data is request specific and is determined by the extended
098         * request OID.
099         * 
100         * @return byte array of data
101         */
102        public byte[] getPayload()
103        {
104            if ( payload == null )
105            {
106                return null;
107            }
108    
109            final byte[] copy = new byte[ payload.length ];
110            System.arraycopy( payload, 0, copy, 0, payload.length );
111            return copy;
112        }
113    
114    
115        /**
116         * Sets the extended request's <b>requestValue</b> portion of the PDU.
117         * 
118         * @param payload
119         *            byte array of data encapsulating ext. req. parameters
120         */
121        public void setPayload( byte[] payload )
122        {
123            if ( payload != null )
124            {
125                this.payload = new byte[ payload.length ];
126                System.arraycopy( payload, 0, this.payload, 0, payload.length );
127            } else {
128                this.payload = null;
129            }
130        }
131    
132    
133        // ------------------------------------------------------------------------
134        // SingleReplyRequest Interface Method Implementations
135        // ------------------------------------------------------------------------
136    
137        /**
138         * Gets the protocol response message type for this request which produces
139         * at least one response.
140         * 
141         * @return the message type of the response.
142         */
143        public MessageTypeEnum getResponseType()
144        {
145            return RESP_TYPE;
146        }
147    
148    
149        /**
150         * The result containing response for this request.
151         * 
152         * @return the result containing response for this request
153         */
154        public InternalResultResponse getResultResponse()
155        {
156            if ( response == null )
157            {
158                response = new ExtendedResponseImpl( getMessageId() );
159            }
160    
161            return response;
162        }
163    
164    
165        /**
166         * Checks to see if an object equals this ExtendedRequest.
167         * 
168         * @param obj
169         *            the object to be checked for equality
170         * @return true if the obj equals this ExtendedRequest, false otherwise
171         */
172        public boolean equals( Object obj )
173        {
174            if ( obj == this )
175            {
176                return true;
177            }
178    
179            if ( !super.equals( obj ) )
180            {
181                return false;
182            }
183            
184            if ( !( obj instanceof InternalExtendedRequest ) )
185            {
186                return false;
187            }
188    
189            InternalExtendedRequest req = ( InternalExtendedRequest ) obj;
190            if ( oid != null && req.getOid() == null )
191            {
192                return false;
193            }
194    
195            if ( oid == null && req.getOid() != null )
196            {
197                return false;
198            }
199    
200            if ( oid != null && req.getOid() != null )
201            {
202                if ( !oid.equals( req.getOid() ) )
203                {
204                    return false;
205                }
206            }
207    
208            if ( payload != null && req.getPayload() == null )
209            {
210                return false;
211            }
212    
213            if ( payload == null && req.getPayload() != null )
214            {
215                return false;
216            }
217    
218            if ( payload != null && req.getPayload() != null )
219            {
220                if ( !Arrays.equals( payload, req.getPayload() ) )
221                {
222                    return false;
223                }
224            }
225    
226            return true;
227        }
228    
229    
230        /**
231         * Get a String representation of an Extended Request
232         * 
233         * @return an Extended Request String
234         */
235        public String toString()
236        {
237            StringBuffer sb = new StringBuffer();
238    
239            sb.append( "    Extended request\n" );
240            sb.append( "        Request name : '" ).append( oid ).append( "'\n" );
241    
242            if ( oid != null )
243            {
244                sb.append( "        Request value : '" ).append( StringTools.utf8ToString( payload ) ).append( '/' )
245                    .append( StringTools.dumpBytes( payload ) ).append( "'\n" );
246            }
247    
248            return sb.toString();
249        }
250    
251    
252        public String getID()
253        {
254            return getOid();
255        }
256    
257    
258        public byte[] getEncodedValue()
259        {
260            return getPayload();
261        }
262    
263    
264        public ExtendedResponse createExtendedResponse( String id, byte[] berValue, int offset, int length )
265            throws NamingException
266        {
267            return null;
268        }
269    }