001/*
002 * Copyright 2009-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2019 Ping Identity Corporation
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 an add response protocol op.
049 */
050@InternalUseOnly()
051@NotMutable()
052@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
053public final class AddResponseProtocolOp
054       extends GenericResponseProtocolOp
055{
056  /**
057   * The serial version UID for this serializable class.
058   */
059  private static final long serialVersionUID = 5870546796977558902L;
060
061
062
063  /**
064   * Creates a new instance of this add response protocol op with the provided
065   * 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 AddResponseProtocolOp(final int resultCode, final String matchedDN,
075                             final String diagnosticMessage,
076                             final List<String> referralURLs)
077  {
078    super(LDAPMessage.PROTOCOL_OP_TYPE_ADD_RESPONSE, resultCode, matchedDN,
079          diagnosticMessage, referralURLs);
080  }
081
082
083
084  /**
085   * Creates a new add response protocol op from the provided LDAP result
086   * object.
087   *
088   * @param  result  The LDAP result object to use to create this protocol op.
089   */
090  public AddResponseProtocolOp(final LDAPResult result)
091  {
092    super(LDAPMessage.PROTOCOL_OP_TYPE_ADD_RESPONSE,
093         result.getResultCode().intValue(), result.getMatchedDN(),
094         result.getDiagnosticMessage(),
095         StaticUtils.toList(result.getReferralURLs()));
096  }
097
098
099
100  /**
101   * Creates a new add response protocol op read from the provided ASN.1 stream
102   * reader.
103   *
104   * @param  reader  The ASN.1 stream reader from which to read the add response
105   *                 protocol op.
106   *
107   * @throws  LDAPException  If a problem occurs while reading or parsing the
108   *                         add response.
109   */
110  AddResponseProtocolOp(final ASN1StreamReader reader)
111       throws LDAPException
112  {
113    super(reader);
114  }
115
116
117
118  /**
119   * {@inheritDoc}
120   */
121  @Override()
122  public ASN1Element encodeProtocolOp()
123  {
124    final ArrayList<ASN1Element> elements = new ArrayList<>(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<>(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_ADD_RESPONSE,
160         elements);
161  }
162
163
164
165  /**
166   * Decodes the provided ASN.1 element as an add response protocol op.
167   *
168   * @param  element  The ASN.1 element to be decoded.
169   *
170   * @return  The decoded add response protocol op.
171   *
172   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
173   *                         an add response protocol op.
174   */
175  public static AddResponseProtocolOp 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.isEmpty())
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.isEmpty())
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<>(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 AddResponseProtocolOp(resultCode, matchedDN, diagnosticMessage,
228           referralURLs);
229    }
230    catch (final Exception e)
231    {
232      Debug.debugException(e);
233      throw new LDAPException(ResultCode.DECODING_ERROR,
234           ERR_ADD_RESPONSE_CANNOT_DECODE.get(
235                StaticUtils.getExceptionMessage(e)),
236           e);
237    }
238  }
239}