001/*
002 * Copyright 2016-2017 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2016-2017 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 */
021package com.unboundid.ldap.sdk.experimental;
022
023
024
025import com.unboundid.ldap.sdk.Entry;
026import com.unboundid.ldap.sdk.LDAPException;
027import com.unboundid.ldap.sdk.OperationType;
028import com.unboundid.ldap.sdk.ResultCode;
029import com.unboundid.util.Debug;
030import com.unboundid.util.NotMutable;
031import com.unboundid.util.StaticUtils;
032import com.unboundid.util.ThreadSafety;
033import com.unboundid.util.ThreadSafetyLevel;
034
035import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*;
036
037
038
039/**
040 * This class represents an entry that holds information about a bind operation
041 * processed by an LDAP server, as per the specification described in
042 * draft-chu-ldap-logschema-00.
043 */
044@NotMutable()
045@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
046public final class DraftChuLDAPLogSchema00BindEntry
047       extends DraftChuLDAPLogSchema00Entry
048{
049  /**
050   * The name of the attribute used to hold the bind request method.
051   */
052  public static final String ATTR_BIND_METHOD = "reqMethod";
053
054
055
056  /**
057   * The name of the attribute used to hold the LDAP protocol version specified
058   * in the bind request.
059   */
060  public static final String ATTR_PROTOCOL_VERSION = "reqVersion";
061
062
063
064  /**
065   * The serial version UID for this serializable class.
066   */
067  private static final long serialVersionUID = 864660009992589945L;
068
069
070
071  // The LDAP protocol version from the bind request.
072  private final int protocolVersion;
073
074  // The base name of the bind request method.
075  private final String bindMethod;
076
077  // The name of the SASL mechanism, if available.
078  private final String saslMechanism;
079
080
081
082  /**
083   * Creates a new instance of this bind access log entry from the provided
084   * entry.
085   *
086   * @param  entry  The entry used to create this bind access log entry.
087   *
088   * @throws  LDAPException  If the provided entry cannot be decoded as a valid
089   *                         bind access log entry as per the specification
090   *                         contained in draft-chu-ldap-logschema-00.
091   */
092  public DraftChuLDAPLogSchema00BindEntry(final Entry entry)
093         throws LDAPException
094  {
095    super(entry, OperationType.BIND);
096
097
098    // Get the protocol version.
099    final String versionString = entry.getAttributeValue(ATTR_PROTOCOL_VERSION);
100    if (versionString == null)
101    {
102      throw new LDAPException(ResultCode.DECODING_ERROR,
103           ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(),
104                ATTR_PROTOCOL_VERSION));
105    }
106    else
107    {
108      try
109      {
110        protocolVersion = Integer.parseInt(versionString);
111      }
112      catch (final Exception e)
113      {
114        Debug.debugException(e);
115        throw new LDAPException(ResultCode.DECODING_ERROR,
116             ERR_LOGSCHEMA_DECODE_BIND_VERSION_ERROR.get(entry.getDN(),
117                  ATTR_PROTOCOL_VERSION, versionString),
118             e);
119      }
120    }
121
122
123    // Get the bind method.  If it starts with "SASL/", then separate out the
124    // SASL mechanism name.
125    final String rawMethod = entry.getAttributeValue(ATTR_BIND_METHOD);
126    if (rawMethod == null)
127    {
128      throw new LDAPException(ResultCode.DECODING_ERROR,
129           ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(),
130                ATTR_BIND_METHOD));
131    }
132
133    final String lowerMethod = StaticUtils.toLowerCase(rawMethod);
134    if (lowerMethod.equals("simple"))
135    {
136      bindMethod = "SIMPLE";
137      saslMechanism = null;
138    }
139    else if (lowerMethod.startsWith("sasl/"))
140    {
141      bindMethod = "SASL";
142      saslMechanism = rawMethod.substring(5);
143    }
144    else
145    {
146      bindMethod = rawMethod;
147      saslMechanism = null;
148    }
149  }
150
151
152
153  /**
154   * Retrieves the LDAP protocol version for the bind request described by this
155   * bind access log entry.
156   *
157   * @return  The LDAP protocol version for the bind request described by this
158   *          bind access log entry.
159   */
160  public int getProtocolVersion()
161  {
162    return protocolVersion;
163  }
164
165
166
167  /**
168   * Retrieves the name of the bind method for the bind request described by
169   * this bind access log entry.  It is expected to be one of "SIMPLE" or
170   * "SASL".
171   *
172   * @return  The name of the bind method for the bind request described by this
173   *          bind access log entry.
174   */
175  public String getBindMethod()
176  {
177    return bindMethod;
178  }
179
180
181
182  /**
183   * Retrieves the name of the SASL mechanism name for the bind request
184   * described by this bind access log entry, if appropriate.
185   *
186   * @return  The name of the SASL mechanism for the bind request described by
187   *          this bind access log entry, or {@code null} if the bind method is
188   *          not "SASL".
189   */
190  public String getSASLMechanism()
191  {
192    return saslMechanism;
193  }
194}