001    /*
002     * Copyright 2008-2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2008-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.extensions;
022    
023    
024    
025    import com.unboundid.ldap.sdk.Control;
026    import com.unboundid.ldap.sdk.ExtendedResult;
027    import com.unboundid.ldap.sdk.LDAPException;
028    import com.unboundid.ldap.sdk.ResultCode;
029    
030    import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
031    
032    
033    
034    /**
035     * This class provides an implementation of the notice of disconnection extended
036     * result as defined in
037     * <A HREF="http://www.ietf.org/rfc/rfc4511.txt">RFC 4511</A>.  It may be used
038     * as an unsolicited notification to indicate that the directory server is
039     * closing the client connection.
040     * <BR><BR>
041     * See the {@code com.unboundid.ldap.sdk.UnsolicitedNotificationHandler}
042     * interface for a mechanism that can be used to receive and handle unsolicited
043     * notifications.
044     */
045    public final class NoticeOfDisconnectionExtendedResult
046           extends ExtendedResult
047    {
048      /**
049       * The OID (1.3.6.1.4.1.1466.20036) for the notice of disconnection extended
050       * result.
051       */
052      public static final String NOTICE_OF_DISCONNECTION_RESULT_OID =
053           "1.3.6.1.4.1.1466.20036";
054    
055    
056    
057      /**
058       * The serial version UID for this serializable class.
059       */
060      private static final long serialVersionUID = -4706102471360689558L;
061    
062    
063    
064      /**
065       * Creates a new instance of this notice of disconnection extended result from
066       * the provided generic extended result.
067       *
068       * @param  resultCode         The result code for the notice of disconnection.
069       * @param  diagnosticMessage  The diagnostic message to include in the
070       *                            notice of disconnection.  It may be {@code null}
071       *                            if no diagnostic message should be included.
072       * @param  responseControls   The set of controls to include in the notice of
073       *                            disconnection.  It may be {@code null} or empty
074       *                            if no response controls are needed.
075       */
076      public NoticeOfDisconnectionExtendedResult(final ResultCode resultCode,
077                                                 final String diagnosticMessage,
078                                                 final Control... responseControls)
079      {
080        this(0, resultCode, diagnosticMessage, null, null, responseControls);
081      }
082    
083    
084    
085      /**
086       * Creates a new instance of this notice of disconnection extended result from
087       * the provided generic extended result.
088       *
089       * @param  extendedResult  The extended result to use to create this notice of
090       *                         disconnection extended result.
091       */
092      public NoticeOfDisconnectionExtendedResult(
093                  final ExtendedResult extendedResult)
094      {
095        super(extendedResult);
096      }
097    
098    
099    
100      /**
101       * Creates a new instance of this notice of disconnection extended result from
102       * the provided LDAP exception.
103       *
104       * @param  ldapException  The LDAP exception to use to create this notice of
105       *                        disconnection extended result.
106       */
107      public NoticeOfDisconnectionExtendedResult(final LDAPException ldapException)
108      {
109        this(0, ldapException.getResultCode(), ldapException.getDiagnosticMessage(),
110             ldapException.getMatchedDN(), ldapException.getReferralURLs(),
111             ldapException.getResponseControls());
112      }
113    
114    
115    
116      /**
117       * Creates a new instance of this notice of disconnection extended result from
118       * the provided information.
119       *
120       * @param  messageID          The message ID for the LDAP message that is
121       *                            associated with this LDAP result.
122       * @param  resultCode         The result code from the response.
123       * @param  diagnosticMessage  The diagnostic message from the response, if
124       *                            available.
125       * @param  matchedDN          The matched DN from the response, if available.
126       * @param  referralURLs       The set of referral URLs from the response, if
127       *                            available.
128       * @param  responseControls   The set of controls from the response, if
129       *                            available.
130       */
131      public NoticeOfDisconnectionExtendedResult(
132                  final int messageID, final ResultCode resultCode,
133                  final String diagnosticMessage, final String matchedDN,
134                  final String[] referralURLs, final Control[] responseControls)
135      {
136        super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
137              NOTICE_OF_DISCONNECTION_RESULT_OID, null, responseControls);
138      }
139    
140    
141    
142      /**
143       * {@inheritDoc}
144       */
145      @Override()
146      public String getExtendedResultName()
147      {
148        return INFO_EXTENDED_RESULT_NAME_NOTICE_OF_DISCONNECT.get();
149      }
150    
151    
152    
153      /**
154       * Appends a string representation of this extended result to the provided
155       * buffer.
156       *
157       * @param  buffer  The buffer to which a string representation of this
158       *                 extended result will be appended.
159       */
160      @Override()
161      public void toString(final StringBuilder buffer)
162      {
163        buffer.append("NoticeOfDisconnectionExtendedResult(resultCode=");
164        buffer.append(getResultCode());
165    
166        final int messageID = getMessageID();
167        if (messageID >= 0)
168        {
169          buffer.append(", messageID=");
170          buffer.append(messageID);
171        }
172    
173        final String diagnosticMessage = getDiagnosticMessage();
174        if (diagnosticMessage != null)
175        {
176          buffer.append(", diagnosticMessage='");
177          buffer.append(diagnosticMessage);
178          buffer.append('\'');
179        }
180    
181        final String matchedDN = getMatchedDN();
182        if (matchedDN != null)
183        {
184          buffer.append(", matchedDN='");
185          buffer.append(matchedDN);
186          buffer.append('\'');
187        }
188    
189        final String[] referralURLs = getReferralURLs();
190        if (referralURLs.length > 0)
191        {
192          buffer.append(", referralURLs={");
193          for (int i=0; i < referralURLs.length; i++)
194          {
195            if (i > 0)
196            {
197              buffer.append(", ");
198            }
199    
200            buffer.append('\'');
201            buffer.append(referralURLs[i]);
202            buffer.append('\'');
203          }
204          buffer.append('}');
205        }
206    
207        buffer.append(", oid=");
208        buffer.append(NOTICE_OF_DISCONNECTION_RESULT_OID);
209    
210        final Control[] responseControls = getResponseControls();
211        if (responseControls.length > 0)
212        {
213          buffer.append(", responseControls={");
214          for (int i=0; i < responseControls.length; i++)
215          {
216            if (i > 0)
217            {
218              buffer.append(", ");
219            }
220    
221            buffer.append(responseControls[i]);
222          }
223          buffer.append('}');
224        }
225    
226        buffer.append(')');
227      }
228    }