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