Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Copyright (C) 2012 FoxLabs
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.foxlabs.validation.constraint;
 
 import java.util.Map;
 import java.util.Set;
 
 import java.net.URL;
 
 
This class provides CheckConstraint implementation that checks whether a string is valid URL reference. Also the allowed set of protocols can be configured.

 
 public final class UrlConstraint extends CheckConstraint<String> {
    
    
UrlConstraint default instance initialized with empty set of protocols.
 
     public static final UrlConstraint DEFAULT = new UrlConstraint((String[]) null);
    
    
Set of allowed protocols (empty set means all protocols are allowed).
 
     private final Set<Stringprotocols;
    
    
Constructs a new UrlConstraint with the specified array of allowed protocols.

Parameters:
protocols Array of allowed protocols.
Throws:
java.lang.IllegalArgumentException if the specified array of protocols contains null or empty elements.
 
     UrlConstraint(String[] protocols) {
         this. = protocols == null || protocols.length == 0
             ? Collections.<String>emptySet()
             : Collections.unmodifiableSet(Assert.noEmptyStringSet(protocols"protocols"));
     }
    
    
Constructs a new UrlConstraint from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines array of protocols that contains empty elements.
 
     UrlConstraint(Url annotation) {
         this(annotation.protocols());
     }
    
    
Returns java.lang.String type.

Returns:
java.lang.String type.
 
     @Override
     public Class<?> getType() {
         return String.class;
     }
    
    
Returns set of allowed protocols (empty set means all protocols are allowed).

Returns:
Set of allowed protocols.
 
     public Set<StringgetProtocols() {
         return ;
     }
    
    
Appends protocols argument that contains set of allowed protocols.

Parameters:
context Validation context.
arguments Arguments to be substituted into the error message template.
Returns:
true.
    @Override
    public boolean appendMessageArguments(ValidationContext<?> contextMap<StringObjectarguments) {
        super.appendMessageArguments(contextarguments);
        if (.size() > 0)
            arguments.put("protocols");
        return true;
    }
    
    
Checks whether the specified string is valid URL reference with allowed protocol.

Parameters:
value URL string.
context Validation context.
Returns:
true if the specified string is valid URL reference with allowed protocol; false otherwise.
    @Override
    protected <T> boolean check(String valueValidationContext<T> context) {
        if (value == null)
            return true;
        try {
            URL url = new URL(value);
            return .isEmpty() || .contains(url.getProtocol().toLowerCase());
        } catch (MalformedURLException e) {}
        return false;
    }
    
New to GrepCode? Check out our FAQ X