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 not within one or more specified
063 * subtrees.
064 */
065@NotMutable()
066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
067public final class ProhibitDNInSubtreeArgumentValueValidator
068       extends ArgumentValueValidator
069       implements Serializable
070{
071  /**
072   * The serial version UID for this serializable class.
073   */
074  private static final long serialVersionUID = 171827460774234825L;
075
076
077
078  // The set of prohibited 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 prohibited base DNs for values of the
088   *                  associated argument.  It must not be {@code null} or
089   *                  empty.
090   */
091  public ProhibitDNInSubtreeArgumentValueValidator(final DN... baseDNs)
092  {
093    this(StaticUtils.toList(baseDNs));
094  }
095
096
097
098  /**
099   * Creates a new instance of this argument value validator with the provided
100   * information.
101   *
102   * @param  baseDNs  The set of prohibited base DNs for values of the
103   *                  associated argument.  It must not be {@code null} or
104   *                  empty.
105   */
106  public ProhibitDNInSubtreeArgumentValueValidator(final Collection<DN> baseDNs)
107  {
108    Validator.ensureNotNull(baseDNs);
109    Validator.ensureFalse(baseDNs.isEmpty());
110
111    this.baseDNs = Collections.unmodifiableList(new ArrayList<>(baseDNs));
112  }
113
114
115
116  /**
117   * Retrieves a list of the prohibited base DNs for this argument value
118   * validator.
119   *
120   * @return  A list of the prohibited base DNs for this argument value
121   *          validator.
122   */
123  public List<DN> getBaseDNs()
124  {
125    return baseDNs;
126  }
127
128
129
130  /**
131   * {@inheritDoc}
132   */
133  @Override()
134  public void validateArgumentValue(final Argument argument,
135                                    final String valueString)
136         throws ArgumentException
137  {
138    final DN dn;
139    try
140    {
141      dn = new DN(valueString);
142    }
143    catch (final Exception e)
144    {
145      Debug.debugException(e);
146      throw new ArgumentException(
147           ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_NOT_DN.get(valueString,
148                argument.getIdentifierString()),
149           e);
150    }
151
152    for (final DN baseDN : baseDNs)
153    {
154      if (dn.isDescendantOf(baseDN, true))
155      {
156        throw new ArgumentException(
157             ERR_PROHIBIT_DN_IN_SUBTREE_VALIDATOR_VALUE_IN_SUBTREE.get(
158                  valueString, argument.getIdentifierString(),
159                  String.valueOf(baseDN)));
160      }
161    }
162  }
163
164
165
166  /**
167   * Retrieves a string representation of this argument value validator.
168   *
169   * @return  A string representation of this argument value validator.
170   */
171  @Override()
172  public String toString()
173  {
174    final StringBuilder buffer = new StringBuilder();
175    toString(buffer);
176    return buffer.toString();
177  }
178
179
180
181  /**
182   * Appends a string representation of this argument value validator to the
183   * provided buffer.
184   *
185   * @param  buffer  The buffer to which the string representation should be
186   *                 appended.
187   */
188  public void toString(final StringBuilder buffer)
189  {
190    buffer.append("ProhibitDNInSubtreeArgumentValueValidator(baseDNs={");
191
192    final Iterator<DN> iterator = baseDNs.iterator();
193    while (iterator.hasNext())
194    {
195      buffer.append('\'');
196      buffer.append(iterator.next().toString());
197      buffer.append('\'');
198
199      if (iterator.hasNext())
200      {
201        buffer.append(", ");
202      }
203    }
204
205    buffer.append("})");
206  }
207}