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; 041import java.util.ArrayList; 042import java.util.Collection; 043import java.util.Collections; 044import java.util.Iterator; 045import java.util.List; 046 047import com.unboundid.ldap.sdk.DN; 048import com.unboundid.util.Debug; 049import com.unboundid.util.NotMutable; 050import com.unboundid.util.StaticUtils; 051import com.unboundid.util.ThreadSafety; 052import com.unboundid.util.ThreadSafetyLevel; 053import com.unboundid.util.Validator; 054 055import static com.unboundid.util.args.ArgsMessages.*; 056 057 058 059/** 060 * This class provides an implementation of an argument value validator that is 061 * expected to be used with string or DN arguments and ensures that all values 062 * for the argument are valid DNs that are within one or more specified 063 * subtrees. 064 */ 065@NotMutable() 066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 067public final class RequireDNInSubtreeArgumentValueValidator 068 extends ArgumentValueValidator 069 implements Serializable 070{ 071 /** 072 * The serial version UID for this serializable class. 073 */ 074 private static final long serialVersionUID = -4517307608327628921L; 075 076 077 078 // The set of permitted base DNs for values of the associated argument. 079 private final List<DN> baseDNs; 080 081 082 083 /** 084 * Creates a new instance of this argument value validator with the provided 085 * information. 086 * 087 * @param baseDNs The set of permitted base DNs for values of the associated 088 * argument. It must not be {@code null} or empty. 089 */ 090 public RequireDNInSubtreeArgumentValueValidator(final DN... baseDNs) 091 { 092 this(StaticUtils.toList(baseDNs)); 093 } 094 095 096 097 /** 098 * Creates a new instance of this argument value validator with the provided 099 * information. 100 * 101 * @param baseDNs The set of permitted base DNs for values of the associated 102 * argument. It must not be {@code null} or empty. 103 */ 104 public RequireDNInSubtreeArgumentValueValidator(final Collection<DN> baseDNs) 105 { 106 Validator.ensureNotNull(baseDNs); 107 Validator.ensureFalse(baseDNs.isEmpty()); 108 109 this.baseDNs = Collections.unmodifiableList(new ArrayList<>(baseDNs)); 110 } 111 112 113 114 /** 115 * Retrieves a list of the permitted base DNs for this argument value 116 * validator. 117 * 118 * @return A list of the permitted base DNs for this argument value 119 * validator. 120 */ 121 public List<DN> getBaseDNs() 122 { 123 return baseDNs; 124 } 125 126 127 128 /** 129 * {@inheritDoc} 130 */ 131 @Override() 132 public void validateArgumentValue(final Argument argument, 133 final String valueString) 134 throws ArgumentException 135 { 136 final DN dn; 137 try 138 { 139 dn = new DN(valueString); 140 } 141 catch (final Exception e) 142 { 143 Debug.debugException(e); 144 throw new ArgumentException( 145 ERR_REQUIRE_DN_IN_SUBTREE_VALIDATOR_VALUE_NOT_DN.get(valueString, 146 argument.getIdentifierString()), 147 e); 148 } 149 150 151 if (baseDNs.size() == 1) 152 { 153 if (! dn.isDescendantOf(baseDNs.get(0), true)) 154 { 155 throw new ArgumentException( 156 ERR_REQUIRE_DN_IN_SUBTREE_VALIDATOR_VALUE_NOT_IN_SUBTREE.get( 157 valueString, argument.getIdentifierString(), 158 String.valueOf(baseDNs.get(0)))); 159 } 160 } 161 else 162 { 163 final StringBuilder dnList = new StringBuilder(); 164 final Iterator<DN> iterator = baseDNs.iterator(); 165 while (iterator.hasNext()) 166 { 167 final DN baseDN = iterator.next(); 168 if (dn.isDescendantOf(baseDN, true)) 169 { 170 return; 171 } 172 173 dnList.append('\''); 174 dnList.append(baseDN); 175 dnList.append('\''); 176 177 if (iterator.hasNext()) 178 { 179 dnList.append(", "); 180 } 181 } 182 183 throw new ArgumentException( 184 ERR_REQUIRE_DN_IN_SUBTREE_VALIDATOR_VALUE_NOT_IN_SUBTREES.get( 185 valueString, argument.getIdentifierString(), 186 dnList.toString())); 187 } 188 } 189 190 191 192 /** 193 * Retrieves a string representation of this argument value validator. 194 * 195 * @return A string representation of this argument value validator. 196 */ 197 @Override() 198 public String toString() 199 { 200 final StringBuilder buffer = new StringBuilder(); 201 toString(buffer); 202 return buffer.toString(); 203 } 204 205 206 207 /** 208 * Appends a string representation of this argument value validator to the 209 * provided buffer. 210 * 211 * @param buffer The buffer to which the string representation should be 212 * appended. 213 */ 214 public void toString(final StringBuilder buffer) 215 { 216 buffer.append("RequireDNInSubtreeArgumentValueValidator(baseDNs={"); 217 218 final Iterator<DN> iterator = baseDNs.iterator(); 219 while (iterator.hasNext()) 220 { 221 buffer.append('\''); 222 buffer.append(iterator.next().toString()); 223 buffer.append('\''); 224 225 if (iterator.hasNext()) 226 { 227 buffer.append(", "); 228 } 229 } 230 231 buffer.append("})"); 232 } 233}