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