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 *    https://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
022import java.util.Arrays;
023
024import org.apache.directory.api.util.Strings;
025
026/**
027 * ExtendedResponse basic implementation.
028 * 
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class OpaqueExtendedResponse extends AbstractExtendedResponse
032{
033    static final long serialVersionUID = 7916990159044177480L;
034
035    /** Extended response value as an opaque byte array */
036    private byte[] responseValue;
037
038
039    /**
040     * Creates an ExtendedResponse implementing object used to perform
041     * extended protocol operation on the server.
042     */
043    public OpaqueExtendedResponse()
044    {
045        super( -1 );
046    }
047
048
049    /**
050     * Creates an ExtendedResponse implementing object used to perform
051     * extended protocol operation on the server.
052     * 
053     * @param messageId the messageID
054     */
055    public OpaqueExtendedResponse( int messageId )
056    {
057        super( messageId );
058    }
059
060
061    /**
062     * Creates an ExtendedResponse implementing object used to perform
063     * extended protocol operation on the server.
064     * 
065     * @param responseName The extended response OID
066     */
067    public OpaqueExtendedResponse( String responseName )
068    {
069        super( -1, responseName );
070    }
071
072
073    /**
074     * Creates an ExtendedResponse implementing object used to perform
075     * extended protocol operation on the server.
076     * 
077     * @param messageId the messageID
078     * @param responseName The extended response OID
079     */
080    public OpaqueExtendedResponse( int messageId, String responseName )
081    {
082        super( messageId, responseName );
083    }
084
085    
086    // ------------------------------------------------------------------------
087    // SingleReplyRequest Interface Method Implementations
088    // ------------------------------------------------------------------------
089
090    /**
091     * @return the response value
092     */
093    public byte[] getResponseValue()
094    {
095        return responseValue;
096    }
097
098
099    /**
100     * @param responseValue the responseValue to set
101     */
102    public void setResponseValue( byte[] responseValue )
103    {
104        this.responseValue = responseValue;
105    }
106
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public int hashCode()
113    {
114        int hash = 37;
115        
116        hash = hash * 17 + super.hashCode();
117
118        if ( responseName != null )
119        {
120            hash = hash * 17 + responseName.hashCode();
121        }
122        
123        if ( responseValue != null )
124        {
125            for ( byte b : responseValue )
126            { 
127                hash = hash * 17 + b;
128            }
129        }
130
131        return hash;
132    }
133
134
135    /**
136     * Checks to see if an object equals this ExtendedRequest.
137     * 
138     * @param obj the object to be checked for equality
139     * @return true if the obj equals this ExtendedRequest, false otherwise
140     */
141    @Override
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 OpaqueExtendedResponse ) )
155        {
156            return false;
157        }
158
159        OpaqueExtendedResponse extendedRequest = ( OpaqueExtendedResponse ) obj;
160
161        if ( ( ( responseName != null ) && !responseName.equals( extendedRequest.responseName ) )
162            || ( ( responseName == null ) && ( extendedRequest.responseName != null ) ) )
163        {
164            return false;
165        }
166
167        return Arrays.equals( responseValue, extendedRequest.responseValue );
168    }
169
170
171    /**
172     * Get a String representation of an Extended Request
173     * 
174     * @return an Extended Request String
175     */
176    @Override
177    public String toString()
178    {
179        StringBuilder sb = new StringBuilder();
180
181        sb.append( "    Extended response\n" );
182        sb.append( "        Response name :  '" ).append( responseName ).append( "'\n" );
183        sb.append( "        Response value : '" ).append( Strings.dumpBytes( responseValue ) ).append( "'\n" );
184
185        return super.toString( sb.toString() );
186    }
187}