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