001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS
026 */
027package org.opends.server.protocols.ldap;
028
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.Iterator;
032import java.util.List;
033
034import org.forgerock.i18n.slf4j.LocalizedLogger;
035import org.forgerock.opendj.io.ASN1Writer;
036import org.forgerock.opendj.ldap.ByteString;
037import org.opends.server.types.RawModification;
038
039import static org.opends.server.protocols.ldap.LDAPConstants.*;
040import static org.opends.server.util.ServerConstants.*;
041
042/**
043 * This class defines the structures and methods for an LDAP modify request
044 * protocol op, which is used to alter the contents of an entry in the Directory
045 * Server.
046 */
047public class ModifyRequestProtocolOp
048       extends ProtocolOp
049{
050  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
051
052  /** The set of modifications for this modify request. */
053  private List<RawModification> modifications;
054
055  /** The DN for this modify request. */
056  private ByteString dn;
057
058
059
060  /**
061   * Creates a new LDAP modify request protocol op with the specified DN and no
062   * modifications.
063   *
064   * @param  dn  The DN for this modify request.
065   */
066  public ModifyRequestProtocolOp(ByteString dn)
067  {
068    this.dn            = dn;
069    this.modifications = new ArrayList<>();
070  }
071
072
073
074  /**
075   * Creates a new LDAP modify request protocol op with the specified DN and set
076   * of modifications.
077   *
078   * @param  dn             The DN for this modify request.
079   * @param  modifications  The set of modifications for this modify request.
080   */
081  public ModifyRequestProtocolOp(ByteString dn,
082      List<RawModification> modifications)
083  {
084    this.dn = dn;
085
086    if (modifications == null)
087    {
088      this.modifications = new ArrayList<>();
089    }
090    else
091    {
092      this.modifications = modifications;
093    }
094  }
095
096
097
098  /**
099   * Retrieves the DN for this modify request.
100   *
101   * @return  The DN for this modify request.
102   */
103  public ByteString getDN()
104  {
105    return dn;
106  }
107
108
109
110  /**
111   * Retrieves the set of modifications for this modify request.  The returned
112   * list may be altered by the caller.
113   *
114   * @return  The set of modifications for this modify request.
115   */
116  public List<RawModification> getModifications()
117  {
118    return modifications;
119  }
120
121
122
123  /**
124   * Retrieves the BER type for this protocol op.
125   *
126   * @return  The BER type for this protocol op.
127   */
128  @Override
129  public byte getType()
130  {
131    return OP_TYPE_MODIFY_REQUEST;
132  }
133
134
135
136  /**
137   * Retrieves the name for this protocol op type.
138   *
139   * @return  The name for this protocol op type.
140   */
141  @Override
142  public String getProtocolOpName()
143  {
144    return "Modify Request";
145  }
146
147  /**
148   * Writes this protocol op to an ASN.1 output stream.
149   *
150   * @param stream The ASN.1 output stream to write to.
151   * @throws IOException If a problem occurs while writing to the stream.
152   */
153  @Override
154  public void write(ASN1Writer stream) throws IOException
155  {
156    stream.writeStartSequence(OP_TYPE_MODIFY_REQUEST);
157    stream.writeOctetString(dn);
158
159    stream.writeStartSequence();
160    for(RawModification mod : modifications)
161    {
162      mod.write(stream);
163    }
164    stream.writeEndSequence();
165
166    stream.writeEndSequence();
167  }
168
169
170
171  /**
172   * Appends a string representation of this LDAP protocol op to the provided
173   * buffer.
174   *
175   * @param  buffer  The buffer to which the string should be appended.
176   */
177  @Override
178  public void toString(StringBuilder buffer)
179  {
180    buffer.append("ModifyRequest(dn=").append(dn);
181    buffer.append(", mods={");
182
183    if (! modifications.isEmpty())
184    {
185      Iterator<RawModification> iterator = modifications.iterator();
186      iterator.next().toString(buffer);
187
188      while (iterator.hasNext())
189      {
190        buffer.append(", ");
191        iterator.next().toString(buffer);
192      }
193    }
194
195    buffer.append("})");
196  }
197
198
199
200  /**
201   * Appends a multi-line string representation of this LDAP protocol op to the
202   * provided buffer.
203   *
204   * @param  buffer  The buffer to which the information should be appended.
205   * @param  indent  The number of spaces from the margin that the lines should
206   *                 be indented.
207   */
208  @Override
209  public void toString(StringBuilder buffer, int indent)
210  {
211    StringBuilder indentBuf = new StringBuilder(indent);
212    for (int i=0 ; i < indent; i++)
213    {
214      indentBuf.append(' ');
215    }
216
217    buffer.append(indentBuf);
218    buffer.append("Modify Request");
219    buffer.append(EOL);
220
221    buffer.append(indentBuf);
222    buffer.append("  DN:  ");
223    buffer.append(dn);
224    buffer.append(EOL);
225
226    buffer.append("  Modifications:");
227    buffer.append(EOL);
228
229    for (RawModification mod : modifications)
230    {
231      mod.toString(buffer, indent+4);
232    }
233  }
234}
235