001/*
002 * Copyright 2011-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2011-2020 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) 2011-2020 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.util;
037
038
039
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.Iterator;
043import java.util.List;
044
045
046
047/**
048 * This class provides a data structure which holds information about a SASL
049 * mechanism supported for use with the {@link SASLUtils} class.
050 */
051@NotMutable()
052@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
053public final class SASLMechanismInfo
054{
055  // Indicates whether this SASL mechanism allows a password to be provided.
056  private final boolean acceptsPassword;
057
058  // Indicates whether this SASL mechanism requires a password to be provided.
059  private final boolean requiresPassword;
060
061  // The list of options available for use with this mechanism.
062  private final List<SASLOption> options;
063
064  // A description for this SASL mechanism.
065  private final String description;
066
067  // The name for this SASL mechanism.
068  private final String name;
069
070
071
072  /**
073   * Creates a new SASL mechanism info object with the provided information.
074   *
075   * @param  name              The name for the SASL mechanism.
076   * @param  description       A description for the SASL mechanism.
077   * @param  acceptsPassword   Indicates whether the SASL mechanism allows a
078   *                           password to be provided.
079   * @param  requiresPassword  Indicates whether the SASL mechanism requires a
080   *                           password to be provided.
081   * @param  options           The set of options that are associated with the
082   *                           SASL mechanism.
083   */
084  public SASLMechanismInfo(final String name, final String description,
085                           final boolean acceptsPassword,
086                           final boolean requiresPassword,
087                           final SASLOption... options)
088  {
089    this.name             = name;
090    this.description      = description;
091    this.acceptsPassword  = acceptsPassword;
092    this.requiresPassword = requiresPassword;
093
094    if ((options == null) || (options.length == 0))
095    {
096      this.options = Collections.emptyList();
097    }
098    else
099    {
100      this.options = Collections.unmodifiableList(Arrays.asList(options));
101    }
102  }
103
104
105
106  /**
107   * Retrieves the name of the SASL mechanism.
108   *
109   * @return  The name of the SASL mechanism.
110   */
111  public String getName()
112  {
113    return name;
114  }
115
116
117
118  /**
119   * Retrieves a description for the SASL mechanism.
120   *
121   * @return  A description for the SASL mechanism.
122   */
123  public String getDescription()
124  {
125    return description;
126  }
127
128
129
130  /**
131   * Indicates whether the SASL mechanism accepts a password for authentication
132   * processing.
133   *
134   * @return  {@code true} if the SASL mechanism accepts a password for
135   *          authentication processing, or {@code false} if not.
136   */
137  public boolean acceptsPassword()
138  {
139    return acceptsPassword;
140  }
141
142
143
144  /**
145   * Indicates whether the SASL mechanism requires a password for authentication
146   * processing.
147   *
148   * @return  {@code true} if the SASL mechanism requires a password for
149   *          authentication processing, or {@code false} if not.
150   */
151  public boolean requiresPassword()
152  {
153    return requiresPassword;
154  }
155
156
157
158  /**
159   * Retrieves a list of the options that may be used with the SASL mechanism.
160   *
161   * @return  A list of the options that may be used with the SASL mechanism, or
162   *          an empty list if there are no supported SASL options for the
163   *          associated mechanism.
164   */
165  public List<SASLOption> getOptions()
166  {
167    return options;
168  }
169
170
171
172  /**
173   * Retrieves a string representation of this SASL mechanism info object.
174   *
175   * @return  A string representation of this SASL mechanism info object.
176   */
177  @Override()
178  public String toString()
179  {
180    final StringBuilder buffer = new StringBuilder();
181    toString(buffer);
182    return buffer.toString();
183  }
184
185
186
187  /**
188   * Appends a string representation of this SASL mechanism info object to the
189   * provided buffer.
190   *
191   * @param  buffer  The buffer to which the information should be appended.
192   */
193  public void toString(final StringBuilder buffer)
194  {
195    buffer.append("SASLMechanismInfo(name='");
196    buffer.append(name);
197    buffer.append("', description='");
198    buffer.append(description);
199    buffer.append("', acceptsPassword=");
200    buffer.append(acceptsPassword);
201    buffer.append(", requiresPassword=");
202    buffer.append(requiresPassword);
203    buffer.append(", options={");
204
205    final Iterator<SASLOption> iterator = options.iterator();
206    while (iterator.hasNext())
207    {
208      iterator.next().toString(buffer);
209      if (iterator.hasNext())
210      {
211        buffer.append(", ");
212      }
213    }
214
215    buffer.append("})");
216  }
217}