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   *    http://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      /** Response Value for the intermediate response */
41      protected byte[] responseValue;
42  
43  
44      public IntermediateResponseImpl( int id )
45      {
46          super( id, TYPE );
47      }
48  
49  
50      // ------------------------------------------------------------------------
51      // IntermediateResponse Interface Method Implementations
52      // ------------------------------------------------------------------------
53  
54      /**
55       * Gets the reponseName specific encoded
56       * 
57       * @return the response value
58       */
59      public byte[] getResponseValue()
60      {
61          if ( responseValue == null )
62          {
63              return null;
64          }
65  
66          final byte[] copy = new byte[responseValue.length];
67          System.arraycopy( responseValue, 0, copy, 0, responseValue.length );
68          return copy;
69      }
70  
71  
72      /**
73       * Sets the response value
74       * 
75       * @param value the response value.
76       */
77      public void setResponseValue( byte[] value )
78      {
79          if ( value != null )
80          {
81              this.responseValue = new byte[value.length];
82              System.arraycopy( value, 0, this.responseValue, 0, value.length );
83          }
84          else
85          {
86              this.responseValue = null;
87          }
88      }
89  
90  
91      /**
92       * Gets the OID uniquely identifying this Intermediate response (a.k.a. its
93       * name).
94       * 
95       * @return the OID of the Intermediate response type.
96       */
97      public String getResponseName()
98      {
99          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 }