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