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 org.apache.directory.api.ldap.model.entry.BinaryValue;
24  import org.apache.directory.api.ldap.model.entry.StringValue;
25  import org.apache.directory.api.ldap.model.entry.Value;
26  import org.apache.directory.api.ldap.model.name.Dn;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * Comparison request implementation.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class CompareRequestImpl extends AbstractAbandonableRequest implements CompareRequest
36  {
37      static final long serialVersionUID = 1699731530016468977L;
38  
39      /** Distinguished name identifying the compared entry */
40      private Dn name;
41  
42      /** The id of the attribute used in the comparison */
43      private String attrId;
44  
45      /** The value of the attribute used in the comparison */
46      private Value<?> attrVal;
47  
48      /** The associated response */
49      private CompareResponse response;
50  
51  
52      // ------------------------------------------------------------------------
53      // Constructors
54      // ------------------------------------------------------------------------
55      /**
56       * Creates an CompareRequest implementation to compare a named entry with an
57       * attribute value assertion pair.
58       */
59      public CompareRequestImpl()
60      {
61          super( -1, MessageTypeEnum.COMPARE_REQUEST );
62      }
63  
64  
65      // ------------------------------------------------------------------------
66      // ComparisonRequest Interface Method Implementations
67      // ------------------------------------------------------------------------
68  
69      /**
70       * Gets the distinguished name of the entry to be compared using the
71       * attribute value assertion.
72       * 
73       * @return the Dn of the compared entry.
74       */
75      public Dn getName()
76      {
77          return name;
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      public CompareRequest setName( Dn name )
85      {
86          this.name = name;
87  
88          return this;
89      }
90  
91  
92      /**
93       * Gets the attribute value to use in making the comparison.
94       * 
95       * @return the attribute value to used in comparison.
96       */
97      public Value<?> getAssertionValue()
98      {
99          return attrVal;
100     }
101 
102 
103     /**
104      * {@inheritDoc}
105      */
106     public CompareRequest setAssertionValue( String value )
107     {
108         this.attrVal = new StringValue( value );
109 
110         return this;
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     public CompareRequest setAssertionValue( byte[] value )
118     {
119         if ( value != null )
120         {
121             this.attrVal = new BinaryValue( value );
122         }
123         else
124         {
125             this.attrVal = null;
126         }
127 
128         return this;
129     }
130 
131 
132     /**
133      * Gets the attribute id use in making the comparison.
134      * 
135      * @return the attribute id used in comparison.
136      */
137     public String getAttributeId()
138     {
139         return attrId;
140     }
141 
142 
143     /**
144      * {@inheritDoc}
145      */
146     public CompareRequest setAttributeId( String attributeId )
147     {
148         this.attrId = attributeId;
149 
150         return this;
151     }
152 
153 
154     /**
155      * {@inheritDoc}
156      */
157     public CompareRequest setMessageId( int messageId )
158     {
159         super.setMessageId( messageId );
160 
161         return this;
162     }
163 
164 
165     /**
166      * {@inheritDoc}
167      */
168     public CompareRequest addControl( Control control )
169     {
170         return ( CompareRequest ) super.addControl( control );
171     }
172 
173 
174     /**
175      * {@inheritDoc}
176      */
177     public CompareRequest addAllControls( Control[] controls )
178     {
179         return ( CompareRequest ) super.addAllControls( controls );
180     }
181 
182 
183     /**
184      * {@inheritDoc}
185      */
186     public CompareRequest removeControl( Control control )
187     {
188         return ( CompareRequest ) super.removeControl( control );
189     }
190 
191 
192     // ------------------------------------------------------------------------
193     // SingleReplyRequest Interface Method Implementations
194     // ------------------------------------------------------------------------
195 
196     /**
197      * Gets the protocol response message type for this request which produces
198      * at least one response.
199      * 
200      * @return the message type of the response.
201      */
202     public MessageTypeEnum getResponseType()
203     {
204         return MessageTypeEnum.COMPARE_RESPONSE;
205     }
206 
207 
208     /**
209      * The result containing response for this request.
210      * 
211      * @return the result containing response for this request
212      */
213     public CompareResponse getResultResponse()
214     {
215         if ( response == null )
216         {
217             response = new CompareResponseImpl( getMessageId() );
218         }
219 
220         return response;
221     }
222 
223 
224     /**
225      * {@inheritDoc}
226      */
227     @Override
228     public int hashCode()
229     {
230         int hash = 37;
231         if ( name != null )
232         {
233             hash = hash * 17 + name.hashCode();
234         }
235         if ( attrId != null )
236         {
237             hash = hash * 17 + attrId.hashCode();
238         }
239         if ( attrVal != null )
240         {
241             hash = hash * 17 + attrVal.hashCode();
242         }
243         Value<?> reqVal = getAssertionValue();
244         if ( reqVal != null )
245         {
246             hash = hash * 17 + reqVal.hashCode();
247         }
248         hash = hash * 17 + super.hashCode();
249 
250         return hash;
251     }
252 
253 
254     /**
255      * Checks to see if an object is equivalent to this CompareRequest.
256      * 
257      * @param obj the obj to compare with this CompareRequest
258      * @return true if the obj is equal to this request, false otherwise
259      */
260     public boolean equals( Object obj )
261     {
262         if ( obj == this )
263         {
264             return true;
265         }
266 
267         if ( !super.equals( obj ) )
268         {
269             return false;
270         }
271 
272         CompareRequest req = ( CompareRequest ) obj;
273         Dn reqName = req.getName();
274 
275         if ( ( name != null ) && ( reqName == null ) )
276         {
277             return false;
278         }
279 
280         if ( ( name == null ) && ( reqName != null ) )
281         {
282             return false;
283         }
284 
285         if ( ( name != null ) && ( reqName != null ) && !name.equals( req.getName() ) )
286         {
287             return false;
288         }
289 
290         String reqId = req.getAttributeId();
291 
292         if ( ( attrId != null ) && ( reqId == null ) )
293         {
294             return false;
295         }
296 
297         if ( ( attrId == null ) && ( reqId != null ) )
298         {
299             return false;
300         }
301 
302         if ( ( attrId != null ) && ( reqId != null ) && !attrId.equals( reqId ) )
303         {
304             return false;
305         }
306 
307         Value<?> reqVal = req.getAssertionValue();
308 
309         if ( attrVal != null )
310         {
311             if ( reqVal != null )
312             {
313                 return attrVal.equals( reqVal );
314             }
315             else
316             {
317                 return false;
318             }
319         }
320         else
321         {
322             return reqVal == null;
323         }
324     }
325 
326 
327     /**
328      * Get a String representation of a Compare Request
329      * 
330      * @return A Compare Request String
331      */
332     public String toString()
333     {
334         StringBuilder sb = new StringBuilder();
335 
336         sb.append( "    Compare request\n" );
337         sb.append( "        Entry : '" ).append( name.toString() ).append( "'\n" );
338         sb.append( "        Attribute description : '" ).append( attrId ).append( "'\n" );
339         sb.append( "        Attribute value : '" );
340 
341         if ( attrVal.isHumanReadable() )
342         {
343             sb.append( attrVal.getValue() );
344         }
345         else
346         {
347             byte[] binVal = attrVal.getBytes();
348             sb.append( Strings.utf8ToString( binVal ) ).append( '/' ).append( Strings.dumpBytes( binVal ) )
349                 .append( "'\n" );
350         }
351 
352         // The controls
353         sb.append( super.toString() );
354 
355         return super.toString( sb.toString() );
356     }
357 }