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