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