001/*
002 * Copyright 2015-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2015-2024 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-2024 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.NotNull;
051import com.unboundid.util.StaticUtils;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054import com.unboundid.util.Validator;
055
056import static com.unboundid.util.args.ArgsMessages.*;
057
058
059
060/**
061 * This class provides an implementation of an argument value validator that is
062 * expected to be used with string or DN arguments and ensures that all values
063 * for the argument are valid DNs that are not within one or more specified
064 * subtrees.
065 */
066@NotMutable()
067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
068public final class ProhibitDNInSubtreeArgumentValueValidator
069       extends ArgumentValueValidator
070       implements Serializable
071{
072  /**
073   * The serial version UID for this serializable class.
074   */
075  private static final long serialVersionUID = 171827460774234825L;
076
077
078
079  // The set of prohibited base DNs for values of the associated argument.
080  @NotNull private final List<DN> baseDNs;
081
082
083
084  /**
085   * Creates a new instance of this argument value validator with the provided
086   * information.
087   *
088   * @param  baseDNs  The set of prohibited base DNs for values of the
089   *                  associated argument.  It must not be {@code null} or
090   *                  empty.
091   */
092  public ProhibitDNInSubtreeArgumentValueValidator(
093              @NotNull final DN... baseDNs)
094  {
095    this(StaticUtils.toList(baseDNs));
096  }
097
098
099
100  /**
101   * Creates a new instance of this argument value validator with the provided
102   * information.
103   *
104   * @param  baseDNs  The set of prohibited base DNs for values of the
105   *                  associated argument.  It must not be {@code null} or
106   *                  empty.
107   */
108  public ProhibitDNInSubtreeArgumentValueValidator(
109              @NotNull final Collection<DN> baseDNs)
110  {
111    Validator.ensureNotNull(baseDNs);
112    Validator.ensureFalse(baseDNs.isEmpty());
113
114    this.baseDNs = Collections.unmodifiableList(new ArrayList<>(baseDNs));
115  }
116
117
118
119  /**
120   * Retrieves a list of the prohibited base DNs for this argument value
121   * validator.
122   *
123   * @return  A list of the prohibited base DNs for this argument value
124   *          validator.
125   */
126  @NotNull()
127  public List<DN> getBaseDNs()
128  {
129    return baseDNs;
130  }
131
132
133
134  /**
135   * {@inheritDoc}
136   */
137  @Override()
138  public void validateArgumentValue(@NotNull final Argument argument,
139                                    @NotNull final String valueString)
140         throws ArgumentException
141  {
142    final DN dn;
143    try
144    {
145      dn = new DN(valueString);
146    }
147    catch (final Exception e)
148    {
149      Debug.debugException(e);
150      throw new ArgumentException(
151           ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_NOT_DN.get(valueString,
152                argument.getIdentifierString()),
153           e);
154    }
155
156    for (final DN baseDN : baseDNs)
157    {
158      if (dn.isDescendantOf(baseDN, true))
159      {
160        throw new ArgumentException(
161             ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_IN_SUBTREE.get(
162                  valueString, argument.getIdentifierString(),
163                  String.valueOf(baseDN)));
164      }
165    }
166  }
167
168
169
170  /**
171   * Retrieves a string representation of this argument value validator.
172   *
173   * @return  A string representation of this argument value validator.
174   */
175  @Override()
176  @NotNull()
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(@NotNull final StringBuilder buffer)
194  {
195    buffer.append("ProhibitDNInSubtreeArgumentValueValidator(baseDNs={");
196
197    final Iterator<DN> iterator = baseDNs.iterator();
198    while (iterator.hasNext())
199    {
200      buffer.append('\'');
201      buffer.append(iterator.next().toString());
202      buffer.append('\'');
203
204      if (iterator.hasNext())
205      {
206        buffer.append(", ");
207      }
208    }
209
210    buffer.append("})");
211  }
212}