001/* 002 * Copyright 2009-2018 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2009-2018 Ping Identity Corporation 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.ldap.protocol; 022 023 024 025import java.util.ArrayList; 026import java.util.Collections; 027import java.util.Iterator; 028import java.util.List; 029 030import com.unboundid.asn1.ASN1Buffer; 031import com.unboundid.asn1.ASN1BufferSequence; 032import com.unboundid.asn1.ASN1Element; 033import com.unboundid.asn1.ASN1OctetString; 034import com.unboundid.asn1.ASN1Sequence; 035import com.unboundid.asn1.ASN1StreamReader; 036import com.unboundid.asn1.ASN1StreamReaderSequence; 037import com.unboundid.ldap.sdk.AddRequest; 038import com.unboundid.ldap.sdk.Attribute; 039import com.unboundid.ldap.sdk.Control; 040import com.unboundid.ldap.sdk.LDAPException; 041import com.unboundid.ldap.sdk.ResultCode; 042import com.unboundid.util.NotMutable; 043import com.unboundid.util.InternalUseOnly; 044import com.unboundid.util.ThreadSafety; 045import com.unboundid.util.ThreadSafetyLevel; 046 047import static com.unboundid.ldap.protocol.ProtocolMessages.*; 048import static com.unboundid.util.Debug.*; 049import static com.unboundid.util.StaticUtils.*; 050import static com.unboundid.util.Validator.*; 051 052 053 054/** 055 * This class provides an implementation of an LDAP add request protocol op. 056 */ 057@InternalUseOnly() 058@NotMutable() 059@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 060public final class AddRequestProtocolOp 061 implements ProtocolOp 062{ 063 /** 064 * The serial version UID for this serializable class. 065 */ 066 private static final long serialVersionUID = -1195296296055518601L; 067 068 069 070 // The list of attributes for this add request. 071 private final List<Attribute> attributes; 072 073 // The entry DN for this add request. 074 private final String dn; 075 076 077 078 /** 079 * Creates a new add request protocol op with the provided information. 080 * 081 * @param dn The entry DN for this add request. 082 * @param attributes The list of attributes to include in this add request. 083 */ 084 public AddRequestProtocolOp(final String dn, final List<Attribute> attributes) 085 { 086 this.dn = dn; 087 this.attributes = Collections.unmodifiableList(attributes); 088 } 089 090 091 092 /** 093 * Creates a new add request protocol op from the provided add request object. 094 * 095 * @param request The add request object to use to create this protocol op. 096 */ 097 public AddRequestProtocolOp(final AddRequest request) 098 { 099 dn = request.getDN(); 100 attributes = request.getAttributes(); 101 } 102 103 104 105 /** 106 * Creates a new add request protocol op read from the provided ASN.1 stream 107 * reader. 108 * 109 * @param reader The ASN.1 stream reader from which to read the add request 110 * protocol op. 111 * 112 * @throws LDAPException If a problem occurs while reading or parsing the 113 * add request. 114 */ 115 AddRequestProtocolOp(final ASN1StreamReader reader) 116 throws LDAPException 117 { 118 try 119 { 120 reader.beginSequence(); 121 dn = reader.readString(); 122 ensureNotNull(dn); 123 124 final ArrayList<Attribute> attrs = new ArrayList<Attribute>(10); 125 final ASN1StreamReaderSequence attrSequence = reader.beginSequence(); 126 while (attrSequence.hasMoreElements()) 127 { 128 attrs.add(Attribute.readFrom(reader)); 129 } 130 131 attributes = Collections.unmodifiableList(attrs); 132 } 133 catch (final LDAPException le) 134 { 135 debugException(le); 136 throw le; 137 } 138 catch (final Exception e) 139 { 140 debugException(e); 141 142 throw new LDAPException(ResultCode.DECODING_ERROR, 143 ERR_ADD_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), e); 144 } 145 } 146 147 148 149 /** 150 * Retrieves the target entry DN for this add request. 151 * 152 * @return The target entry DN for this add request. 153 */ 154 public String getDN() 155 { 156 return dn; 157 } 158 159 160 161 /** 162 * Retrieves the list of attributes for this add request. 163 * 164 * @return The list of attributes for this add request. 165 */ 166 public List<Attribute> getAttributes() 167 { 168 return attributes; 169 } 170 171 172 173 /** 174 * {@inheritDoc} 175 */ 176 @Override() 177 public byte getProtocolOpType() 178 { 179 return LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST; 180 } 181 182 183 184 /** 185 * {@inheritDoc} 186 */ 187 @Override() 188 public ASN1Element encodeProtocolOp() 189 { 190 final ArrayList<ASN1Element> attrElements = 191 new ArrayList<ASN1Element>(attributes.size()); 192 for (final Attribute a : attributes) 193 { 194 attrElements.add(a.encode()); 195 } 196 197 return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST, 198 new ASN1OctetString(dn), 199 new ASN1Sequence(attrElements)); 200 } 201 202 203 204 /** 205 * Decodes the provided ASN.1 element as an add request protocol op. 206 * 207 * @param element The ASN.1 element to be decoded. 208 * 209 * @return The decoded add request protocol op. 210 * 211 * @throws LDAPException If the provided ASN.1 element cannot be decoded as 212 * an add request protocol op. 213 */ 214 public static AddRequestProtocolOp decodeProtocolOp(final ASN1Element element) 215 throws LDAPException 216 { 217 try 218 { 219 final ASN1Element[] elements = 220 ASN1Sequence.decodeAsSequence(element).elements(); 221 final String dn = 222 ASN1OctetString.decodeAsOctetString(elements[0]).stringValue(); 223 224 final ASN1Element[] attrElements = 225 ASN1Sequence.decodeAsSequence(elements[1]).elements(); 226 final ArrayList<Attribute> attributes = 227 new ArrayList<Attribute>(attrElements.length); 228 for (final ASN1Element ae : attrElements) 229 { 230 attributes.add(Attribute.decode(ASN1Sequence.decodeAsSequence(ae))); 231 } 232 233 return new AddRequestProtocolOp(dn, attributes); 234 } 235 catch (final Exception e) 236 { 237 debugException(e); 238 throw new LDAPException(ResultCode.DECODING_ERROR, 239 ERR_ADD_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), 240 e); 241 } 242 } 243 244 245 246 /** 247 * {@inheritDoc} 248 */ 249 @Override() 250 public void writeTo(final ASN1Buffer buffer) 251 { 252 final ASN1BufferSequence opSequence = 253 buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST); 254 buffer.addOctetString(dn); 255 256 final ASN1BufferSequence attrSequence = buffer.beginSequence(); 257 for (final Attribute a : attributes) 258 { 259 a.writeTo(buffer); 260 } 261 attrSequence.end(); 262 opSequence.end(); 263 } 264 265 266 267 /** 268 * Creates an add request from this protocol op. 269 * 270 * @param controls The set of controls to include in the add request. It 271 * may be empty or {@code null} if no controls should be 272 * included. 273 * 274 * @return The add request that was created. 275 */ 276 public AddRequest toAddRequest(final Control... controls) 277 { 278 return new AddRequest(dn, attributes, controls); 279 } 280 281 282 283 /** 284 * Retrieves a string representation of this protocol op. 285 * 286 * @return A string representation of this protocol op. 287 */ 288 @Override() 289 public String toString() 290 { 291 final StringBuilder buffer = new StringBuilder(); 292 toString(buffer); 293 return buffer.toString(); 294 } 295 296 297 298 /** 299 * {@inheritDoc} 300 */ 301 @Override() 302 public void toString(final StringBuilder buffer) 303 { 304 buffer.append("AddRequestProtocolOp(dn='"); 305 buffer.append(dn); 306 buffer.append("', attrs={"); 307 308 final Iterator<Attribute> iterator = attributes.iterator(); 309 while (iterator.hasNext()) 310 { 311 iterator.next().toString(buffer); 312 if (iterator.hasNext()) 313 { 314 buffer.append(','); 315 } 316 } 317 318 buffer.append("})"); 319 } 320}