001/*
002 * Copyright 2009-2017 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2017 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.protocol;
022
023
024
025import java.util.ArrayList;
026import java.util.List;
027
028import com.unboundid.asn1.ASN1Element;
029import com.unboundid.asn1.ASN1Enumerated;
030import com.unboundid.asn1.ASN1OctetString;
031import com.unboundid.asn1.ASN1Sequence;
032import com.unboundid.asn1.ASN1StreamReader;
033import com.unboundid.ldap.sdk.LDAPException;
034import com.unboundid.ldap.sdk.LDAPResult;
035import com.unboundid.ldap.sdk.ResultCode;
036import com.unboundid.util.Debug;
037import com.unboundid.util.NotMutable;
038import com.unboundid.util.InternalUseOnly;
039import com.unboundid.util.StaticUtils;
040import com.unboundid.util.ThreadSafety;
041import com.unboundid.util.ThreadSafetyLevel;
042
043import static com.unboundid.ldap.protocol.ProtocolMessages.*;
044
045
046
047/**
048 * This class provides an implementation of a modify DN response protocol op.
049 */
050@InternalUseOnly()
051@NotMutable()
052@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
053public final class ModifyDNResponseProtocolOp
054       extends GenericResponseProtocolOp
055{
056  /**
057   * The serial version UID for this serializable class.
058   */
059  private static final long serialVersionUID = -8133223270933706583L;
060
061
062
063  /**
064   * Creates a new instance of this modify DN response protocol op with the
065   * provided information.
066   *
067   * @param  resultCode         The result code for this response.
068   * @param  matchedDN          The matched DN for this response, if available.
069   * @param  diagnosticMessage  The diagnostic message for this response, if
070   *                            any.
071   * @param  referralURLs       The list of referral URLs for this response, if
072   *                            any.
073   */
074  public ModifyDNResponseProtocolOp(final int resultCode,
075                                    final String matchedDN,
076                                    final String diagnosticMessage,
077                                    final List<String> referralURLs)
078  {
079    super(LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_RESPONSE, resultCode,
080          matchedDN, diagnosticMessage, referralURLs);
081  }
082
083
084
085  /**
086   * Creates a new modify DN response protocol op from the provided LDAP result
087   * object.
088   *
089   * @param  result  The LDAP result object to use to create this protocol op.
090   */
091  public ModifyDNResponseProtocolOp(final LDAPResult result)
092  {
093    super(LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_RESPONSE,
094         result.getResultCode().intValue(), result.getMatchedDN(),
095         result.getDiagnosticMessage(),
096         StaticUtils.toList(result.getReferralURLs()));
097  }
098
099
100
101  /**
102   * Creates a new modify DN response protocol op read from the provided ASN.1
103   * stream reader.
104   *
105   * @param  reader  The ASN.1 stream reader from which to read the modify DN
106   *                 response protocol op.
107   *
108   * @throws  LDAPException  If a problem occurs while reading or parsing the
109   *                         modify DN response.
110   */
111  ModifyDNResponseProtocolOp(final ASN1StreamReader reader)
112       throws LDAPException
113  {
114    super(reader);
115  }
116
117
118
119  /**
120   * {@inheritDoc}
121   */
122  public ASN1Element encodeProtocolOp()
123  {
124    final ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(4);
125    elements.add(new ASN1Enumerated(getResultCode()));
126
127    final String matchedDN = getMatchedDN();
128    if (matchedDN == null)
129    {
130      elements.add(new ASN1OctetString());
131    }
132    else
133    {
134      elements.add(new ASN1OctetString(matchedDN));
135    }
136
137    final String diagnosticMessage = getDiagnosticMessage();
138    if (diagnosticMessage == null)
139    {
140      elements.add(new ASN1OctetString());
141    }
142    else
143    {
144      elements.add(new ASN1OctetString(diagnosticMessage));
145    }
146
147    final List<String> referralURLs = getReferralURLs();
148    if (! referralURLs.isEmpty())
149    {
150      final ArrayList<ASN1Element> refElements =
151           new ArrayList<ASN1Element>(referralURLs.size());
152      for (final String r : referralURLs)
153      {
154        refElements.add(new ASN1OctetString(r));
155      }
156      elements.add(new ASN1Sequence(TYPE_REFERRALS, refElements));
157    }
158
159    return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_RESPONSE,
160         elements);
161  }
162
163
164
165  /**
166   * Decodes the provided ASN.1 element as a modify DN response protocol op.
167   *
168   * @param  element  The ASN.1 element to be decoded.
169   *
170   * @return  The decoded modify DN response protocol op.
171   *
172   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
173   *                         a modify DN response protocol op.
174   */
175  public static ModifyDNResponseProtocolOp decodeProtocolOp(
176                                                final ASN1Element element)
177         throws LDAPException
178  {
179    try
180    {
181      final ASN1Element[] elements =
182           ASN1Sequence.decodeAsSequence(element).elements();
183      final int resultCode =
184           ASN1Enumerated.decodeAsEnumerated(elements[0]).intValue();
185
186      final String matchedDN;
187      final String md =
188           ASN1OctetString.decodeAsOctetString(elements[1]).stringValue();
189      if (md.length() > 0)
190      {
191        matchedDN = md;
192      }
193      else
194      {
195        matchedDN = null;
196      }
197
198      final String diagnosticMessage;
199      final String dm =
200           ASN1OctetString.decodeAsOctetString(elements[2]).stringValue();
201      if (dm.length() > 0)
202      {
203        diagnosticMessage = dm;
204      }
205      else
206      {
207        diagnosticMessage = null;
208      }
209
210      final List<String> referralURLs;
211      if (elements.length == 4)
212      {
213        final ASN1Element[] refElements =
214             ASN1Sequence.decodeAsSequence(elements[3]).elements();
215        referralURLs = new ArrayList<String>(refElements.length);
216        for (final ASN1Element e : refElements)
217        {
218          referralURLs.add(
219               ASN1OctetString.decodeAsOctetString(e).stringValue());
220        }
221      }
222      else
223      {
224        referralURLs = null;
225      }
226
227      return new ModifyDNResponseProtocolOp(resultCode, matchedDN,
228           diagnosticMessage, referralURLs);
229    }
230    catch (final Exception e)
231    {
232      Debug.debugException(e);
233      throw new LDAPException(ResultCode.DECODING_ERROR,
234           ERR_MODIFY_DN_RESPONSE_CANNOT_DECODE.get(
235                StaticUtils.getExceptionMessage(e)),
236           e);
237    }
238  }
239}