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.BinaryValue; 024import org.apache.directory.api.ldap.model.entry.StringValue; 025import org.apache.directory.api.ldap.model.entry.Value; 026import org.apache.directory.api.ldap.model.name.Dn; 027import org.apache.directory.api.util.Strings; 028 029 030/** 031 * Comparison request implementation. 032 * 033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 034 */ 035public class CompareRequestImpl extends AbstractAbandonableRequest implements CompareRequest 036{ 037 static final long serialVersionUID = 1699731530016468977L; 038 039 /** Distinguished name identifying the compared entry */ 040 private Dn name; 041 042 /** The id of the attribute used in the comparison */ 043 private String attrId; 044 045 /** The value of the attribute used in the comparison */ 046 private Value<?> attrVal; 047 048 /** The associated response */ 049 private CompareResponse response; 050 051 052 // ------------------------------------------------------------------------ 053 // Constructors 054 // ------------------------------------------------------------------------ 055 /** 056 * Creates an CompareRequest implementation to compare a named entry with an 057 * attribute value assertion pair. 058 */ 059 public CompareRequestImpl() 060 { 061 super( -1, MessageTypeEnum.COMPARE_REQUEST ); 062 } 063 064 065 // ------------------------------------------------------------------------ 066 // ComparisonRequest Interface Method Implementations 067 // ------------------------------------------------------------------------ 068 069 /** 070 * Gets the distinguished name of the entry to be compared using the 071 * attribute value assertion. 072 * 073 * @return the Dn of the compared entry. 074 */ 075 public Dn getName() 076 { 077 return name; 078 } 079 080 081 /** 082 * {@inheritDoc} 083 */ 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 public Value<?> getAssertionValue() 098 { 099 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}