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  
23  import java.util.Arrays;
24  
25  import org.apache.directory.api.util.Strings;
26  
27  
28  /**
29   * IntermediateResponse implementation
30   * 
31   * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
32   */
33  public class IntermediateResponseImpl extends AbstractResultResponse implements IntermediateResponse
34  {
35      static final long serialVersionUID = -6646752766410531060L;
36  
37      /** ResponseName for the intermediate response */
38      protected String responseName;
39  
40      /** Intermediate response message type enumeration value */
41      private static final MessageTypeEnum TYPE = MessageTypeEnum.INTERMEDIATE_RESPONSE;
42  
43      /** Response Value for the intermediate response */
44      protected byte[] responseValue;
45  
46      /**
47       * Creates an IntermediateResponseImpl instance
48       * 
49       * @param responseName the IntermediateResponse's name
50       */
51      public IntermediateResponseImpl( String responseName )
52      {
53          super( -1, TYPE );
54          this.responseName = responseName;
55      }
56  
57  
58      /**
59       * Creates an IntermediateResponseImpl instance
60       * 
61       * @param id the session unique message id
62       * @param responseName the IntermediateResponse's name
63       */
64      public IntermediateResponseImpl( int id, String responseName )
65      {
66          super( id, TYPE );
67          this.responseName = responseName;
68      }
69  
70  
71      /**
72       * Creates a new IntermediateResponseImpl instance
73       * @param id The request ID
74       */
75      public IntermediateResponseImpl( int id )
76      {
77          super( id, TYPE );
78      }
79  
80  
81      // ------------------------------------------------------------------------
82      // IntermediateResponse Interface Method Implementations
83      // ------------------------------------------------------------------------
84  
85      /**
86       * Gets the reponseName specific encoded
87       * 
88       * @return the response value
89       */
90      @Override
91      public byte[] getResponseValue()
92      {
93          if ( responseValue == null )
94          {
95              return null;
96          }
97  
98          final byte[] copy = new byte[responseValue.length];
99          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 }