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