001/* 002 * Copyright 2011-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2011-2024 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-2024 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 @NotNull private final List<SASLOption> options; 063 064 // A description for this SASL mechanism. 065 @NotNull private final String description; 066 067 // The name for this SASL mechanism. 068 @NotNull 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(@NotNull final String name, 085 @NotNull final String description, 086 final boolean acceptsPassword, 087 final boolean requiresPassword, 088 @Nullable final SASLOption... options) 089 { 090 this.name = name; 091 this.description = description; 092 this.acceptsPassword = acceptsPassword; 093 this.requiresPassword = requiresPassword; 094 095 if ((options == null) || (options.length == 0)) 096 { 097 this.options = Collections.emptyList(); 098 } 099 else 100 { 101 this.options = Collections.unmodifiableList(Arrays.asList(options)); 102 } 103 } 104 105 106 107 /** 108 * Retrieves the name of the SASL mechanism. 109 * 110 * @return The name of the SASL mechanism. 111 */ 112 @NotNull() 113 public String getName() 114 { 115 return name; 116 } 117 118 119 120 /** 121 * Retrieves a description for the SASL mechanism. 122 * 123 * @return A description for the SASL mechanism. 124 */ 125 @NotNull() 126 public String getDescription() 127 { 128 return description; 129 } 130 131 132 133 /** 134 * Indicates whether the SASL mechanism accepts a password for authentication 135 * processing. 136 * 137 * @return {@code true} if the SASL mechanism accepts a password for 138 * authentication processing, or {@code false} if not. 139 */ 140 public boolean acceptsPassword() 141 { 142 return acceptsPassword; 143 } 144 145 146 147 /** 148 * Indicates whether the SASL mechanism requires a password for authentication 149 * processing. 150 * 151 * @return {@code true} if the SASL mechanism requires a password for 152 * authentication processing, or {@code false} if not. 153 */ 154 public boolean requiresPassword() 155 { 156 return requiresPassword; 157 } 158 159 160 161 /** 162 * Retrieves a list of the options that may be used with the SASL mechanism. 163 * 164 * @return A list of the options that may be used with the SASL mechanism, or 165 * an empty list if there are no supported SASL options for the 166 * associated mechanism. 167 */ 168 @NotNull() 169 public List<SASLOption> getOptions() 170 { 171 return options; 172 } 173 174 175 176 /** 177 * Retrieves a string representation of this SASL mechanism info object. 178 * 179 * @return A string representation of this SASL mechanism info object. 180 */ 181 @Override() 182 @NotNull() 183 public String toString() 184 { 185 final StringBuilder buffer = new StringBuilder(); 186 toString(buffer); 187 return buffer.toString(); 188 } 189 190 191 192 /** 193 * Appends a string representation of this SASL mechanism info object to the 194 * provided buffer. 195 * 196 * @param buffer The buffer to which the information should be appended. 197 */ 198 public void toString(@NotNull final StringBuilder buffer) 199 { 200 buffer.append("SASLMechanismInfo(name='"); 201 buffer.append(name); 202 buffer.append("', description='"); 203 buffer.append(description); 204 buffer.append("', acceptsPassword="); 205 buffer.append(acceptsPassword); 206 buffer.append(", requiresPassword="); 207 buffer.append(requiresPassword); 208 buffer.append(", options={"); 209 210 final Iterator<SASLOption> iterator = options.iterator(); 211 while (iterator.hasNext()) 212 { 213 iterator.next().toString(buffer); 214 if (iterator.hasNext()) 215 { 216 buffer.append(", "); 217 } 218 } 219 220 buffer.append("})"); 221 } 222}