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