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 org.apache.directory.shared.ldap.message.internal.InternalAbstractResultResponse;
026    import org.apache.directory.shared.ldap.message.internal.InternalExtendedResponse;
027    
028    /**
029     * Lockable ExtendedResponse implementation
030     * 
031     * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
032     * @version $Rev: 905344 $
033     */
034    public class ExtendedResponseImpl extends InternalAbstractResultResponse implements InternalExtendedResponse
035    {
036        static final long serialVersionUID = -6646752766410531060L;
037    
038        /** Object identifier for the extended response */
039        protected String oid;
040    
041        /** Values encoded in the extended response payload */
042        protected byte[] value;
043    
044    
045        // ------------------------------------------------------------------------
046        // Constructors
047        // ------------------------------------------------------------------------
048    
049        /**
050         * Creates a Lockable ExtendedResponse as a reply to an ExtendedRequest.
051         * 
052         * @param id
053         *            the session unique message id
054         */
055        public ExtendedResponseImpl( final int id, String oid )
056        {
057            super( id, TYPE );
058            this.oid = oid;
059        }
060    
061    
062        public ExtendedResponseImpl( int id )
063        {
064            super( id, TYPE );
065        }
066    
067    
068        // ------------------------------------------------------------------------
069        // ExtendedResponse Interface Method Implementations
070        // ------------------------------------------------------------------------
071    
072        /**
073         * Gets the reponse OID specific encoded response values.
074         * 
075         * @return the response specific encoded response values.
076         */
077        public byte[] getResponse()
078        {
079            if ( value == null )
080            {
081                return null;
082            }
083    
084            final byte[] copy = new byte[ value.length ];
085            System.arraycopy( value, 0, copy, 0, value.length );
086            return copy;
087        }
088    
089    
090        /**
091         * Sets the response OID specific encoded response values.
092         * 
093         * @param value
094         *            the response specific encoded response values.
095         */
096        public void setResponse( byte[] value )
097        {
098            if ( value != null )
099            {
100                this.value = new byte[ value.length ];
101                System.arraycopy( value, 0, this.value, 0, value.length );
102            } else {
103                this.value = null;
104            }
105        }
106        
107        
108        public void setOid( String oid )
109        {
110            this.oid = oid;
111        }
112    
113    
114        /**
115         * Gets the OID uniquely identifying this extended response (a.k.a. its
116         * name).
117         * 
118         * @return the OID of the extended response type.
119         */
120        public String getResponseName()
121        {
122            return oid;
123        }
124    
125    
126        /**
127         * Sets the OID uniquely identifying this extended response (a.k.a. its
128         * name).
129         * 
130         * @param oid
131         *            the OID of the extended response type.
132         */
133        public void setResponseName( String oid )
134        {
135            this.oid = oid;
136        }
137    
138    
139        /**
140         * Checks to see if an object equals this ExtendedRequest.
141         * 
142         * @param obj
143         *            the object to be checked for equality
144         * @return true if the obj equals this ExtendedRequest, false otherwise
145         */
146        public boolean equals( Object obj )
147        {
148            if ( obj == this )
149            {
150                return true;
151            }
152    
153            if ( !super.equals( obj ) )
154            {
155                return false;
156            }
157            
158            if ( !( obj instanceof InternalExtendedResponse ) )
159            {
160                return false;
161            }
162    
163            InternalExtendedResponse resp = ( InternalExtendedResponse ) obj;
164    
165            if ( oid != null && resp.getResponseName() == null )
166            {
167                return false;
168            }
169    
170            if ( oid == null && resp.getResponseName() != null )
171            {
172                return false;
173            }
174    
175            if ( oid != null && resp.getResponseName() != null )
176            {
177                if ( !oid.equals( resp.getResponseName() ) )
178                {
179                    return false;
180                }
181            }
182    
183            if ( value != null && resp.getResponse() == null )
184            {
185                return false;
186            }
187    
188            if ( value == null && resp.getResponse() != null )
189            {
190                return false;
191            }
192    
193            if ( value != null && resp.getResponse() != null )
194            {
195                if ( !Arrays.equals( value, resp.getResponse() ) )
196                {
197                    return false;
198                }
199            }
200    
201            return true;
202        }
203    
204    
205        public String getID()
206        {
207            return getResponseName();
208        }
209    
210    
211        public byte[] getEncodedValue()
212        {
213            return getResponse();
214        }
215    }