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