001    /*
002     * Copyright 2011-2014 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2011-2014 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;
022    
023    
024    
025    import com.unboundid.asn1.ASN1OctetString;
026    import com.unboundid.util.Validator;
027    
028    
029    
030    /**
031     * This class provides a mechanism for performing SASL authentication in a
032     * generic manner.  The caller is responsible for properly encoding the
033     * credentials (if any) and interpreting the result.  Further, if the requested
034     * SASL mechanism is one that requires multiple stages, then the caller is
035     * responsible for all processing in each stage.
036     */
037    public final class GenericSASLBindRequest
038           extends SASLBindRequest
039    {
040      /**
041       * The serial version UID for this serializable class.
042       */
043      private static final long serialVersionUID = 7740968332104559230L;
044    
045    
046    
047      // The SASL credentials that should be used for the bind request.
048      private final ASN1OctetString credentials;
049    
050      // The bind DN to use for the bind request.
051      private final String bindDN;
052    
053      // The name of the SASL mechanism that should be used for the bind request.
054      private final String mechanism;
055    
056    
057    
058      /**
059       * Creates a new generic SASL bind request with the provided information.
060       *
061       * @param  bindDN       The bind DN that should be used for the request.  It
062       *                      may be {@code null} if the target identity should be
063       *                      derived from the credentials or some other source.
064       * @param  mechanism    The name of the mechanism that should be used for the
065       *                      SASL bind.  It must not be {@code null}.
066       * @param  credentials  The credentials that should be used for the SASL bind.
067       *                      It may be {@code null} if no credentials should be
068       *                      used.
069       * @param  controls     The set of controls to include in the SASL bind
070       *                      request.  It may be {@code null} or empty if no
071       *                      request controls are needed.
072       */
073      public GenericSASLBindRequest(final String bindDN, final String mechanism,
074                                    final ASN1OctetString credentials,
075                                    final Control... controls)
076      {
077        super(controls);
078    
079        Validator.ensureNotNull(mechanism);
080    
081        this.bindDN      = bindDN;
082        this.mechanism   = mechanism;
083        this.credentials = credentials;
084      }
085    
086    
087    
088      /**
089       * Retrieves the bind DN for this SASL bind request, if any.
090       *
091       * @return  The bind DN for this SASL bind request, or {@code null} if the
092       *          target identity should be determined from the credentials or some
093       *          other mechanism.
094       */
095      public String getBindDN()
096      {
097        return bindDN;
098      }
099    
100    
101    
102      /**
103       * {@inheritDoc}
104       */
105      @Override()
106      public String getSASLMechanismName()
107      {
108        return mechanism;
109      }
110    
111    
112    
113      /**
114       * Retrieves the credentials for the SASL bind request, if any.
115       *
116       * @return  The credentials for the SASL bind request, or {@code null} if
117       *          there are none.
118       */
119      public ASN1OctetString getCredentials()
120      {
121        return credentials;
122      }
123    
124    
125    
126      /**
127       * {@inheritDoc}
128       */
129      @Override()
130      protected BindResult process(final LDAPConnection connection, final int depth)
131                throws LDAPException
132      {
133        return sendBindRequest(connection, bindDN, credentials, getControls(),
134             getResponseTimeoutMillis(connection));
135      }
136    
137    
138    
139      /**
140       * {@inheritDoc}
141       */
142      @Override()
143      public GenericSASLBindRequest duplicate()
144      {
145        return duplicate(getControls());
146      }
147    
148    
149    
150      /**
151       * {@inheritDoc}
152       */
153      @Override()
154      public GenericSASLBindRequest duplicate(final Control[] controls)
155      {
156        return new GenericSASLBindRequest(bindDN, mechanism, credentials,
157             controls);
158      }
159    
160    
161    
162      /**
163       * {@inheritDoc}
164       */
165      @Override()
166      public void toString(final StringBuilder buffer)
167      {
168        buffer.append("GenericSASLBindRequest(mechanism='");
169        buffer.append(mechanism);
170        buffer.append('\'');
171    
172        if (bindDN != null)
173        {
174          buffer.append(", bindDN='");
175          buffer.append(bindDN);
176          buffer.append('\'');
177        }
178    
179        if (credentials != null)
180        {
181          buffer.append(", credentials=byte[");
182          buffer.append(credentials.getValueLength());
183          buffer.append(']');
184        }
185    
186        final Control[] controls = getControls();
187        if (controls.length > 0)
188        {
189          buffer.append(", controls={");
190          for (int i=0; i < controls.length; i++)
191          {
192            if (i > 0)
193            {
194              buffer.append(", ");
195            }
196    
197            buffer.append(controls[i]);
198          }
199          buffer.append('}');
200        }
201    
202        buffer.append(')');
203      }
204    }