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