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.Attribute; 043import com.unboundid.ldap.sdk.persist.PersistUtils; 044import com.unboundid.ldap.sdk.schema.Schema; 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 attribute type names (or numeric OIDs) or attribute 057 * descriptions (a name or OID with attribute options). It can optionally use a 058 * provided schema to verify that the specified attribute type is defined. 059 */ 060@NotMutable() 061@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 062public final class AttributeNameArgumentValueValidator 063 extends ArgumentValueValidator 064 implements Serializable 065{ 066 /** 067 * The serial version UID for this serializable class. 068 */ 069 private static final long serialVersionUID = 1781129993679474323L; 070 071 072 073 // Indicates whether to allow values to include attribute options. 074 private final boolean allowOptions; 075 076 // An optional schema to use to verify that the specified attribute type is 077 // defined. 078 private final Schema schema; 079 080 081 082 /** 083 * Creates a new instance of this attribute name argument value validator that 084 * will not permit attribute options and will not attempt to verify that the 085 * specified attribute type is defined in a schema. 086 */ 087 public AttributeNameArgumentValueValidator() 088 { 089 this(false, null); 090 } 091 092 093 094 /** 095 * Creates a new instance of this attribute name argument value validator with 096 * the provided information. 097 * 098 * @param allowOptions Indicates whether to allow values that include one or 099 * more attribute options. 100 * @param schema An optional schema that can be used to verify that 101 * the specified attribute type is defined. 102 */ 103 public AttributeNameArgumentValueValidator(final boolean allowOptions, 104 final Schema schema) 105 { 106 this.allowOptions = allowOptions; 107 this.schema = schema; 108 } 109 110 111 112 /** 113 * Indicates whether to allow values that include one or more attribute 114 * options. 115 * 116 * @return {@code true} if values will be allowed to include attribute 117 * options, or {@code false} if not. 118 */ 119 public boolean allowOptions() 120 { 121 return allowOptions; 122 } 123 124 125 126 /** 127 * Retrieves the schema that will be used to verify that attribute types 128 * specified in argument values are defined, if any. 129 * 130 * @return The schema that will be used to verify that attribute types 131 * specified in argument values are defined, or {@code null} if no 132 * such validation will be performed. 133 */ 134 public Schema getSchema() 135 { 136 return schema; 137 } 138 139 140 141 /** 142 * {@inheritDoc} 143 */ 144 @Override() 145 public void validateArgumentValue(final Argument argument, 146 final String valueString) 147 throws ArgumentException 148 { 149 final StringBuilder errorMessage = new StringBuilder(); 150 if (! PersistUtils.isValidLDAPName(valueString, allowOptions, errorMessage)) 151 { 152 throw new ArgumentException(ERR_ATTR_NAME_VALIDATOR_INVALID_VALUE.get( 153 valueString, argument.getIdentifierString(), 154 String.valueOf(errorMessage))); 155 } 156 157 if (schema != null) 158 { 159 final String baseName = Attribute.getBaseName(valueString); 160 if (schema.getAttributeType(baseName) == null) 161 { 162 throw new ArgumentException( 163 ERR_ATTR_NAME_VALIDATOR_TYPE_NOT_DEFINED.get(valueString, 164 argument.getIdentifierString(), baseName)); 165 } 166 } 167 } 168 169 170 171 /** 172 * Retrieves a string representation of this argument value validator. 173 * 174 * @return A string representation of this argument value validator. 175 */ 176 @Override() 177 public String toString() 178 { 179 final StringBuilder buffer = new StringBuilder(); 180 toString(buffer); 181 return buffer.toString(); 182 } 183 184 185 186 /** 187 * Appends a string representation of this argument value validator to the 188 * provided buffer. 189 * 190 * @param buffer The buffer to which the string representation should be 191 * appended. 192 */ 193 public void toString(final StringBuilder buffer) 194 { 195 buffer.append("AttributeNameArgumentValueValidator(allowOptions="); 196 buffer.append(allowOptions); 197 buffer.append(", hasSchema="); 198 buffer.append(schema != null); 199 buffer.append(')'); 200 } 201}