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      /**
45       * Creates a new IntermediateResponseImpl instance
46       * @param id The request ID
47       */
48      public IntermediateResponseImpl( int id )
49      {
50          super( id, TYPE );
51      }
52  
53  
54      // ------------------------------------------------------------------------
55      // IntermediateResponse Interface Method Implementations
56      // ------------------------------------------------------------------------
57  
58      /**
59       * Gets the reponseName specific encoded
60       * 
61       * @return the response value
62       */
63      @Override
64      public byte[] getResponseValue()
65      {
66          if ( responseValue == null )
67          {
68              return null;
69          }
70  
71          final byte[] copy = new byte[responseValue.length];
72          System.arraycopy( responseValue, 0, copy, 0, responseValue.length );
73          return copy;
74      }
75  
76  
77      /**
78       * Sets the response value
79       * 
80       * @param value the response value.
81       */
82      @Override
83      public void setResponseValue( byte[] value )
84      {
85          if ( value != null )
86          {
87              this.responseValue = new byte[value.length];
88              System.arraycopy( value, 0, this.responseValue, 0, value.length );
89          }
90          else
91          {
92              this.responseValue = null;
93          }
94      }
95  
96  
97      /**
98       * Gets the OID uniquely identifying this Intermediate response (a.k.a. its
99       * name).
100      * 
101      * @return the OID of the Intermediate response type.
102      */
103     @Override
104     public String getResponseName()
105     {
106         return ( responseName == null ) ? "" : responseName;
107     }
108 
109 
110     /**
111      * Sets the OID uniquely identifying this Intermediate response (a.k.a. its
112      * name).
113      * 
114      * @param oid the OID of the Intermediate response type.
115      */
116     @Override
117     public void setResponseName( String oid )
118     {
119         this.responseName = oid;
120     }
121 
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public int hashCode()
128     {
129         int hash = 37;
130         if ( responseName != null )
131         {
132             hash = hash * 17 + responseName.hashCode();
133         }
134         if ( responseValue != null )
135         {
136             hash = hash * 17 + Arrays.hashCode( responseValue );
137         }
138         hash = hash * 17 + super.hashCode();
139 
140         return hash;
141     }
142 
143 
144     /**
145      * Checks to see if an object equals this IntemediateResponse.
146      * 
147      * @param obj the object to be checked for equality
148      * @return true if the obj equals this IntemediateResponse, false otherwise
149      */
150     @Override
151     public boolean equals( Object obj )
152     {
153         if ( obj == this )
154         {
155             return true;
156         }
157 
158         if ( !super.equals( obj ) )
159         {
160             return false;
161         }
162 
163         if ( !( obj instanceof IntermediateResponse ) )
164         {
165             return false;
166         }
167 
168         IntermediateResponse resp = ( IntermediateResponse ) obj;
169 
170         if ( ( responseName != null ) && ( resp.getResponseName() == null ) )
171         {
172             return false;
173         }
174 
175         if ( ( responseName == null ) && ( resp.getResponseName() != null ) )
176         {
177             return false;
178         }
179 
180         if ( ( responseName != null ) && ( resp.getResponseName() != null )
181             && !responseName.equals( resp.getResponseName() ) )
182         {
183             return false;
184         }
185 
186         if ( ( responseValue != null ) && ( resp.getResponseValue() == null ) )
187         {
188             return false;
189         }
190 
191         if ( ( responseValue == null ) && ( resp.getResponseValue() != null ) )
192         {
193             return false;
194         }
195 
196         return ( responseValue == null ) || ( resp.getResponseValue() == null )
197         || Arrays.equals( responseValue, resp.getResponseValue() );
198     }
199 
200 
201     /**
202      * Get a String representation of an IntermediateResponse
203      * 
204      * @return An IntermediateResponse String
205      */
206     @Override
207     public String toString()
208     {
209         StringBuilder sb = new StringBuilder();
210 
211         sb.append( "    Intermediate Response\n" );
212 
213         if ( responseName != null )
214         {
215             sb.append( "        Response name :'" ).append( responseName ).append( "'\n" );
216         }
217 
218         if ( responseValue != null )
219         {
220             sb.append( "        ResponseValue :'" );
221             sb.append( Strings.dumpBytes( responseValue ) );
222             sb.append( "'\n" );
223         }
224 
225         sb.append( super.toString() );
226 
227         return super.toString( sb.toString() );
228     }
229 }