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