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.shared.ldap.model.message; 021 022 023import org.apache.directory.shared.ldap.model.entry.Attribute; 024import org.apache.directory.shared.ldap.model.entry.DefaultAttribute; 025import org.apache.directory.shared.ldap.model.entry.DefaultEntry; 026import org.apache.directory.shared.ldap.model.entry.Entry; 027import org.apache.directory.shared.ldap.model.entry.Value; 028import org.apache.directory.shared.ldap.model.exception.LdapException; 029import org.apache.directory.shared.ldap.model.exception.MessageException; 030import org.apache.directory.shared.ldap.model.name.Dn; 031 032 033/** 034 * Lockable add request implementation. 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038public class AddRequestImpl extends AbstractAbandonableRequest implements AddRequest 039{ 040 static final long serialVersionUID = 7534132448349520346L; 041 042 /** A MultiMap of the new entry's attributes and their values */ 043 private Entry entry; 044 045 private AddResponse response; 046 047 /** The current attribute being decoded */ 048 private Attribute currentAttribute; 049 050 051 // ------------------------------------------------------------------------ 052 // Constructors 053 // ------------------------------------------------------------------------ 054 055 056 /** 057 * Creates an AddRequest implementation to create a new entry. 058 */ 059 public AddRequestImpl() 060 { 061 super( -1, TYPE ); 062 entry = new DefaultEntry(); 063 } 064 065 066 /** 067 * Create a new attributeValue 068 * 069 * @param type The attribute's name (called 'type' in the grammar) 070 */ 071 public void addAttributeType( String type ) throws LdapException 072 { 073 // do not create a new attribute if we have seen this attributeType before 074 if ( entry.get( type ) != null ) 075 { 076 currentAttribute = entry.get( type ); 077 return; 078 } 079 080 // fix this to use AttributeImpl(type.getString().toLowerCase()) 081 currentAttribute = new DefaultAttribute( type ); 082 entry.put( currentAttribute ); 083 } 084 085 086 /** 087 * @return Returns the currentAttribute type. 088 */ 089 public String getCurrentAttributeType() 090 { 091 return currentAttribute.getId(); 092 } 093 094 095 /** 096 * Add a new value to the current attribute 097 * 098 * @param value The value to add 099 */ 100 public void addAttributeValue( String value ) throws LdapException 101 { 102 currentAttribute.add( value ); 103 } 104 105 106 /** 107 * Add a new value to the current attribute 108 * 109 * @param value The value to add 110 */ 111 public void addAttributeValue( Value<?> value ) throws LdapException 112 { 113 currentAttribute.add( value ); 114 } 115 116 117 /** 118 * Add a new value to the current attribute 119 * 120 * @param value The value to add 121 */ 122 public void addAttributeValue( byte[] value ) throws LdapException 123 { 124 currentAttribute.add( value ); 125 } 126 127 128 // ------------------------------------------------------------------------ 129 // AddRequest Interface Method Implementations 130 // ------------------------------------------------------------------------ 131 132 133 /** 134 * Gets the distinguished name of the entry to add. 135 * 136 * @return the Dn of the added entry. 137 */ 138 public Dn getEntryDn() 139 { 140 return entry.getDn(); 141 } 142 143 144 /** 145 * {@inheritDoc} 146 */ 147 public AddRequest setEntryDn( Dn dn ) 148 { 149 entry.setDn( dn ); 150 151 return this; 152 } 153 154 155 /** 156 * {@inheritDoc} 157 */ 158 public Entry getEntry() 159 { 160 return entry; 161 } 162 163 164 /** 165 * {@inheritDoc} 166 */ 167 public AddRequest setEntry( Entry entry ) 168 { 169 this.entry = entry; 170 171 return this; 172 } 173 174 175 /** 176 * {@inheritDoc} 177 */ 178 public AddRequest setMessageId( int messageId ) 179 { 180 super.setMessageId( messageId ); 181 182 return this; 183 } 184 185 186 /** 187 * {@inheritDoc} 188 */ 189 public AddRequest addControl( Control control ) throws MessageException 190 { 191 return (AddRequest)super.addControl( control ); 192 } 193 194 195 /** 196 * {@inheritDoc} 197 */ 198 public AddRequest addAllControls( Control[] controls ) throws MessageException 199 { 200 return (AddRequest)super.addAllControls( controls ); 201 } 202 203 204 /** 205 * {@inheritDoc} 206 */ 207 public AddRequest removeControl( Control control ) throws MessageException 208 { 209 return (AddRequest)super.removeControl( control ); 210 } 211 212 213 // ------------------------------------------------------------------------ 214 // SingleReplyRequest Interface Method Implementations 215 // ------------------------------------------------------------------------ 216 217 /** 218 * Gets the protocol response message type for this request which produces 219 * at least one response. 220 * 221 * @return the message type of the response. 222 */ 223 public MessageTypeEnum getResponseType() 224 { 225 return RESP_TYPE; 226 } 227 228 229 /** 230 * The result containing response for this request. 231 * 232 * @return the result containing response for this request 233 */ 234 public AddResponse getResultResponse() 235 { 236 if ( response == null ) 237 { 238 response = new AddResponseImpl( getMessageId() ); 239 } 240 241 return response; 242 } 243 244 245 /** 246 * Checks to see if an object is equivalent to this AddRequest. First 247 * there's a quick test to see if the obj is the same object as this one - 248 * if so true is returned. Next if the super method fails false is returned. 249 * Then the name of the entry is compared - if not the same false is 250 * returned. Lastly the attributes of the entry are compared. If they are 251 * not the same false is returned otherwise the method exists returning 252 * true. 253 * 254 * @param obj the object to test for equality to this 255 * @return true if the obj is equal to this AddRequest, false otherwise 256 */ 257 public boolean equals( Object obj ) 258 { 259 // Short circuit 260 if ( this == obj ) 261 { 262 return true; 263 } 264 265 // Check the object class. If null, it will exit. 266 if ( !( obj instanceof AddRequest ) ) 267 { 268 return false; 269 } 270 271 if ( !super.equals( obj ) ) 272 { 273 return false; 274 } 275 276 AddRequest req = ( AddRequest ) obj; 277 278 // Check the entry 279 if ( entry == null ) 280 { 281 return ( req.getEntry() == null ); 282 } 283 else 284 { 285 return ( entry.equals( req.getEntry() ) ); 286 } 287 } 288 289 290 /** 291 * @see Object#hashCode() 292 * @return the instance's hash code 293 */ 294 public int hashCode() 295 { 296 int hash = 37; 297 hash = hash * 17 + ( entry == null ? 0 : entry.hashCode() ); 298 hash = hash * 17 + ( response == null ? 0 : response.hashCode() ); 299 hash = hash * 17 + super.hashCode(); 300 301 return hash; 302 } 303 304 305 /** 306 * @see Object#toString() 307 */ 308 public String toString() 309 { 310 StringBuilder sb = new StringBuilder(); 311 312 sb.append( " Add Request :\n" ); 313 314 if ( entry == null ) 315 { 316 sb.append( " No entry\n" ); 317 } 318 else 319 { 320 sb.append( entry.toString() ); 321 } 322 323 return super.toString( sb.toString() ); 324 } 325}