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;
026
027import com.unboundid.ldap.sdk.LDAPException;
028import com.unboundid.ldap.sdk.LDAPURL;
029import com.unboundid.util.Debug;
030import com.unboundid.util.NotMutable;
031import com.unboundid.util.ThreadSafety;
032import com.unboundid.util.ThreadSafetyLevel;
033
034import static com.unboundid.util.args.ArgsMessages.*;
035
036
037
038/**
039 * This class provides an implementation of an argument value validator that is
040 * expected to be used with a string argument and ensures that all values for
041 * the argument are valid LDAP URLs.  It can optionally indicate which elements
042 * are required to be present in the URL.
043 */
044@NotMutable()
045@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
046public final class LDAPURLArgumentValueValidator
047       extends ArgumentValueValidator
048       implements Serializable
049{
050  /**
051   * The serial version UID for this serializable class.
052   */
053  private static final long serialVersionUID = -8867023666922488786L;
054
055
056
057  // Indicates whether the attributes element is required to be present in the
058  // URL with at least one value.
059  private final boolean requireAttributes;
060
061  // Indicates whether a non-empty base DN element is required to be present in
062  // the URL.
063  private final boolean requireBaseDN;
064
065  // Indicates whether the filter element is required to be present in the URL.
066  private final boolean requireFilter;
067
068  // Indicates whether the host element is required to be present in the URL.
069  private final boolean requireHost;
070
071  // Indicates whether the port element is required to be present in the URL.
072  private final boolean requirePort;
073
074  // Indicates whether the scope element is required to be present in the URL.
075  private final boolean requireScope;
076
077
078
079  /**
080   * Creates a new instance of this LDAP URL argument value validator that will
081   * accept values that represent any valid LDAP URL.
082   */
083  public LDAPURLArgumentValueValidator()
084  {
085    this(false, false, false, false, false, false);
086  }
087
088
089
090  /**
091   * Creates a new instance of this LDAP URL argument value validator that will
092   * accept values that represent valid LDAP URLs with the specified
093   * constraints.
094   *
095   * @param  requireHost        Indicates whether LDAP URL values are required
096   *                            to include the host element.
097   * @param  requirePort        Indicates whether LDAP URL values are required
098   *                            to include the port element.
099   * @param  requireBaseDN      Indicates whether LDAP URL values are required
100   *                            to include a non-empty base DN element.
101   * @param  requireAttributes  Indicates whether LDAP URL values are required
102   *                            to include an attribute list with at least one
103   *                            attribute description.
104   * @param  requireScope       Indicates whether LDAP URL values are required
105   *                            to include the scope element.
106   * @param  requireFilter      Indicates whether LDAP URL values are required
107   *                            to include the filter element.
108   */
109  public LDAPURLArgumentValueValidator(final boolean requireHost,
110                                       final boolean requirePort,
111                                       final boolean requireBaseDN,
112                                       final boolean requireAttributes,
113                                       final boolean requireScope,
114                                       final boolean requireFilter)
115  {
116    this.requireHost       = requireHost;
117    this.requirePort       = requirePort;
118    this.requireBaseDN     = requireBaseDN;
119    this.requireAttributes = requireAttributes;
120    this.requireScope      = requireScope;
121    this.requireFilter     = requireFilter;
122  }
123
124
125
126  /**
127   * Indicates whether LDAP URL values are required to include the host element.
128   *
129   * @return  {@code true} if LDAP URL values are required to include the host
130   *          element, or {@code false} if not.
131   */
132  public boolean requireHost()
133  {
134    return requireHost;
135  }
136
137
138
139  /**
140   * Indicates whether LDAP URL values are required to include the port element.
141   *
142   * @return  {@code true} if LDAP URL values are required to include the port
143   *          element, or {@code false} if not.
144   */
145  public boolean requirePort()
146  {
147    return requirePort;
148  }
149
150
151
152  /**
153   * Indicates whether LDAP URL values are required to include a non-empty base
154   * DN element.
155   *
156   * @return  {@code true} if LDAP URL values are required to include a
157   *          non-empty base DN element, or {@code false} if not.
158   */
159  public boolean requireBaseDN()
160  {
161    return requireBaseDN;
162  }
163
164
165
166  /**
167   * Indicates whether LDAP URL values are required to include the attributes
168   * element with at least one attribute description.
169   *
170   * @return  {@code true} if LDAP URL values are required to include the
171   *          attributes element, or {@code false} if not.
172   */
173  public boolean requireAttributes()
174  {
175    return requireAttributes;
176  }
177
178
179
180  /**
181   * Indicates whether LDAP URL values are required to include the scope
182   * element.
183   *
184   * @return  {@code true} if LDAP URL values are required to include the scope
185   *          element, or {@code false} if not.
186   */
187  public boolean requireScope()
188  {
189    return requireScope;
190  }
191
192
193
194  /**
195   * Indicates whether LDAP URL values are required to include the filter
196   * element.
197   *
198   * @return  {@code true} if LDAP URL values are required to include the filter
199   *          element, or {@code false} if not.
200   */
201  public boolean requireFilter()
202  {
203    return requireFilter;
204  }
205
206
207
208  /**
209   * {@inheritDoc}
210   */
211  @Override()
212  public void validateArgumentValue(final Argument argument,
213                                    final String valueString)
214         throws ArgumentException
215  {
216    final LDAPURL ldapURL;
217    try
218    {
219      ldapURL = new LDAPURL(valueString);
220    }
221    catch (final LDAPException e)
222    {
223      Debug.debugException(e);
224      throw new ArgumentException(
225           ERR_LDAP_URL_VALIDATOR_VALUE_NOT_LDAP_URL.get(valueString,
226                argument.getIdentifierString(), e.getMessage()),
227           e);
228    }
229
230    if (requireHost && (! ldapURL.hostProvided()))
231    {
232      throw new ArgumentException(
233           ERR_LDAP_URL_VALIDATOR_MISSING_HOST.get(valueString,
234                argument.getIdentifierString()));
235    }
236
237    if (requirePort && (! ldapURL.portProvided()))
238    {
239      throw new ArgumentException(
240           ERR_LDAP_URL_VALIDATOR_MISSING_PORT.get(valueString,
241                argument.getIdentifierString()));
242    }
243
244    if (requireBaseDN && (! ldapURL.baseDNProvided()))
245    {
246      throw new ArgumentException(
247           ERR_LDAP_URL_VALIDATOR_MISSING_BASE_DN.get(valueString,
248                argument.getIdentifierString()));
249    }
250
251    if (requireAttributes && (! ldapURL.attributesProvided()))
252    {
253      throw new ArgumentException(
254           ERR_LDAP_URL_VALIDATOR_MISSING_ATTRIBUTES.get(valueString,
255                argument.getIdentifierString()));
256    }
257
258    if (requireScope && (! ldapURL.scopeProvided()))
259    {
260      throw new ArgumentException(
261           ERR_LDAP_URL_VALIDATOR_MISSING_SCOPE.get(valueString,
262                argument.getIdentifierString()));
263    }
264
265    if (requireFilter && (! ldapURL.filterProvided()))
266    {
267      throw new ArgumentException(
268           ERR_LDAP_URL_VALIDATOR_MISSING_FILTER.get(valueString,
269                argument.getIdentifierString()));
270    }
271  }
272
273
274
275  /**
276   * Retrieves a string representation of this argument value validator.
277   *
278   * @return  A string representation of this argument value validator.
279   */
280  @Override()
281  public String toString()
282  {
283    final StringBuilder buffer = new StringBuilder();
284    toString(buffer);
285    return buffer.toString();
286  }
287
288
289
290  /**
291   * Appends a string representation of this argument value validator to the
292   * provided buffer.
293   *
294   * @param  buffer  The buffer to which the string representation should be
295   *                 appended.
296   */
297  public void toString(final StringBuilder buffer)
298  {
299    buffer.append("LDAPURLArgumentValueValidator(requireHost=");
300    buffer.append(requireHost);
301    buffer.append(", requirePort=");
302    buffer.append(requirePort);
303    buffer.append(", requireBaseDN=");
304    buffer.append(requireBaseDN);
305    buffer.append(", requireAttributes=");
306    buffer.append(requireAttributes);
307    buffer.append(", requireScope=");
308    buffer.append(requireScope);
309    buffer.append(", requireFilter=");
310    buffer.append(requireFilter);
311    buffer.append(')');
312  }
313}