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