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