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
022
023import java.util.Arrays;
024
025import org.apache.directory.api.util.Strings;
026
027
028/**
029 * IntermediateResponse implementation
030 * 
031 * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
032 */
033public class IntermediateResponseImpl extends AbstractResultResponse implements IntermediateResponse
034{
035    static final long serialVersionUID = -6646752766410531060L;
036
037    /** ResponseName for the intermediate response */
038    protected String responseName;
039
040    /** Response Value for the intermediate response */
041    protected byte[] responseValue;
042
043
044    /**
045     * Creates a new IntermediateResponseImpl instance
046     * @param id The request ID
047     */
048    public IntermediateResponseImpl( int id )
049    {
050        super( id, TYPE );
051    }
052
053
054    // ------------------------------------------------------------------------
055    // IntermediateResponse Interface Method Implementations
056    // ------------------------------------------------------------------------
057
058    /**
059     * Gets the reponseName specific encoded
060     * 
061     * @return the response value
062     */
063    @Override
064    public byte[] getResponseValue()
065    {
066        if ( responseValue == null )
067        {
068            return null;
069        }
070
071        final byte[] copy = new byte[responseValue.length];
072        System.arraycopy( responseValue, 0, copy, 0, responseValue.length );
073        return copy;
074    }
075
076
077    /**
078     * Sets the response value
079     * 
080     * @param value the response value.
081     */
082    @Override
083    public void setResponseValue( byte[] value )
084    {
085        if ( value != null )
086        {
087            this.responseValue = new byte[value.length];
088            System.arraycopy( value, 0, this.responseValue, 0, value.length );
089        }
090        else
091        {
092            this.responseValue = null;
093        }
094    }
095
096
097    /**
098     * Gets the OID uniquely identifying this Intermediate response (a.k.a. its
099     * name).
100     * 
101     * @return the OID of the Intermediate response type.
102     */
103    @Override
104    public String getResponseName()
105    {
106        return ( responseName == null ) ? "" : responseName;
107    }
108
109
110    /**
111     * Sets the OID uniquely identifying this Intermediate response (a.k.a. its
112     * name).
113     * 
114     * @param oid the OID of the Intermediate response type.
115     */
116    @Override
117    public void setResponseName( String oid )
118    {
119        this.responseName = oid;
120    }
121
122
123    /**
124     * {@inheritDoc}
125     */
126    @Override
127    public int hashCode()
128    {
129        int hash = 37;
130        if ( responseName != null )
131        {
132            hash = hash * 17 + responseName.hashCode();
133        }
134        if ( responseValue != null )
135        {
136            hash = hash * 17 + Arrays.hashCode( responseValue );
137        }
138        hash = hash * 17 + super.hashCode();
139
140        return hash;
141    }
142
143
144    /**
145     * Checks to see if an object equals this IntemediateResponse.
146     * 
147     * @param obj the object to be checked for equality
148     * @return true if the obj equals this IntemediateResponse, false otherwise
149     */
150    @Override
151    public boolean equals( Object obj )
152    {
153        if ( obj == this )
154        {
155            return true;
156        }
157
158        if ( !super.equals( obj ) )
159        {
160            return false;
161        }
162
163        if ( !( obj instanceof IntermediateResponse ) )
164        {
165            return false;
166        }
167
168        IntermediateResponse resp = ( IntermediateResponse ) obj;
169
170        if ( ( responseName != null ) && ( resp.getResponseName() == null ) )
171        {
172            return false;
173        }
174
175        if ( ( responseName == null ) && ( resp.getResponseName() != null ) )
176        {
177            return false;
178        }
179
180        if ( ( responseName != null ) && ( resp.getResponseName() != null )
181            && !responseName.equals( resp.getResponseName() ) )
182        {
183            return false;
184        }
185
186        if ( ( responseValue != null ) && ( resp.getResponseValue() == null ) )
187        {
188            return false;
189        }
190
191        if ( ( responseValue == null ) && ( resp.getResponseValue() != null ) )
192        {
193            return false;
194        }
195
196        return ( responseValue == null ) || ( resp.getResponseValue() == null )
197        || Arrays.equals( responseValue, resp.getResponseValue() );
198    }
199
200
201    /**
202     * Get a String representation of an IntermediateResponse
203     * 
204     * @return An IntermediateResponse String
205     */
206    @Override
207    public String toString()
208    {
209        StringBuilder sb = new StringBuilder();
210
211        sb.append( "    Intermediate Response\n" );
212
213        if ( responseName != null )
214        {
215            sb.append( "        Response name :'" ).append( responseName ).append( "'\n" );
216        }
217
218        if ( responseValue != null )
219        {
220            sb.append( "        ResponseValue :'" );
221            sb.append( Strings.dumpBytes( responseValue ) );
222            sb.append( "'\n" );
223        }
224
225        sb.append( super.toString() );
226
227        return super.toString( sb.toString() );
228    }
229}