001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.message;
021
022
023import org.apache.directory.api.ldap.model.entry.Value;
024import org.apache.directory.api.ldap.model.name.Dn;
025import org.apache.directory.api.util.Strings;
026
027
028/**
029 * Comparison request implementation.
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class CompareRequestImpl extends AbstractAbandonableRequest implements CompareRequest
034{
035    static final long serialVersionUID = 1699731530016468977L;
036
037    /** Distinguished name identifying the compared entry */
038    private Dn name;
039
040    /** The id of the attribute used in the comparison */
041    private String attrId;
042
043    /** The value of the attribute used in the comparison */
044    private Value attrVal;
045
046    /** The associated response */
047    private CompareResponse response;
048
049
050    // ------------------------------------------------------------------------
051    // Constructors
052    // ------------------------------------------------------------------------
053    /**
054     * Creates an CompareRequest implementation to compare a named entry with an
055     * attribute value assertion pair.
056     */
057    public CompareRequestImpl()
058    {
059        super( -1, MessageTypeEnum.COMPARE_REQUEST );
060    }
061
062
063    // ------------------------------------------------------------------------
064    // ComparisonRequest Interface Method Implementations
065    // ------------------------------------------------------------------------
066
067    /**
068     * Gets the distinguished name of the entry to be compared using the
069     * attribute value assertion.
070     * 
071     * @return the Dn of the compared entry.
072     */
073    @Override
074    public Dn getName()
075    {
076        return name;
077    }
078
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public CompareRequest setName( Dn name )
085    {
086        this.name = name;
087
088        return this;
089    }
090
091
092    /**
093     * Gets the attribute value to use in making the comparison.
094     * 
095     * @return the attribute value to used in comparison.
096     */
097    @Override
098public Value getAssertionValue()
099    {
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}