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 java.net.URI;
026 import java.util.Collection;
027 import java.util.Collections;
028 import java.util.Iterator;
029 import java.util.LinkedHashSet;
030 import java.util.Set;
031
032 import com.unboundid.util.Debug;
033 import com.unboundid.util.NotMutable;
034 import com.unboundid.util.StaticUtils;
035 import com.unboundid.util.ThreadSafety;
036 import com.unboundid.util.ThreadSafetyLevel;
037
038 import static com.unboundid.util.args.ArgsMessages.*;
039
040
041
042 /**
043 * This class provides an implementation of an argument value validator that is
044 * expected to be used with a string argument and ensures that all values for
045 * the argument are valid URLs. It can optionally restrict the URLs to a
046 * specified set of schemes.
047 */
048 @NotMutable()
049 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
050 public final class URLArgumentValueValidator
051 extends ArgumentValueValidator
052 {
053 // The set of schemes allowed to be used in URLs.
054 private final Set<String> allowedSchemes;
055
056
057
058 /**
059 * Creates a new instance of this URL argument value validator that will
060 * accept values that are URLs with any of the specified schemes.
061 *
062 * @param allowedSchemes The names of the schemes for the URLs that will be
063 * accepted. It may be {@code null} or empty if any
064 * scheme will be accepted.
065 */
066 public URLArgumentValueValidator(final String... allowedSchemes)
067 {
068 this(StaticUtils.toList(allowedSchemes));
069 }
070
071
072
073 /**
074 * Creates a new instance of this URL argument value validator that will
075 * accept values that are URLs with any of the specified schemes.
076 *
077 * @param allowedSchemes The names of the schemes for the URLs that will be
078 * accepted. It may be {@code null} or empty if any
079 * scheme will be accepted.
080 */
081 public URLArgumentValueValidator(final Collection<String> allowedSchemes)
082 {
083 if (allowedSchemes == null)
084 {
085 this.allowedSchemes = Collections.emptySet();
086 }
087 else
088 {
089 this.allowedSchemes = Collections.unmodifiableSet(
090 new LinkedHashSet<String>(allowedSchemes));
091 }
092 }
093
094
095
096 /**
097 * Retrieves the names of the schemes for the URLs that will be accepted.
098 *
099 * @return The names of the schemes for the URLs that will be accepted, or
100 * an empty set if URLs will be allowed to have any scheme.
101 */
102 public Set<String> getAllowedSchemes()
103 {
104 return allowedSchemes;
105 }
106
107
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override()
113 public void validateArgumentValue(final Argument argument,
114 final String valueString)
115 throws ArgumentException
116 {
117 final URI uri;
118 try
119 {
120 uri = new URI(valueString);
121 }
122 catch (final Exception e)
123 {
124 Debug.debugException(e);
125 throw new ArgumentException(
126 ERR_URL_VALIDATOR_VALUE_NOT_URL.get(valueString,
127 argument.getIdentifierString(),
128 StaticUtils.getExceptionMessage(e)),
129 e);
130 }
131
132 if (uri.getScheme() == null)
133 {
134 throw new ArgumentException(ERR_URL_VALIDATOR_MISSING_SCHEME.get(
135 valueString, argument.getIdentifierString()));
136 }
137
138 if ((! allowedSchemes.isEmpty()) &&
139 (! allowedSchemes.contains(uri.getScheme())))
140 {
141 throw new ArgumentException(
142 ERR_URL_VALIDATOR_UNACCEPTABLE_SCHEME.get(valueString,
143 argument.getIdentifierString(), uri.getScheme()));
144 }
145 }
146
147
148
149 /**
150 * Retrieves a string representation of this argument value validator.
151 *
152 * @return A string representation of this argument value validator.
153 */
154 @Override()
155 public String toString()
156 {
157 final StringBuilder buffer = new StringBuilder();
158 toString(buffer);
159 return buffer.toString();
160 }
161
162
163
164 /**
165 * Appends a string representation of this argument value validator to the
166 * provided buffer.
167 *
168 * @param buffer The buffer to which the string representation should be
169 * appended.
170 */
171 public void toString(final StringBuilder buffer)
172 {
173 buffer.append("URLArgumentValueValidator(");
174
175 if (allowedSchemes != null)
176 {
177 buffer.append("allowedSchemes={");
178
179 final Iterator<String> iterator = allowedSchemes.iterator();
180 while (iterator.hasNext())
181 {
182 buffer.append('\'');
183 buffer.append(iterator.next());
184 buffer.append('\'');
185
186 if (iterator.hasNext())
187 {
188 buffer.append(", ");
189 }
190 }
191
192 buffer.append('}');
193 }
194
195 buffer.append(')');
196 }
197 }