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.sdk.migrate.ldapjdk;
037
038
039
040import java.io.Serializable;
041
042import com.unboundid.ldap.sdk.Control;
043import com.unboundid.ldap.sdk.LDAPResult;
044import com.unboundid.util.Extensible;
045import com.unboundid.util.NotMutable;
046import com.unboundid.util.NotNull;
047import com.unboundid.util.Nullable;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050
051
052
053/**
054 * This class provides a data structure that represents a response that may be
055 * received from a directory server.
056 * <BR><BR>
057 * This class is primarily intended to be used in the process of updating
058 * applications which use the Netscape Directory SDK for Java to switch to or
059 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
060 * using the Netscape Directory SDK for Java, the {@link LDAPResult} class
061 * should be used instead.
062 */
063@Extensible()
064@NotMutable()
065@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
066public class LDAPResponse
067       implements Serializable
068{
069  /**
070   * The serial version UID for this serializable class.
071   */
072  private static final long serialVersionUID = -8401666939604882177L;
073
074
075
076  // The LDAP result for this LDAP response.
077  @NotNull private final LDAPResult ldapResult;
078
079
080
081  /**
082   * Creates a new LDAP response from the provided {@link LDAPResult}.
083   *
084   * @param  ldapResult  The {@code LDAPResult} object to use to create this
085   *                     LDAP response.
086   */
087  public LDAPResponse(@NotNull final LDAPResult ldapResult)
088  {
089    this.ldapResult = ldapResult;
090  }
091
092
093
094  /**
095   * Retrieves the LDAP message ID for this LDAP response.
096   *
097   * @return  The LDAP message ID for this LDAP response.
098   */
099  public int getMessageID()
100  {
101    return ldapResult.getMessageID();
102  }
103
104
105
106  /**
107   * Retrieves the result code for this LDAP response.
108   *
109   * @return  The result code for this LDAP response.
110   */
111  public int getResultCode()
112  {
113    return ldapResult.getResultCode().intValue();
114  }
115
116
117
118  /**
119   * Retrieves the error message for this LDAP response, if available.
120   *
121   * @return  The error message for this LDAP response, or {@code null} if there
122   *          is none.
123   */
124  @Nullable()
125  public String getErrorMessage()
126  {
127    return ldapResult.getDiagnosticMessage();
128  }
129
130
131
132  /**
133   * Retrieves the matched DN for this LDAP response, if available.
134   *
135   * @return  The matched DN for this LDAP response, or {@code null} if there
136   *          is none.
137   */
138  @Nullable()
139  public String getMatchedDN()
140  {
141    return ldapResult.getMatchedDN();
142  }
143
144
145
146  /**
147   * Retrieves the set of referrals for this LDAP response, if any.
148   *
149   * @return  The set of referrals for this LDAP response, or {@code null} if
150   *          there are none.
151   */
152  @Nullable()
153  public String[] getReferrals()
154  {
155    final String[] referrals = ldapResult.getReferralURLs();
156    if (referrals.length == 0)
157    {
158      return null;
159    }
160    else
161    {
162      return referrals;
163    }
164  }
165
166
167
168  /**
169   * Retrieves the list of controls for this LDAP response, if any.
170   *
171   * @return  The list of controls for this LDAP response, or {@code null} if
172   *          there are none.
173   */
174  @Nullable()
175  public LDAPControl[] getControls()
176  {
177    final Control[] controls = ldapResult.getResponseControls();
178    if (controls.length == 0)
179    {
180      return null;
181    }
182
183    return LDAPControl.toLDAPControls(controls);
184  }
185
186
187
188  /**
189   * Retrieves an {@link LDAPResult} object that is the equivalent of this LDAP
190   * response.
191   *
192   * @return  An {@code LDAPResult} object that is the equivalent of this LDAP
193   *          response.
194   */
195  @NotNull()
196  public final LDAPResult toLDAPResult()
197  {
198    return ldapResult;
199  }
200
201
202
203  /**
204   * Retrieves a string representation of this LDAP response.
205   *
206   * @return  A string representation of this LDAP response.
207   */
208  @Override()
209  @NotNull()
210  public String toString()
211  {
212    return ldapResult.toString();
213  }
214}