Coverage Report - org.apache.tapestry.valid.UrlValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlValidator
0%
0/99
0%
0/42
2.435
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.valid;
 16  
 
 17  
 import java.net.MalformedURLException;
 18  
 import java.net.URL;
 19  
 import java.util.Collection;
 20  
 import java.util.HashMap;
 21  
 import java.util.Iterator;
 22  
 import java.util.Locale;
 23  
 import java.util.Map;
 24  
 import java.util.ResourceBundle;
 25  
 import java.util.Vector;
 26  
 
 27  
 import org.apache.tapestry.IMarkupWriter;
 28  
 import org.apache.tapestry.IRequestCycle;
 29  
 import org.apache.tapestry.form.IFormComponent;
 30  
 import org.apache.tapestry.util.StringSplitter;
 31  
 
 32  
 /**
 33  
  * @since 3.0
 34  
  */
 35  
 public class UrlValidator extends BaseValidator
 36  
 {
 37  
 
 38  
     private int _minimumLength;
 39  
 
 40  
     private String _minimumLengthMessage;
 41  
 
 42  
     private String _invalidUrlFormatMessage;
 43  
 
 44  
     private String _disallowedProtocolMessage;
 45  
 
 46  
     private Collection _allowedProtocols;
 47  
 
 48  0
     private String _scriptPath = "/org/apache/tapestry/valid/UrlValidator.script"; //$NON-NLS-1$
 49  
 
 50  
     public UrlValidator()
 51  0
     {
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Initializes the UrlValidator with properties defined by the initializer.
 56  
      * 
 57  
      * @since 4.0
 58  
      */
 59  
 
 60  
     public UrlValidator(String initializer)
 61  
     {
 62  0
         super(initializer);
 63  0
     }
 64  
 
 65  
     public String toString(IFormComponent field, Object value)
 66  
     {
 67  0
         if (value == null) return null;
 68  
 
 69  0
         return value.toString();
 70  
     }
 71  
 
 72  
     public Object toObject(IFormComponent field, String input)
 73  
         throws ValidatorException
 74  
     {
 75  0
         if (checkRequired(field, input)) return null;
 76  
 
 77  0
         if (_minimumLength > 0 && input.length() < _minimumLength)
 78  0
             throw new ValidatorException(buildMinimumLengthMessage(field),
 79  
                     ValidationConstraint.MINIMUM_WIDTH);
 80  
 
 81  0
         if (!isValidUrl(input))
 82  0
             throw new ValidatorException(buildInvalidUrlFormatMessage(field),
 83  
                     ValidationConstraint.URL_FORMAT);
 84  
 
 85  0
         if (!isAllowedProtocol(input)) { throw new ValidatorException(
 86  
                 buildDisallowedProtocolMessage(field),
 87  
                 ValidationConstraint.DISALLOWED_PROTOCOL); }
 88  
 
 89  0
         return input;
 90  
     }
 91  
 
 92  
     public int getMinimumLength()
 93  
     {
 94  0
         return _minimumLength;
 95  
     }
 96  
 
 97  
     public void setMinimumLength(int minimumLength)
 98  
     {
 99  0
         _minimumLength = minimumLength;
 100  0
     }
 101  
 
 102  
     public void renderValidatorContribution(IFormComponent field,
 103  
             IMarkupWriter writer, IRequestCycle cycle)
 104  
     {
 105  0
         if (!isClientScriptingEnabled()) return;
 106  
 
 107  0
         Map symbols = new HashMap();
 108  
 
 109  0
         if (isRequired())
 110  0
             symbols.put("requiredMessage", buildRequiredMessage(field)); //$NON-NLS-1$
 111  
 
 112  0
         if (_minimumLength > 0) symbols.put("minimumLengthMessage", //$NON-NLS-1$
 113  
                 buildMinimumLengthMessage(field));
 114  
 
 115  0
         symbols.put("urlFormatMessage", buildInvalidUrlFormatMessage(field)); //$NON-NLS-1$
 116  
 
 117  0
         symbols.put("urlDisallowedProtocolMessage", //$NON-NLS-1$
 118  
                 buildDisallowedProtocolMessage(field));
 119  
 
 120  0
         symbols.put("urlRegexpProtocols", buildUrlRegexpProtocols()); //$NON-NLS-1$
 121  
 
 122  0
         processValidatorScript(_scriptPath, cycle, field, symbols);
 123  0
     }
 124  
 
 125  
     private String buildUrlRegexpProtocols()
 126  
     {
 127  0
         if (_allowedProtocols == null) { return null; }
 128  0
         String regexp = "/("; //$NON-NLS-1$
 129  0
         Iterator iter = _allowedProtocols.iterator();
 130  0
         while(iter.hasNext())
 131  
         {
 132  0
             String protocol = (String) iter.next();
 133  0
             regexp += protocol;
 134  0
             if (iter.hasNext())
 135  
             {
 136  0
                 regexp += "|"; //$NON-NLS-1$
 137  
             }
 138  0
         }
 139  0
         regexp += "):///"; //$NON-NLS-1$
 140  0
         return regexp;
 141  
     }
 142  
 
 143  
     public String getScriptPath()
 144  
     {
 145  0
         return _scriptPath;
 146  
     }
 147  
 
 148  
     public void setScriptPath(String scriptPath)
 149  
     {
 150  0
         _scriptPath = scriptPath;
 151  0
     }
 152  
 
 153  
     protected boolean isValidUrl(String url)
 154  
     {
 155  
         boolean bIsValid;
 156  
         try
 157  
         {
 158  0
             new URL(url);
 159  0
             bIsValid = true;
 160  
         }
 161  0
         catch (MalformedURLException mue)
 162  
         {
 163  0
             bIsValid = false;
 164  0
         }
 165  0
         return bIsValid;
 166  
     }
 167  
 
 168  
     protected boolean isAllowedProtocol(String url)
 169  
     {
 170  0
         boolean bIsAllowed = false;
 171  0
         if (_allowedProtocols != null)
 172  
         {
 173  
             URL oUrl;
 174  
             try
 175  
             {
 176  0
                 oUrl = new URL(url);
 177  
             }
 178  0
             catch (MalformedURLException e)
 179  
             {
 180  0
                 return false;
 181  0
             }
 182  0
             String actualProtocol = oUrl.getProtocol();
 183  0
             Iterator iter = _allowedProtocols.iterator();
 184  0
             while(iter.hasNext())
 185  
             {
 186  0
                 String protocol = (String) iter.next();
 187  0
                 if (protocol.equals(actualProtocol))
 188  
                 {
 189  0
                     bIsAllowed = true;
 190  0
                     break;
 191  
                 }
 192  0
             }
 193  0
         }
 194  
         else
 195  
         {
 196  0
             bIsAllowed = true;
 197  
         }
 198  0
         return bIsAllowed;
 199  
     }
 200  
 
 201  
     public String getInvalidUrlFormatMessage()
 202  
     {
 203  0
         return _invalidUrlFormatMessage;
 204  
     }
 205  
 
 206  
     public String getMinimumLengthMessage()
 207  
     {
 208  0
         return _minimumLengthMessage;
 209  
     }
 210  
 
 211  
     public void setInvalidUrlFormatMessage(String string)
 212  
     {
 213  0
         _invalidUrlFormatMessage = string;
 214  0
     }
 215  
 
 216  
     public String getDisallowedProtocolMessage()
 217  
     {
 218  0
         return _disallowedProtocolMessage;
 219  
     }
 220  
 
 221  
     public void setDisallowedProtocolMessage(String string)
 222  
     {
 223  0
         _disallowedProtocolMessage = string;
 224  0
     }
 225  
 
 226  
     public void setMinimumLengthMessage(String string)
 227  
     {
 228  0
         _minimumLengthMessage = string;
 229  0
     }
 230  
 
 231  
     protected String buildMinimumLengthMessage(IFormComponent field)
 232  
     {
 233  0
         String pattern = getPattern(_minimumLengthMessage, "field-too-short", //$NON-NLS-1$
 234  
                 field.getPage().getLocale());
 235  
 
 236  0
         return formatString(pattern, Integer.toString(_minimumLength), field
 237  
                 .getDisplayName());
 238  
     }
 239  
 
 240  
     protected String buildInvalidUrlFormatMessage(IFormComponent field)
 241  
     {
 242  0
         String pattern = getPattern(_invalidUrlFormatMessage,
 243  
                 "invalid-url-format", //$NON-NLS-1$
 244  
                 field.getPage().getLocale());
 245  
 
 246  0
         return formatString(pattern, field.getDisplayName());
 247  
     }
 248  
 
 249  
     protected String buildDisallowedProtocolMessage(IFormComponent field)
 250  
     {
 251  0
         if (_allowedProtocols == null) { return null; }
 252  0
         String pattern = getPattern(_disallowedProtocolMessage,
 253  
                 "disallowed-protocol", //$NON-NLS-1$
 254  
                 field.getPage().getLocale());
 255  
 
 256  0
         String allowedProtocols = ""; //$NON-NLS-1$
 257  0
         Iterator iter = _allowedProtocols.iterator();
 258  0
         while(iter.hasNext())
 259  
         {
 260  0
             String protocol = (String) iter.next();
 261  0
             if (!allowedProtocols.equals("")) { //$NON-NLS-1$
 262  0
                 if (iter.hasNext())
 263  
                 {
 264  0
                     allowedProtocols += ", "; //$NON-NLS-1$
 265  
                 }
 266  
                 else
 267  
                 {
 268  0
                     allowedProtocols += " or "; //$NON-NLS-1$
 269  
                 }
 270  
             }
 271  0
             allowedProtocols += protocol;
 272  0
         }
 273  
 
 274  0
         return formatString(pattern, allowedProtocols);
 275  
     }
 276  
 
 277  
     protected String getPattern(String override, String key, Locale locale)
 278  
     {
 279  0
         if (override != null) return override;
 280  
 
 281  0
         ResourceBundle strings = ResourceBundle.getBundle(
 282  
                 "org.apache.tapestry.valid.ValidationStrings", locale);
 283  0
         return strings.getString(key);
 284  
     }
 285  
 
 286  
     /**
 287  
      * @param protocols
 288  
      *            comma separated list of allowed protocols
 289  
      */
 290  
     public void setAllowedProtocols(String protocols)
 291  
     {
 292  0
         StringSplitter spliter = new StringSplitter(',');
 293  
         // String[] aProtocols = protocols.split(","); //$NON-NLS-1$
 294  0
         String[] aProtocols = spliter.splitToArray(protocols); //$NON-NLS-1$
 295  0
         _allowedProtocols = new Vector();
 296  0
         for(int i = 0; i < aProtocols.length; i++)
 297  
         {
 298  0
             _allowedProtocols.add(aProtocols[i]);
 299  
         }
 300  0
     }
 301  
 
 302  
 }