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