001 /*
002 * Copyright 2015-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2015-2016 UnboundID Corp.
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 */
021 package com.unboundid.util.args;
022
023
024
025 import java.util.ArrayList;
026 import java.util.Collection;
027 import java.util.Collections;
028 import java.util.Iterator;
029 import java.util.List;
030
031 import com.unboundid.ldap.sdk.DN;
032 import com.unboundid.util.Debug;
033 import com.unboundid.util.NotMutable;
034 import com.unboundid.util.StaticUtils;
035 import com.unboundid.util.ThreadSafety;
036 import com.unboundid.util.ThreadSafetyLevel;
037 import com.unboundid.util.Validator;
038
039 import static com.unboundid.util.args.ArgsMessages.*;
040
041
042
043 /**
044 * This class provides an implementation of an argument value validator that is
045 * expected to be used with string or DN arguments and ensures that all values
046 * for the argument are valid DNs that are not within one or more specified
047 * subtrees.
048 */
049 @NotMutable()
050 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
051 public final class ProhibitDNInSubtreeArgumentValueValidator
052 extends ArgumentValueValidator
053 {
054 // The set of prohibited base DNs for values of the associated argument.
055 private final List<DN> baseDNs;
056
057
058
059 /**
060 * Creates a new instance of this argument value validator with the provided
061 * information.
062 *
063 * @param baseDNs The set of prohibited base DNs for values of the
064 * associated argument. It must not be {@code null} or
065 * empty.
066 */
067 public ProhibitDNInSubtreeArgumentValueValidator(final DN... baseDNs)
068 {
069 this(StaticUtils.toList(baseDNs));
070 }
071
072
073
074 /**
075 * Creates a new instance of this argument value validator with the provided
076 * information.
077 *
078 * @param baseDNs The set of prohibited base DNs for values of the
079 * associated argument. It must not be {@code null} or
080 * empty.
081 */
082 public ProhibitDNInSubtreeArgumentValueValidator(final Collection<DN> baseDNs)
083 {
084 Validator.ensureNotNull(baseDNs);
085 Validator.ensureFalse(baseDNs.isEmpty());
086
087 this.baseDNs = Collections.unmodifiableList(new ArrayList<DN>(baseDNs));
088 }
089
090
091
092 /**
093 * Retrieves a list of the prohibited base DNs for this argument value
094 * validator.
095 *
096 * @return A list of the prohibited base DNs for this argument value
097 * validator.
098 */
099 public List<DN> getBaseDNs()
100 {
101 return baseDNs;
102 }
103
104
105
106 /**
107 * {@inheritDoc}
108 */
109 @Override()
110 public void validateArgumentValue(final Argument argument,
111 final String valueString)
112 throws ArgumentException
113 {
114 final DN dn;
115 try
116 {
117 dn = new DN(valueString);
118 }
119 catch (final Exception e)
120 {
121 Debug.debugException(e);
122 throw new ArgumentException(
123 ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_NOT_DN.get(valueString,
124 argument.getIdentifierString()),
125 e);
126 }
127
128 for (final DN baseDN : baseDNs)
129 {
130 if (dn.isDescendantOf(baseDN, true))
131 {
132 throw new ArgumentException(
133 ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_IN_SUBTREE.get(
134 valueString, argument.getIdentifierString(),
135 String.valueOf(baseDN)));
136 }
137 }
138 }
139
140
141
142 /**
143 * Retrieves a string representation of this argument value validator.
144 *
145 * @return A string representation of this argument value validator.
146 */
147 @Override()
148 public String toString()
149 {
150 final StringBuilder buffer = new StringBuilder();
151 toString(buffer);
152 return buffer.toString();
153 }
154
155
156
157 /**
158 * Appends a string representation of this argument value validator to the
159 * provided buffer.
160 *
161 * @param buffer The buffer to which the string representation should be
162 * appended.
163 */
164 public void toString(final StringBuilder buffer)
165 {
166 buffer.append("ProhibitDNInSubtreeArgumentValueValidator(baseDNs={");
167
168 final Iterator<DN> iterator = baseDNs.iterator();
169 while (iterator.hasNext())
170 {
171 buffer.append('\'');
172 buffer.append(iterator.next().toString());
173 buffer.append('\'');
174
175 if (iterator.hasNext())
176 {
177 buffer.append(", ");
178 }
179 }
180
181 buffer.append("})");
182 }
183 }