001    /*
002     * Copyright 2009-2012 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2009-2012 UnboundID Corp.
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     */
021    package com.unboundid.ldap.protocol;
022    
023    
024    
025    import java.util.ArrayList;
026    import java.util.Collections;
027    import java.util.Iterator;
028    import java.util.List;
029    
030    import com.unboundid.asn1.ASN1Buffer;
031    import com.unboundid.asn1.ASN1BufferSequence;
032    import com.unboundid.asn1.ASN1StreamReader;
033    import com.unboundid.asn1.ASN1StreamReaderSequence;
034    import com.unboundid.ldap.sdk.Attribute;
035    import com.unboundid.ldap.sdk.LDAPException;
036    import com.unboundid.ldap.sdk.ResultCode;
037    import com.unboundid.util.NotMutable;
038    import com.unboundid.util.InternalUseOnly;
039    import com.unboundid.util.ThreadSafety;
040    import com.unboundid.util.ThreadSafetyLevel;
041    
042    import static com.unboundid.ldap.protocol.ProtocolMessages.*;
043    import static com.unboundid.util.Debug.*;
044    import static com.unboundid.util.StaticUtils.*;
045    import static com.unboundid.util.Validator.*;
046    
047    
048    
049    /**
050     * This class provides an implementation of an LDAP add request protocol op.
051     */
052    @InternalUseOnly()
053    @NotMutable()
054    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
055    public final class AddRequestProtocolOp
056           implements ProtocolOp
057    {
058      /**
059       * The serial version UID for this serializable class.
060       */
061      private static final long serialVersionUID = -1195296296055518601L;
062    
063    
064    
065      // The list of attributes for this add request.
066      private final List<Attribute> attributes;
067    
068      // The entry DN for this add request.
069      private final String dn;
070    
071    
072    
073      /**
074       * Creates a new add request protocol op with the provided information.
075       *
076       * @param  dn          The entry DN for this add request.
077       * @param  attributes  The list of attributes to include in this add request.
078       */
079      public AddRequestProtocolOp(final String dn, final List<Attribute> attributes)
080      {
081        this.dn         = dn;
082        this.attributes = Collections.unmodifiableList(attributes);
083      }
084    
085    
086    
087      /**
088       * Creates a new add request protocol op read from the provided ASN.1 stream
089       * reader.
090       *
091       * @param  reader  The ASN.1 stream reader from which to read the add request
092       *                 protocol op.
093       *
094       * @throws  LDAPException  If a problem occurs while reading or parsing the
095       *                         add request.
096       */
097      AddRequestProtocolOp(final ASN1StreamReader reader)
098           throws LDAPException
099      {
100        try
101        {
102          reader.beginSequence();
103          dn = reader.readString();
104          ensureNotNull(dn);
105    
106          final ArrayList<Attribute> attrs = new ArrayList<Attribute>(10);
107          final ASN1StreamReaderSequence attrSequence = reader.beginSequence();
108          while (attrSequence.hasMoreElements())
109          {
110            attrs.add(Attribute.readFrom(reader));
111          }
112    
113          attributes = Collections.unmodifiableList(attrs);
114        }
115        catch (LDAPException le)
116        {
117          debugException(le);
118          throw le;
119        }
120        catch (Exception e)
121        {
122          debugException(e);
123    
124          throw new LDAPException(ResultCode.DECODING_ERROR,
125               ERR_ADD_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), e);
126        }
127      }
128    
129    
130    
131      /**
132       * Retrieves the target entry DN for this add request.
133       *
134       * @return  The target entry DN for this add request.
135       */
136      public String getDN()
137      {
138        return dn;
139      }
140    
141    
142    
143      /**
144       * Retrieves the list of attributes for this add request.
145       *
146       * @return  The list of attributes for this add request.
147       */
148      public List<Attribute> getAttributes()
149      {
150        return attributes;
151      }
152    
153    
154    
155      /**
156       * {@inheritDoc}
157       */
158      public byte getProtocolOpType()
159      {
160        return LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST;
161      }
162    
163    
164    
165      /**
166       * {@inheritDoc}
167       */
168      public void writeTo(final ASN1Buffer buffer)
169      {
170        final ASN1BufferSequence opSequence =
171             buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST);
172        buffer.addOctetString(dn);
173    
174        final ASN1BufferSequence attrSequence = buffer.beginSequence();
175        for (final Attribute a : attributes)
176        {
177          a.writeTo(buffer);
178        }
179        attrSequence.end();
180        opSequence.end();
181      }
182    
183    
184    
185      /**
186       * Retrieves a string representation of this protocol op.
187       *
188       * @return  A string representation of this protocol op.
189       */
190      @Override()
191      public String toString()
192      {
193        final StringBuilder buffer = new StringBuilder();
194        toString(buffer);
195        return buffer.toString();
196      }
197    
198    
199    
200      /**
201       * {@inheritDoc}
202       */
203      public void toString(final StringBuilder buffer)
204      {
205        buffer.append("AddRequestProtocolOp(dn='");
206        buffer.append(dn);
207        buffer.append("', attrs={");
208    
209        final Iterator<Attribute> iterator = attributes.iterator();
210        while (iterator.hasNext())
211        {
212          iterator.next().toString(buffer);
213          if (iterator.hasNext())
214          {
215            buffer.append(',');
216          }
217        }
218    
219        buffer.append("})");
220      }
221    }