001/*
002 * Copyright 2009-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2009-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.protocol;
037
038
039
040import com.unboundid.asn1.ASN1Boolean;
041import com.unboundid.asn1.ASN1Buffer;
042import com.unboundid.asn1.ASN1BufferSequence;
043import com.unboundid.asn1.ASN1Element;
044import com.unboundid.asn1.ASN1OctetString;
045import com.unboundid.asn1.ASN1Sequence;
046import com.unboundid.asn1.ASN1StreamReader;
047import com.unboundid.asn1.ASN1StreamReaderSequence;
048import com.unboundid.ldap.sdk.Control;
049import com.unboundid.ldap.sdk.LDAPException;
050import com.unboundid.ldap.sdk.ModifyDNRequest;
051import com.unboundid.ldap.sdk.ResultCode;
052import com.unboundid.util.Debug;
053import com.unboundid.util.InternalUseOnly;
054import com.unboundid.util.NotMutable;
055import com.unboundid.util.StaticUtils;
056import com.unboundid.util.ThreadSafety;
057import com.unboundid.util.ThreadSafetyLevel;
058
059import static com.unboundid.ldap.protocol.ProtocolMessages.*;
060
061
062
063/**
064 * This class provides an implementation of an LDAP modify DN request protocol
065 * op.
066 */
067@InternalUseOnly()
068@NotMutable()
069@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
070public final class ModifyDNRequestProtocolOp
071       implements ProtocolOp
072{
073  /**
074   * The BER type for the newSuperior element.
075   */
076  public static final byte TYPE_NEW_SUPERIOR = (byte) 0x80;
077
078
079
080  /**
081   * The serial version UID for this serializable class.
082   */
083  private static final long serialVersionUID = 7514385089303489375L;
084
085
086
087  // The deleteOldRDN flag for this modify DN request.
088  private final boolean deleteOldRDN;
089
090  // The entry DN for this modify DN request.
091  private final String dn;
092
093  // The new RDN for this modify DN request.
094  private final String newRDN;
095
096  // The new superior DN for this modify DN request.
097  private final String newSuperiorDN;
098
099
100
101  /**
102   * Creates a new modify DN request protocol op with the provided information.
103   *
104   * @param  dn             The entry DN for this modify DN request.
105   * @param  newRDN         The new RDN for this modify DN request.
106   * @param  deleteOldRDN   Indicates whether to delete the old RDN values.
107   * @param  newSuperiorDN  The new superior DN for this modify DN request, or
108   *                        {@code null} if there is none.
109   */
110  public ModifyDNRequestProtocolOp(final String dn, final String newRDN,
111                                   final boolean deleteOldRDN,
112                                   final String newSuperiorDN)
113  {
114    this.dn            = dn;
115    this.newRDN        = newRDN;
116    this.deleteOldRDN  = deleteOldRDN;
117    this.newSuperiorDN = newSuperiorDN;
118  }
119
120
121
122  /**
123   * Creates a new modify DN request protocol op from the provided modify DN
124   * request object.
125   *
126   * @param  request  The modify DN request object to use to create this
127   *                  protocol op.
128   */
129  public ModifyDNRequestProtocolOp(final ModifyDNRequest request)
130  {
131    dn            = request.getDN();
132    newRDN        = request.getNewRDN();
133    deleteOldRDN  = request.deleteOldRDN();
134    newSuperiorDN = request.getNewSuperiorDN();
135  }
136
137
138
139  /**
140   * Creates a new modify DN request protocol op read from the provided ASN.1
141   * stream reader.
142   *
143   * @param  reader  The ASN.1 stream reader from which to read the modify DN
144   *                 request protocol op.
145   *
146   * @throws  LDAPException  If a problem occurs while reading or parsing the
147   *                         modify DN request.
148   */
149  ModifyDNRequestProtocolOp(final ASN1StreamReader reader)
150       throws LDAPException
151  {
152    try
153    {
154      final ASN1StreamReaderSequence opSequence = reader.beginSequence();
155
156      dn           = reader.readString();
157      newRDN       = reader.readString();
158      deleteOldRDN = reader.readBoolean();
159
160      if (opSequence.hasMoreElements())
161      {
162        newSuperiorDN = reader.readString();
163      }
164      else
165      {
166        newSuperiorDN = null;
167      }
168    }
169    catch (final Exception e)
170    {
171      Debug.debugException(e);
172
173      throw new LDAPException(ResultCode.DECODING_ERROR,
174           ERR_MODIFY_DN_REQUEST_CANNOT_DECODE.get(
175                StaticUtils.getExceptionMessage(e)),
176           e);
177    }
178  }
179
180
181
182  /**
183   * Retrieves the target entry DN for this modify DN request.
184   *
185   * @return  The target entry DN for this modify DN request.
186   */
187  public String getDN()
188  {
189    return dn;
190  }
191
192
193
194  /**
195   * Retrieves the new RDN for this modify DN request.
196   *
197   * @return  The new RDN for this modify DN request.
198   */
199  public String getNewRDN()
200  {
201    return newRDN;
202  }
203
204
205
206  /**
207   * Indicates whether to delete the old RDN values from the target entry.
208   *
209   * @return  {@code true} if the old RDN values should be removed from the
210   *          entry, or {@code false} if not.
211   */
212  public boolean deleteOldRDN()
213  {
214    return deleteOldRDN;
215  }
216
217
218
219  /**
220   * Retrieves the new superior DN for this modify DN request, if any.
221   *
222   * @return  The new superior DN for this modify DN request, or {@code null} if
223   *          there is none.
224   */
225  public String getNewSuperiorDN()
226  {
227    return newSuperiorDN;
228  }
229
230
231
232  /**
233   * {@inheritDoc}
234   */
235  @Override()
236  public byte getProtocolOpType()
237  {
238    return LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST;
239  }
240
241
242
243  /**
244   * {@inheritDoc}
245   */
246  @Override()
247  public ASN1Element encodeProtocolOp()
248  {
249    if (newSuperiorDN == null)
250    {
251      return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST,
252           new ASN1OctetString(dn),
253           new ASN1OctetString(newRDN),
254           new ASN1Boolean(deleteOldRDN));
255    }
256    else
257    {
258      return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST,
259           new ASN1OctetString(dn),
260           new ASN1OctetString(newRDN),
261           new ASN1Boolean(deleteOldRDN),
262           new ASN1OctetString(TYPE_NEW_SUPERIOR, newSuperiorDN));
263    }
264  }
265
266
267
268  /**
269   * Decodes the provided ASN.1 element as a modify DN request protocol op.
270   *
271   * @param  element  The ASN.1 element to be decoded.
272   *
273   * @return  The decoded modify DN request protocol op.
274   *
275   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
276   *                         a modify DN request protocol op.
277   */
278  public static ModifyDNRequestProtocolOp decodeProtocolOp(
279                                               final ASN1Element element)
280         throws LDAPException
281  {
282    try
283    {
284      final ASN1Element[] elements =
285           ASN1Sequence.decodeAsSequence(element).elements();
286      final String dn =
287           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
288      final String newRDN =
289           ASN1OctetString.decodeAsOctetString(elements[1]).stringValue();
290      final boolean deleteOldRDN =
291           ASN1Boolean.decodeAsBoolean(elements[2]).booleanValue();
292
293      final String newSuperiorDN;
294      if (elements.length > 3)
295      {
296        newSuperiorDN =
297             ASN1OctetString.decodeAsOctetString(elements[3]).stringValue();
298      }
299      else
300      {
301        newSuperiorDN = null;
302      }
303
304      return new ModifyDNRequestProtocolOp(dn, newRDN, deleteOldRDN,
305           newSuperiorDN);
306    }
307    catch (final Exception e)
308    {
309      Debug.debugException(e);
310      throw new LDAPException(ResultCode.DECODING_ERROR,
311           ERR_MODIFY_DN_REQUEST_CANNOT_DECODE.get(
312                StaticUtils.getExceptionMessage(e)),
313           e);
314    }
315  }
316
317
318
319  /**
320   * {@inheritDoc}
321   */
322  @Override()
323  public void writeTo(final ASN1Buffer buffer)
324  {
325    final ASN1BufferSequence opSequence =
326         buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST);
327    buffer.addOctetString(dn);
328    buffer.addOctetString(newRDN);
329    buffer.addBoolean(deleteOldRDN);
330
331    if (newSuperiorDN != null)
332    {
333      buffer.addOctetString(TYPE_NEW_SUPERIOR, newSuperiorDN);
334    }
335    opSequence.end();
336  }
337
338
339
340  /**
341   * Creates a modify DN request from this protocol op.
342   *
343   * @param  controls  The set of controls to include in the modify DN request.
344   *                   It may be empty or {@code null} if no controls should be
345   *                   included.
346   *
347   * @return  The modify DN request that was created.
348   */
349  public ModifyDNRequest toModifyDNRequest(final Control... controls)
350  {
351    return new ModifyDNRequest(dn, newRDN, deleteOldRDN, newSuperiorDN,
352         controls);
353  }
354
355
356
357  /**
358   * Retrieves a string representation of this protocol op.
359   *
360   * @return  A string representation of this protocol op.
361   */
362  @Override()
363  public String toString()
364  {
365    final StringBuilder buffer = new StringBuilder();
366    toString(buffer);
367    return buffer.toString();
368  }
369
370
371
372  /**
373   * {@inheritDoc}
374   */
375  @Override()
376  public void toString(final StringBuilder buffer)
377  {
378    buffer.append("ModifyDNRequestProtocolOp(dn='");
379    buffer.append(dn);
380    buffer.append("', newRDN='");
381    buffer.append(newRDN);
382    buffer.append("', deleteOldRDN=");
383    buffer.append(deleteOldRDN);
384
385    if (newSuperiorDN != null)
386    {
387      buffer.append(", newSuperiorDN='");
388      buffer.append(newSuperiorDN);
389      buffer.append('\'');
390    }
391
392    buffer.append(')');
393  }
394}