001/* 002 * Copyright 2015-2019 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2015-2019 Ping Identity Corporation 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.util.args; 022 023 024 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.Collections; 029import java.util.Iterator; 030import java.util.List; 031 032import com.unboundid.ldap.sdk.DN; 033import com.unboundid.util.Debug; 034import com.unboundid.util.NotMutable; 035import com.unboundid.util.StaticUtils; 036import com.unboundid.util.ThreadSafety; 037import com.unboundid.util.ThreadSafetyLevel; 038import com.unboundid.util.Validator; 039 040import static com.unboundid.util.args.ArgsMessages.*; 041 042 043 044/** 045 * This class provides an implementation of an argument value validator that is 046 * expected to be used with string or DN arguments and ensures that all values 047 * for the argument are valid DNs that are not within one or more specified 048 * subtrees. 049 */ 050@NotMutable() 051@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 052public final class ProhibitDNInSubtreeArgumentValueValidator 053 extends ArgumentValueValidator 054 implements Serializable 055{ 056 /** 057 * The serial version UID for this serializable class. 058 */ 059 private static final long serialVersionUID = 171827460774234825L; 060 061 062 063 // The set of prohibited base DNs for values of the associated argument. 064 private final List<DN> baseDNs; 065 066 067 068 /** 069 * Creates a new instance of this argument value validator with the provided 070 * information. 071 * 072 * @param baseDNs The set of prohibited base DNs for values of the 073 * associated argument. It must not be {@code null} or 074 * empty. 075 */ 076 public ProhibitDNInSubtreeArgumentValueValidator(final DN... baseDNs) 077 { 078 this(StaticUtils.toList(baseDNs)); 079 } 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 prohibited base DNs for values of the 088 * associated argument. It must not be {@code null} or 089 * empty. 090 */ 091 public ProhibitDNInSubtreeArgumentValueValidator(final Collection<DN> baseDNs) 092 { 093 Validator.ensureNotNull(baseDNs); 094 Validator.ensureFalse(baseDNs.isEmpty()); 095 096 this.baseDNs = Collections.unmodifiableList(new ArrayList<>(baseDNs)); 097 } 098 099 100 101 /** 102 * Retrieves a list of the prohibited base DNs for this argument value 103 * validator. 104 * 105 * @return A list of the prohibited base DNs for this argument value 106 * validator. 107 */ 108 public List<DN> getBaseDNs() 109 { 110 return baseDNs; 111 } 112 113 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override() 119 public void validateArgumentValue(final Argument argument, 120 final String valueString) 121 throws ArgumentException 122 { 123 final DN dn; 124 try 125 { 126 dn = new DN(valueString); 127 } 128 catch (final Exception e) 129 { 130 Debug.debugException(e); 131 throw new ArgumentException( 132 ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_NOT_DN.get(valueString, 133 argument.getIdentifierString()), 134 e); 135 } 136 137 for (final DN baseDN : baseDNs) 138 { 139 if (dn.isDescendantOf(baseDN, true)) 140 { 141 throw new ArgumentException( 142 ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_IN_SUBTREE.get( 143 valueString, argument.getIdentifierString(), 144 String.valueOf(baseDN))); 145 } 146 } 147 } 148 149 150 151 /** 152 * Retrieves a string representation of this argument value validator. 153 * 154 * @return A string representation of this argument value validator. 155 */ 156 @Override() 157 public String toString() 158 { 159 final StringBuilder buffer = new StringBuilder(); 160 toString(buffer); 161 return buffer.toString(); 162 } 163 164 165 166 /** 167 * Appends a string representation of this argument value validator to the 168 * provided buffer. 169 * 170 * @param buffer The buffer to which the string representation should be 171 * appended. 172 */ 173 public void toString(final StringBuilder buffer) 174 { 175 buffer.append("ProhibitDNInSubtreeArgumentValueValidator(baseDNs={"); 176 177 final Iterator<DN> iterator = baseDNs.iterator(); 178 while (iterator.hasNext()) 179 { 180 buffer.append('\''); 181 buffer.append(iterator.next().toString()); 182 buffer.append('\''); 183 184 if (iterator.hasNext()) 185 { 186 buffer.append(", "); 187 } 188 } 189 190 buffer.append("})"); 191 } 192}