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