001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 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-2024 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.NotNull;
061import com.unboundid.util.Nullable;
062import com.unboundid.util.StaticUtils;
063import com.unboundid.util.ThreadSafety;
064import com.unboundid.util.ThreadSafetyLevel;
065import com.unboundid.util.Validator;
066
067import static com.unboundid.ldap.protocol.ProtocolMessages.*;
068
069
070
071/**
072 * This class provides an implementation of an LDAP add request protocol op.
073 */
074@InternalUseOnly()
075@NotMutable()
076@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
077public final class AddRequestProtocolOp
078       implements ProtocolOp
079{
080  /**
081   * The serial version UID for this serializable class.
082   */
083  private static final long serialVersionUID = -1195296296055518601L;
084
085
086
087  // The list of attributes for this add request.
088  @NotNull private final List<Attribute> attributes;
089
090  // The entry DN for this add request.
091  @NotNull private final String dn;
092
093
094
095  /**
096   * Creates a new add request protocol op with the provided information.
097   *
098   * @param  dn          The entry DN for this add request.
099   * @param  attributes  The list of attributes to include in this add request.
100   */
101  public AddRequestProtocolOp(@NotNull final String dn,
102                              @NotNull final List<Attribute> attributes)
103  {
104    this.dn         = dn;
105    this.attributes = Collections.unmodifiableList(attributes);
106  }
107
108
109
110  /**
111   * Creates a new add request protocol op from the provided add request object.
112   *
113   * @param  request  The add request object to use to create this protocol op.
114   */
115  public AddRequestProtocolOp(@NotNull final AddRequest request)
116  {
117    dn          = request.getDN();
118    attributes = request.getAttributes();
119  }
120
121
122
123  /**
124   * Creates a new add request protocol op read from the provided ASN.1 stream
125   * reader.
126   *
127   * @param  reader  The ASN.1 stream reader from which to read the add request
128   *                 protocol op.
129   *
130   * @throws  LDAPException  If a problem occurs while reading or parsing the
131   *                         add request.
132   */
133  AddRequestProtocolOp(@NotNull final ASN1StreamReader reader)
134       throws LDAPException
135  {
136    try
137    {
138      reader.beginSequence();
139      dn = reader.readString();
140      Validator.ensureNotNull(dn);
141
142      final ArrayList<Attribute> attrs = new ArrayList<>(10);
143      final ASN1StreamReaderSequence attrSequence = reader.beginSequence();
144      while (attrSequence.hasMoreElements())
145      {
146        attrs.add(Attribute.readFrom(reader));
147      }
148
149      attributes = Collections.unmodifiableList(attrs);
150    }
151    catch (final LDAPException le)
152    {
153      Debug.debugException(le);
154      throw le;
155    }
156    catch (final Exception e)
157    {
158      Debug.debugException(e);
159
160      throw new LDAPException(ResultCode.DECODING_ERROR,
161           ERR_ADD_REQUEST_CANNOT_DECODE.get(
162                StaticUtils.getExceptionMessage(e)),
163           e);
164    }
165  }
166
167
168
169  /**
170   * Retrieves the target entry DN for this add request.
171   *
172   * @return  The target entry DN for this add request.
173
174   */
175  @NotNull()
176  public String getDN()
177  {
178    return dn;
179  }
180
181
182
183  /**
184   * Retrieves the list of attributes for this add request.
185   *
186   * @return  The list of attributes for this add request.
187   */
188  @NotNull()
189  public List<Attribute> getAttributes()
190  {
191    return attributes;
192  }
193
194
195
196  /**
197   * {@inheritDoc}
198   */
199  @Override()
200  public byte getProtocolOpType()
201  {
202    return LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST;
203  }
204
205
206
207  /**
208   * {@inheritDoc}
209   */
210  @Override()
211  @NotNull()
212  public ASN1Element encodeProtocolOp()
213  {
214    final ArrayList<ASN1Element> attrElements =
215         new ArrayList<>(attributes.size());
216    for (final Attribute a : attributes)
217    {
218      attrElements.add(a.encode());
219    }
220
221    return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST,
222         new ASN1OctetString(dn),
223         new ASN1Sequence(attrElements));
224  }
225
226
227
228  /**
229   * Decodes the provided ASN.1 element as an add request protocol op.
230   *
231   * @param  element  The ASN.1 element to be decoded.
232   *
233   * @return  The decoded add request protocol op.
234   *
235   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
236   *                         an add request protocol op.
237   */
238  @NotNull()
239  public static AddRequestProtocolOp decodeProtocolOp(
240                                          @NotNull final ASN1Element element)
241         throws LDAPException
242  {
243    try
244    {
245      final ASN1Element[] elements =
246           ASN1Sequence.decodeAsSequence(element).elements();
247      final String dn =
248           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
249
250      final ASN1Element[] attrElements =
251           ASN1Sequence.decodeAsSequence(elements[1]).elements();
252      final ArrayList<Attribute> attributes =
253           new ArrayList<>(attrElements.length);
254      for (final ASN1Element ae : attrElements)
255      {
256        attributes.add(Attribute.decode(ASN1Sequence.decodeAsSequence(ae)));
257      }
258
259      return new AddRequestProtocolOp(dn, attributes);
260    }
261    catch (final Exception e)
262    {
263      Debug.debugException(e);
264      throw new LDAPException(ResultCode.DECODING_ERROR,
265           ERR_ADD_REQUEST_CANNOT_DECODE.get(
266                StaticUtils.getExceptionMessage(e)),
267           e);
268    }
269  }
270
271
272
273  /**
274   * {@inheritDoc}
275   */
276  @Override()
277  public void writeTo(@NotNull final ASN1Buffer buffer)
278  {
279    final ASN1BufferSequence opSequence =
280         buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST);
281    buffer.addOctetString(dn);
282
283    final ASN1BufferSequence attrSequence = buffer.beginSequence();
284    for (final Attribute a : attributes)
285    {
286      a.writeTo(buffer);
287    }
288    attrSequence.end();
289    opSequence.end();
290  }
291
292
293
294  /**
295   * Creates an add request from this protocol op.
296   *
297   * @param  controls  The set of controls to include in the add request.  It
298   *                   may be empty or {@code null} if no controls should be
299   *                   included.
300   *
301   * @return  The add request that was created.
302   */
303  @NotNull()
304  public AddRequest toAddRequest(@Nullable final Control... controls)
305  {
306    return new AddRequest(dn, attributes, controls);
307  }
308
309
310
311  /**
312   * Retrieves a string representation of this protocol op.
313   *
314   * @return  A string representation of this protocol op.
315   */
316  @Override()
317  @NotNull()
318  public String toString()
319  {
320    final StringBuilder buffer = new StringBuilder();
321    toString(buffer);
322    return buffer.toString();
323  }
324
325
326
327  /**
328   * {@inheritDoc}
329   */
330  @Override()
331  public void toString(@NotNull final StringBuilder buffer)
332  {
333    buffer.append("AddRequestProtocolOp(dn='");
334    buffer.append(dn);
335    buffer.append("', attrs={");
336
337    final Iterator<Attribute> iterator = attributes.iterator();
338    while (iterator.hasNext())
339    {
340      iterator.next().toString(buffer);
341      if (iterator.hasNext())
342      {
343        buffer.append(',');
344      }
345    }
346
347    buffer.append("})");
348  }
349}