View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message;
21  
22  import java.util.Arrays;
23  
24  import org.apache.directory.api.util.Strings;
25  
26  /**
27   * ExtendedResponse basic implementation.
28   * 
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public class OpaqueExtendedResponse extends AbstractExtendedResponse
32  {
33      static final long serialVersionUID = 7916990159044177480L;
34  
35      /** Extended response value as an opaque byte array */
36      private byte[] responseValue;
37  
38  
39      /**
40       * Creates an ExtendedResponse implementing object used to perform
41       * extended protocol operation on the server.
42       */
43      public OpaqueExtendedResponse()
44      {
45          super( -1 );
46      }
47  
48  
49      /**
50       * Creates an ExtendedResponse implementing object used to perform
51       * extended protocol operation on the server.
52       * 
53       * @param messageId the messageID
54       */
55      public OpaqueExtendedResponse( int messageId )
56      {
57          super( messageId );
58      }
59  
60  
61      /**
62       * Creates an ExtendedResponse implementing object used to perform
63       * extended protocol operation on the server.
64       * 
65       * @param responseName The extended response OID
66       */
67      public OpaqueExtendedResponse( String responseName )
68      {
69          super( -1, responseName );
70      }
71  
72  
73      /**
74       * Creates an ExtendedResponse implementing object used to perform
75       * extended protocol operation on the server.
76       * 
77       * @param messageId the messageID
78       * @param responseName The extended response OID
79       */
80      public OpaqueExtendedResponse( int messageId, String responseName )
81      {
82          super( messageId, responseName );
83      }
84  
85      
86      // ------------------------------------------------------------------------
87      // SingleReplyRequest Interface Method Implementations
88      // ------------------------------------------------------------------------
89  
90      /**
91       * @return the response value
92       */
93      public byte[] getResponseValue()
94      {
95          return responseValue;
96      }
97  
98  
99      /**
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 }