You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/06/16 21:45:09 UTC

svn commit: r668275 - /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/urlvalidator/UrlValidator.java

Author: lu4242
Date: Mon Jun 16 12:45:08 2008
New Revision: 668275

URL: http://svn.apache.org/viewvc?rev=668275&view=rev
Log:
TOMAHAWK-449 URL Validator does not accept simple web addresses

Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/urlvalidator/UrlValidator.java

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/urlvalidator/UrlValidator.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/urlvalidator/UrlValidator.java?rev=668275&r1=668274&r2=668275&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/urlvalidator/UrlValidator.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/urlvalidator/UrlValidator.java Mon Jun 16 12:45:08 2008
@@ -24,7 +24,6 @@
 import javax.faces.context.FacesContext;
 import javax.faces.validator.ValidatorException;
 
-import org.apache.commons.validator.GenericValidator;
 import org.apache.myfaces.validator.ValidatorBase;
 
 /**
@@ -50,8 +49,11 @@
 	 * the maximum length check fails.</p>
 	 */
 	public static final String URL_MESSAGE_ID = "org.apache.myfaces.Url.INVALID";
+	
+	private org.apache.commons.validator.UrlValidator _urlValidator;
 
 	public UrlValidator(){
+	    _urlValidator = null;
 	}
 
 	/**
@@ -72,11 +74,132 @@
 			{
 				return;
 			}
-			if (!GenericValidator.isUrl(value.toString())) {
+			
+			if (_urlValidator == null){
+	            int options = 0;
+	            
+	            if (isAllow2Slashes())
+	            {
+	                options = options | org.apache.commons.validator.UrlValidator.ALLOW_2_SLASHES; 
+	            }
+	            
+	            if (isAllowAllSchemas())
+	            {
+	                options = options | org.apache.commons.validator.UrlValidator.ALLOW_ALL_SCHEMES;
+	            }
+	            
+	            String [] schemesList = getSchemesList(); 
+			    if (schemesList == null){
+			        _urlValidator = new 
+			            org.apache.commons.validator.UrlValidator(options);
+			    }
+			    else
+			    {
+                    _urlValidator = new 
+                        org.apache.commons.validator.UrlValidator(schemesList,options);
+			    }			     
+			}
+			
+			if (!_urlValidator.isValid(value.toString())) {
 				Object[] args = {value.toString()};
 				throw new ValidatorException(getFacesMessage(URL_MESSAGE_ID, args));
             }
 
 	}
+	
+	private String[] getSchemesList(){
+	    if (_schemes == null)
+	    {
+	        return null;
+	    }
+	    String [] list = _schemes.split(",");
+	    String [] resp = new String [list.length];
+	    
+	    for (int i = 0; i < list.length; i++)
+	    {
+	        resp[i] = list[i].trim();
+	    }	    
+	    return resp;	    
+	}
+
+    private String _schemes;
+    
+    private boolean _allow2Slashes = false;
+    
+    private boolean _allowAllSchemas = false;
+    	
+	public void setSchemes(String _schemes)
+    {
+        this._schemes = _schemes;
+        _urlValidator =  null;
+    }
+
+	/**
+	 *  CSV values that indicates the set of schemes to check this url.
+	 *  
+	 *  If allowAllSchemas = true, the values of this field are ignored.
+	 * 
+	 *  If no schemes are provided, default to this set ("http", "https", "ftp").
+	 * 
+	 * @JSFProperty
+	 */
+    public String getSchemes()
+    {
+        return _schemes;
+    }
+
+    public void setAllow2Slashes(boolean _allow2Slashes)
+    {
+        this._allow2Slashes = _allow2Slashes;
+        _urlValidator =  null;
+    }
+
+    /**
+     *  Allow two slashes in the path component of the URL.
+     * 
+     * @JSFProperty
+     */
+    public boolean isAllow2Slashes()
+    {
+        return _allow2Slashes;
+    }
+
+    public void setAllowAllSchemas(boolean _allowAllSchemas)
+    {
+        this._allowAllSchemas = _allowAllSchemas;
+        _urlValidator = null;
+    }
+
+    /**
+     *  Allows all validly formatted schemes to pass validation instead of 
+     *  supplying a set of valid schemes.
+     *  
+     * @JSFProperty
+     */
+    public boolean isAllowAllSchemas()
+    {
+        return _allowAllSchemas;
+    }
+
+    // -------------------------------------------------------- StateholderIF
+
+    public Object saveState(FacesContext context) {
+        Object[] state = new Object[5];
+        state[0] = super.saveState(context);
+        state[1] = _schemes;
+        state[2] = Boolean.valueOf(_allow2Slashes);
+        state[3] = saveAttachedState(context, _urlValidator);
+        state[4] = Boolean.valueOf(_allowAllSchemas);
+        return state;
+    }
+
+    public void restoreState(FacesContext context, Object state) {
+        Object values[] = (Object[])state;
+        super.restoreState(context, values[0]);
+        _schemes = (String)values[1];
+        _allow2Slashes = ((Boolean)values[2]).booleanValue();
+        _urlValidator = (org.apache.commons.validator.UrlValidator) restoreAttachedState(context, values[3]);
+        _allowAllSchemas = ((Boolean)values[4]).booleanValue();
+    }
 
 }