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 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.protocols.ldap;
018
019
020import java.io.IOException;
021
022import org.forgerock.opendj.io.*;
023import org.forgerock.opendj.ldap.ByteString;
024
025import org.forgerock.i18n.slf4j.LocalizedLogger;
026import static org.opends.server.protocols.ldap.LDAPConstants.*;
027import static org.opends.server.util.ServerConstants.*;
028
029
030/**
031 * This class defines the structures and methods for an LDAP modify DN request
032 * protocol op, which is used to move or rename an entry or subtree within the
033 * Directory Server.
034 */
035public class ModifyDNRequestProtocolOp
036       extends ProtocolOp
037{
038  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
039
040  /** The current entry DN for this modify DN request. */
041  private ByteString entryDN;
042
043  /** The new RDN for this modify DN request. */
044  private ByteString newRDN;
045
046  /** The new superior DN for this modify DN request. */
047  private ByteString newSuperior;
048
049  /** Indicates whether to delete the current RDN value(s). */
050  private boolean deleteOldRDN;
051
052
053
054  /**
055   * Creates a new modify DN request protocol op with the provided information.
056   *
057   * @param  entryDN       The current entry DN for this modify DN request.
058   * @param  newRDN        The new RDN for this modify DN request.
059   * @param  deleteOldRDN  Indicates whether to delete the current RDN value(s).
060   */
061  public ModifyDNRequestProtocolOp(ByteString entryDN,
062                                   ByteString newRDN, boolean deleteOldRDN)
063  {
064    this.entryDN      = entryDN;
065    this.newRDN       = newRDN;
066    this.deleteOldRDN = deleteOldRDN;
067    this.newSuperior  = null;
068  }
069
070
071
072  /**
073   * Creates a new modify DN request protocol op with the provided information.
074   *
075   * @param  entryDN       The current entry DN for this modify DN request.
076   * @param  newRDN        The new RDN for this modify DN request.
077   * @param  deleteOldRDN  Indicates whether to delete the current RDN value(s).
078   * @param  newSuperior   The new superior DN for this modify DN request.
079   */
080  public ModifyDNRequestProtocolOp(ByteString entryDN,
081                                   ByteString newRDN, boolean deleteOldRDN,
082                                   ByteString newSuperior)
083  {
084    this.entryDN      = entryDN;
085    this.newRDN       = newRDN;
086    this.deleteOldRDN = deleteOldRDN;
087    this.newSuperior  = newSuperior;
088  }
089
090
091
092  /**
093   * Retrieves the current entry DN for this modify DN request.
094   *
095   * @return  The current entry DN for this modify DN request.
096   */
097  public ByteString getEntryDN()
098  {
099    return entryDN;
100  }
101
102
103
104  /**
105   * Retrieves the new RDN for this modify DN request.
106   *
107   * @return  The new RDN for this modify DN request.
108   */
109  public ByteString getNewRDN()
110  {
111    return newRDN;
112  }
113
114
115
116  /**
117   * Indicates whether the current RDN value(s) should be deleted.
118   *
119   * @return  <CODE>true</CODE> if the current RDN value(s) should be deleted,
120   *          or <CODE>false</CODE> if not.
121   */
122  public boolean deleteOldRDN()
123  {
124    return deleteOldRDN;
125  }
126
127
128
129  /**
130   * Retrieves the new superior DN for this modify DN request.
131   *
132   * @return  The new superior DN for this modify DN request, or
133   *          <CODE>null</CODE> if none was provided.
134   */
135  public ByteString getNewSuperior()
136  {
137    return newSuperior;
138  }
139
140
141
142  /**
143   * Retrieves the BER type for this protocol op.
144   *
145   * @return  The BER type for this protocol op.
146   */
147  @Override
148  public byte getType()
149  {
150    return OP_TYPE_MODIFY_DN_REQUEST;
151  }
152
153
154
155  /**
156   * Retrieves the name for this protocol op type.
157   *
158   * @return  The name for this protocol op type.
159   */
160  @Override
161  public String getProtocolOpName()
162  {
163    return "Modify DN Request";
164  }
165
166  /**
167   * Writes this protocol op to an ASN.1 output stream.
168   *
169   * @param stream The ASN.1 output stream to write to.
170   * @throws IOException If a problem occurs while writing to the stream.
171   */
172  @Override
173  public void write(ASN1Writer stream) throws IOException
174  {
175    stream.writeStartSequence(OP_TYPE_MODIFY_DN_REQUEST);
176    stream.writeOctetString(entryDN);
177    stream.writeOctetString(newRDN);
178    stream.writeBoolean(deleteOldRDN);
179
180    if(newSuperior != null)
181    {
182      stream.writeOctetString(TYPE_MODIFY_DN_NEW_SUPERIOR, newSuperior);
183    }
184
185    stream.writeEndSequence();
186  }
187
188
189
190  /**
191   * Appends a string representation of this LDAP protocol op to the provided
192   * buffer.
193   *
194   * @param  buffer  The buffer to which the string should be appended.
195   */
196  @Override
197  public void toString(StringBuilder buffer)
198  {
199    buffer.append("ModifyDNRequest(dn=").append(entryDN);
200    buffer.append(", newRDN=").append(newRDN);
201    buffer.append(", deleteOldRDN=").append(deleteOldRDN);
202
203    if (newSuperior != null)
204    {
205      buffer.append(", newSuperior=").append(newSuperior);
206    }
207
208    buffer.append(")");
209  }
210
211
212
213  /**
214   * Appends a multi-line string representation of this LDAP protocol op to the
215   * provided buffer.
216   *
217   * @param  buffer  The buffer to which the information should be appended.
218   * @param  indent  The number of spaces from the margin that the lines should
219   *                 be indented.
220   */
221  @Override
222  public void toString(StringBuilder buffer, int indent)
223  {
224    StringBuilder indentBuf = new StringBuilder(indent);
225    for (int i=0 ; i < indent; i++)
226    {
227      indentBuf.append(' ');
228    }
229
230    buffer.append(indentBuf);
231    buffer.append("Modify DN Request");
232    buffer.append(EOL);
233
234    buffer.append(indentBuf);
235    buffer.append("  Entry DN:  ");
236    buffer.append(entryDN);
237    buffer.append(EOL);
238
239    buffer.append(indentBuf);
240    buffer.append("  New RDN:  ");
241    buffer.append(newRDN);
242    buffer.append(EOL);
243
244    buffer.append(indentBuf);
245    buffer.append("  Delete Old RDN:  ");
246    buffer.append(deleteOldRDN);
247    buffer.append(EOL);
248
249    if (newSuperior != null)
250    {
251      buffer.append(indentBuf);
252      buffer.append("  New Superior:  ");
253      buffer.append(newSuperior);
254      buffer.append(EOL);
255    }
256  }
257}
258