1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.myfaces.custom.urlvalidator;
21
22 import javax.faces.application.FacesMessage;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.validator.ValidatorException;
26
27 import org.apache.myfaces.validator.ValidatorBase;
28
29 /**
30 * A custom validator for url format, based upons Jakarta Commons.
31 *
32 * @JSFValidator
33 * name = "s:validateUrl"
34 * class = "org.apache.myfaces.custom.urlvalidator.UrlValidator"
35 * tagClass = "org.apache.myfaces.custom.urlvalidator.ValidateUrlTag"
36 * serialuidtag = "6041422002721046221L"
37 *
38 * @author Fabian Frederick
39 *
40 * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
41 */
42 public abstract class AbstractUrlValidator extends ValidatorBase {
43
44 /**
45 * <p>The standard converter id for this converter.</p>
46 */
47 public static final String VALIDATOR_ID = "org.apache.myfaces.validator.Url";
48 /**
49 * <p>The message identifier of the {@link FacesMessage} to be created if
50 * the maximum length check fails.</p>
51 */
52 public static final String URL_MESSAGE_ID = "org.apache.myfaces.Url.INVALID";
53
54 public AbstractUrlValidator(){
55
56 }
57
58 /**
59 * method that validates an url address.
60 * it uses the commons-validator
61 */
62 public void validate(
63 FacesContext facesContext,
64 UIComponent uiComponent,
65 Object value)
66 throws ValidatorException {
67
68
69 if (facesContext == null) throw new NullPointerException("facesContext");
70 if (uiComponent == null) throw new NullPointerException("uiComponent");
71
72 if (value == null)
73 {
74 return;
75 }
76
77 org.apache.commons.validator.UrlValidator urlValidator = initValidator();
78
79 if (!urlValidator.isValid(value.toString())) {
80 Object[] args = {value.toString()};
81 throw new ValidatorException(getFacesMessage(URL_MESSAGE_ID, args));
82 }
83
84 }
85
86 private org.apache.commons.validator.UrlValidator initValidator()
87 {
88 int options = 0;
89
90 if (isAllow2Slashes())
91 {
92 options = options | org.apache.commons.validator.UrlValidator.ALLOW_2_SLASHES;
93 }
94
95 if (isAllowAllSchemas())
96 {
97 options = options | org.apache.commons.validator.UrlValidator.ALLOW_ALL_SCHEMES;
98 }
99
100 String [] schemesList = getSchemesList();
101 org.apache.commons.validator.UrlValidator urlValidator = null;
102 if (schemesList == null){
103 urlValidator = new
104 org.apache.commons.validator.UrlValidator(options);
105 }
106 else
107 {
108 urlValidator = new
109 org.apache.commons.validator.UrlValidator(schemesList,options);
110 }
111 return urlValidator;
112 }
113
114 private String[] getSchemesList(){
115 if (getSchemes() == null)
116 {
117 return null;
118 }
119 String [] list = getSchemes().split(",");
120 String [] resp = new String [list.length];
121
122 for (int i = 0; i < list.length; i++)
123 {
124 resp[i] = list[i].trim();
125 }
126 return resp;
127 }
128
129 public abstract void setSchemes(String _schemes);
130
131 /**
132 * CSV values that indicates the set of schemes to check this url.
133 *
134 * If allowAllSchemas = true, the values of this field are ignored.
135 *
136 * If no schemes are provided, default to this set ("http", "https", "ftp").
137 *
138 * @JSFProperty
139 */
140 public abstract String getSchemes();
141
142 public abstract void setAllow2Slashes(boolean _allow2Slashes);
143
144 /**
145 * Allow two slashes in the path component of the URL.
146 *
147 * @JSFProperty
148 * defaultValue = "false"
149 */
150 public abstract boolean isAllow2Slashes();
151
152 public abstract void setAllowAllSchemas(boolean _allowAllSchemas);
153
154 /**
155 * Allows all validly formatted schemes to pass validation instead of
156 * supplying a set of valid schemes.
157 *
158 * @JSFProperty
159 * defaultValue = "false"
160 */
161 public abstract boolean isAllowAllSchemas();
162
163 }