001/* 002 * Copyright 2015-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2015-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) 2015-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.args; 037 038 039 040import java.io.Serializable; 041 042import com.unboundid.ldap.sdk.LDAPException; 043import com.unboundid.ldap.sdk.LDAPURL; 044import com.unboundid.util.Debug; 045import com.unboundid.util.NotMutable; 046import com.unboundid.util.NotNull; 047import com.unboundid.util.ThreadSafety; 048import com.unboundid.util.ThreadSafetyLevel; 049 050import static com.unboundid.util.args.ArgsMessages.*; 051 052 053 054/** 055 * This class provides an implementation of an argument value validator that is 056 * expected to be used with a string argument and ensures that all values for 057 * the argument are valid LDAP URLs. It can optionally indicate which elements 058 * are required to be present in the URL. 059 */ 060@NotMutable() 061@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 062public final class LDAPURLArgumentValueValidator 063 extends ArgumentValueValidator 064 implements Serializable 065{ 066 /** 067 * The serial version UID for this serializable class. 068 */ 069 private static final long serialVersionUID = -8867023666922488786L; 070 071 072 073 // Indicates whether the attributes element is required to be present in the 074 // URL with at least one value. 075 private final boolean requireAttributes; 076 077 // Indicates whether a non-empty base DN element is required to be present in 078 // the URL. 079 private final boolean requireBaseDN; 080 081 // Indicates whether the filter element is required to be present in the URL. 082 private final boolean requireFilter; 083 084 // Indicates whether the host element is required to be present in the URL. 085 private final boolean requireHost; 086 087 // Indicates whether the port element is required to be present in the URL. 088 private final boolean requirePort; 089 090 // Indicates whether the scope element is required to be present in the URL. 091 private final boolean requireScope; 092 093 094 095 /** 096 * Creates a new instance of this LDAP URL argument value validator that will 097 * accept values that represent any valid LDAP URL. 098 */ 099 public LDAPURLArgumentValueValidator() 100 { 101 this(false, false, false, false, false, false); 102 } 103 104 105 106 /** 107 * Creates a new instance of this LDAP URL argument value validator that will 108 * accept values that represent valid LDAP URLs with the specified 109 * constraints. 110 * 111 * @param requireHost Indicates whether LDAP URL values are required 112 * to include the host element. 113 * @param requirePort Indicates whether LDAP URL values are required 114 * to include the port element. 115 * @param requireBaseDN Indicates whether LDAP URL values are required 116 * to include a non-empty base DN element. 117 * @param requireAttributes Indicates whether LDAP URL values are required 118 * to include an attribute list with at least one 119 * attribute description. 120 * @param requireScope Indicates whether LDAP URL values are required 121 * to include the scope element. 122 * @param requireFilter Indicates whether LDAP URL values are required 123 * to include the filter element. 124 */ 125 public LDAPURLArgumentValueValidator(final boolean requireHost, 126 final boolean requirePort, 127 final boolean requireBaseDN, 128 final boolean requireAttributes, 129 final boolean requireScope, 130 final boolean requireFilter) 131 { 132 this.requireHost = requireHost; 133 this.requirePort = requirePort; 134 this.requireBaseDN = requireBaseDN; 135 this.requireAttributes = requireAttributes; 136 this.requireScope = requireScope; 137 this.requireFilter = requireFilter; 138 } 139 140 141 142 /** 143 * Indicates whether LDAP URL values are required to include the host element. 144 * 145 * @return {@code true} if LDAP URL values are required to include the host 146 * element, or {@code false} if not. 147 */ 148 public boolean requireHost() 149 { 150 return requireHost; 151 } 152 153 154 155 /** 156 * Indicates whether LDAP URL values are required to include the port element. 157 * 158 * @return {@code true} if LDAP URL values are required to include the port 159 * element, or {@code false} if not. 160 */ 161 public boolean requirePort() 162 { 163 return requirePort; 164 } 165 166 167 168 /** 169 * Indicates whether LDAP URL values are required to include a non-empty base 170 * DN element. 171 * 172 * @return {@code true} if LDAP URL values are required to include a 173 * non-empty base DN element, or {@code false} if not. 174 */ 175 public boolean requireBaseDN() 176 { 177 return requireBaseDN; 178 } 179 180 181 182 /** 183 * Indicates whether LDAP URL values are required to include the attributes 184 * element with at least one attribute description. 185 * 186 * @return {@code true} if LDAP URL values are required to include the 187 * attributes element, or {@code false} if not. 188 */ 189 public boolean requireAttributes() 190 { 191 return requireAttributes; 192 } 193 194 195 196 /** 197 * Indicates whether LDAP URL values are required to include the scope 198 * element. 199 * 200 * @return {@code true} if LDAP URL values are required to include the scope 201 * element, or {@code false} if not. 202 */ 203 public boolean requireScope() 204 { 205 return requireScope; 206 } 207 208 209 210 /** 211 * Indicates whether LDAP URL values are required to include the filter 212 * element. 213 * 214 * @return {@code true} if LDAP URL values are required to include the filter 215 * element, or {@code false} if not. 216 */ 217 public boolean requireFilter() 218 { 219 return requireFilter; 220 } 221 222 223 224 /** 225 * {@inheritDoc} 226 */ 227 @Override() 228 public void validateArgumentValue(@NotNull final Argument argument, 229 @NotNull final String valueString) 230 throws ArgumentException 231 { 232 final LDAPURL ldapURL; 233 try 234 { 235 ldapURL = new LDAPURL(valueString); 236 } 237 catch (final LDAPException e) 238 { 239 Debug.debugException(e); 240 throw new ArgumentException( 241 ERR_LDAP_URL_VALIDATOR_VALUE_NOT_LDAP_URL.get(valueString, 242 argument.getIdentifierString(), e.getMessage()), 243 e); 244 } 245 246 if (requireHost && (! ldapURL.hostProvided())) 247 { 248 throw new ArgumentException( 249 ERR_LDAP_URL_VALIDATOR_MISSING_HOST.get(valueString, 250 argument.getIdentifierString())); 251 } 252 253 if (requirePort && (! ldapURL.portProvided())) 254 { 255 throw new ArgumentException( 256 ERR_LDAP_URL_VALIDATOR_MISSING_PORT.get(valueString, 257 argument.getIdentifierString())); 258 } 259 260 if (requireBaseDN && (! ldapURL.baseDNProvided())) 261 { 262 throw new ArgumentException( 263 ERR_LDAP_URL_VALIDATOR_MISSING_BASE_DN.get(valueString, 264 argument.getIdentifierString())); 265 } 266 267 if (requireAttributes && (! ldapURL.attributesProvided())) 268 { 269 throw new ArgumentException( 270 ERR_LDAP_URL_VALIDATOR_MISSING_ATTRIBUTES.get(valueString, 271 argument.getIdentifierString())); 272 } 273 274 if (requireScope && (! ldapURL.scopeProvided())) 275 { 276 throw new ArgumentException( 277 ERR_LDAP_URL_VALIDATOR_MISSING_SCOPE.get(valueString, 278 argument.getIdentifierString())); 279 } 280 281 if (requireFilter && (! ldapURL.filterProvided())) 282 { 283 throw new ArgumentException( 284 ERR_LDAP_URL_VALIDATOR_MISSING_FILTER.get(valueString, 285 argument.getIdentifierString())); 286 } 287 } 288 289 290 291 /** 292 * Retrieves a string representation of this argument value validator. 293 * 294 * @return A string representation of this argument value validator. 295 */ 296 @Override() 297 @NotNull() 298 public String toString() 299 { 300 final StringBuilder buffer = new StringBuilder(); 301 toString(buffer); 302 return buffer.toString(); 303 } 304 305 306 307 /** 308 * Appends a string representation of this argument value validator to the 309 * provided buffer. 310 * 311 * @param buffer The buffer to which the string representation should be 312 * appended. 313 */ 314 public void toString(@NotNull final StringBuilder buffer) 315 { 316 buffer.append("LDAPURLArgumentValueValidator(requireHost="); 317 buffer.append(requireHost); 318 buffer.append(", requirePort="); 319 buffer.append(requirePort); 320 buffer.append(", requireBaseDN="); 321 buffer.append(requireBaseDN); 322 buffer.append(", requireAttributes="); 323 buffer.append(requireAttributes); 324 buffer.append(", requireScope="); 325 buffer.append(requireScope); 326 buffer.append(", requireFilter="); 327 buffer.append(requireFilter); 328 buffer.append(')'); 329 } 330}