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