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     */
020    package org.apache.directory.shared.ldap.message;
021    
022    
023    import org.apache.directory.shared.ldap.entry.Value;
024    import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
025    import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
026    import org.apache.directory.shared.ldap.name.LdapDN;
027    import org.apache.directory.shared.ldap.util.StringTools;
028    
029    
030    /**
031     * Comparison request implementation.
032     * 
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev: 798550 $
035     */
036    public class CompareRequestImpl extends AbstractAbandonableRequest implements InternalCompareRequest
037    {
038        static final long serialVersionUID = 1699731530016468977L;
039    
040        /** Distinguished name identifying the compared entry */
041        private LdapDN name;
042    
043        /** The id of the attribute used in the comparison */
044        private String attrId;
045    
046        /** The value of the attribute used in the comparison */
047        private Value<?> attrVal;
048    
049        private InternalCompareResponse response;
050    
051    
052        // ------------------------------------------------------------------------
053        // Constructors
054        // ------------------------------------------------------------------------
055    
056        /**
057         * Creates an CompareRequest implementation to compare a named entry with an
058         * attribute value assertion pair.
059         * 
060         * @param id
061         *            the sequence identifier of the CompareRequest message.
062         */
063        public CompareRequestImpl( final int id )
064        {
065            super( id, TYPE );
066        }
067    
068    
069        // ------------------------------------------------------------------------
070        // ComparisonRequest Interface Method Implementations
071        // ------------------------------------------------------------------------
072    
073        /**
074         * Gets the distinguished name of the entry to be compared using the
075         * attribute value assertion.
076         * 
077         * @return the DN of the compared entry.
078         */
079        public LdapDN getName()
080        {
081            return name;
082        }
083    
084    
085        /**
086         * Sets the distinguished name of the entry to be compared using the
087         * attribute value assertion.
088         * 
089         * @param name
090         *            the DN of the compared entry.
091         */
092        public void setName( LdapDN name )
093        {
094            this.name = name;
095        }
096    
097    
098        /**
099         * Gets the attribute value to use in making the comparison.
100         * 
101         * @return the attribute value to used in comparison.
102         */
103        public Value<?> getAssertionValue()
104        {
105            return attrVal;
106        }
107    
108    
109        /**
110         * Sets the attribute value to use in the comparison.
111         * 
112         * @param attrVal
113         *            the attribute value used in comparison.
114         */
115        public void setAssertionValue( String attrVal )
116        {
117            this.attrVal = new ClientStringValue( attrVal );
118        }
119    
120    
121        /**
122         * Sets the attribute value to use in the comparison.
123         * 
124         * @param attrVal
125         *            the attribute value used in comparison.
126         */
127        public void setAssertionValue( byte[] attrVal )
128        {
129            if ( attrVal != null )
130            {
131                this.attrVal = new ClientBinaryValue( attrVal );
132            }
133            else
134            {
135                this.attrVal = null;
136            }
137        }
138    
139    
140        /**
141         * Gets the attribute id use in making the comparison.
142         * 
143         * @return the attribute id used in comparison.
144         */
145        public String getAttributeId()
146        {
147            return attrId;
148        }
149    
150    
151        /**
152         * Sets the attribute id used in the comparison.
153         * 
154         * @param attrId
155         *            the attribute id used in comparison.
156         */
157        public void setAttributeId( String attrId )
158        {
159            this.attrId = attrId;
160        }
161    
162    
163        // ------------------------------------------------------------------------
164        // SingleReplyRequest Interface Method Implementations
165        // ------------------------------------------------------------------------
166    
167        /**
168         * Gets the protocol response message type for this request which produces
169         * at least one response.
170         * 
171         * @return the message type of the response.
172         */
173        public MessageTypeEnum getResponseType()
174        {
175            return RESP_TYPE;
176        }
177    
178    
179        /**
180         * The result containing response for this request.
181         * 
182         * @return the result containing response for this request
183         */
184        public InternalResultResponse getResultResponse()
185        {
186            if ( response == null )
187            {
188                response = new CompareResponseImpl( getMessageId() );
189            }
190    
191            return response;
192        }
193    
194    
195        /**
196         * Checks to see if an object is equivalent to this CompareRequest.
197         * 
198         * @param obj the obj to compare with this CompareRequest
199         * @return true if the obj is equal to this request, false otherwise
200         */
201        public boolean equals( Object obj )
202        {
203            if ( obj == this )
204            {
205                return true;
206            }
207    
208            if ( !super.equals( obj ) )
209            {
210                return false;
211            }
212    
213            InternalCompareRequest req = ( InternalCompareRequest ) obj;
214            LdapDN reqName = req.getName();
215    
216            if ( ( name != null ) && ( reqName == null ) )
217            {
218                return false;
219            }
220    
221            if ( ( name == null ) && ( reqName != null ) )
222            {
223                return false;
224            }
225    
226            if ( ( name != null ) && ( reqName != null ) )
227            {
228                if ( !name.equals( req.getName() ) )
229                {
230                    return false;
231                }
232            }
233    
234            String reqId = req.getAttributeId();
235    
236            if ( ( attrId != null ) && ( reqId == null ) )
237            {
238                return false;
239            }
240    
241            if ( ( attrId == null ) && ( reqId != null ) )
242            {
243                return false;
244            }
245    
246            if ( ( attrId != null ) && ( reqId != null ) )
247            {
248                if ( !attrId.equals( reqId ) )
249                {
250                    return false;
251                }
252            }
253    
254            Value<?> reqVal = req.getAssertionValue();
255    
256            if ( attrVal != null )
257            {
258                if ( reqVal != null )
259                {
260                    return attrVal.equals( reqVal );
261                }
262                else
263                {
264                    return false;
265                }
266            }
267            else
268            {
269                return reqVal == null;
270            }
271        }
272    
273    
274        /**
275         * Get a String representation of a Compare Request
276         * 
277         * @return A Compare Request String
278         */
279        public String toString()
280        {
281            StringBuilder sb = new StringBuilder();
282    
283            sb.append( "    Compare request\n" );
284            sb.append( "        Entry : '" ).append( name.toString() ).append( "'\n" );
285            sb.append( "        Attribute description : '" ).append( attrId ).append( "'\n" );
286            sb.append( "        Attribute value : '" );
287            
288            if ( !attrVal.isBinary() )
289            {
290                sb.append( attrVal.get() );
291            }
292            else
293            {
294                byte[] binVal = attrVal.getBytes();
295                sb.append( StringTools.utf8ToString( binVal ) ).append( '/' ).append(
296                    StringTools.dumpBytes( binVal ) ).append( "'\n" );
297            }
298    
299            return sb.toString();
300        }
301    }