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