You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by hu...@apache.org on 2004/04/16 02:08:17 UTC
cvs commit: jakarta-struts/src/share/org/apache/struts/validator FieldChecks.java
husted 2004/04/15 17:08:17
Modified: src/share/org/apache/struts/validator FieldChecks.java
Log:
Apply #28148 "Url Validation Configuration Options" submitted by Nial Pemberton.
Revision Changes Path
1.19 +69 -7 jakarta-struts/src/share/org/apache/struts/validator/FieldChecks.java
Index: FieldChecks.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/FieldChecks.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- FieldChecks.java 14 Mar 2004 06:23:47 -0000 1.18
+++ FieldChecks.java 16 Apr 2004 00:08:17 -0000 1.19
@@ -23,6 +23,7 @@
import java.io.Serializable;
import java.util.Date;
import java.util.Locale;
+import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
@@ -31,6 +32,7 @@
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
import org.apache.commons.validator.GenericValidator;
+import org.apache.commons.validator.UrlValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
@@ -827,7 +829,23 @@
}
/**
- * Checks if a field has a valid url.
+ * Checks if a field has a valid url. Four optional variables can be
+ * specified to configure url validation.
+ * <ul>
+ * <li>Variable <code>allow2slashes</code> can be set to <code>true</code> or
+ * <code>false</code> to control whether two slashes are allowed -
+ * default is <code>false</code> (i.e. two slashes are NOT allowed).</li>
+ * <li>Variable <code>nofragments</code> can be set to <code>true</code> or
+ * <code>false</code> to control whether fragments are allowed -
+ * default is <code>false</code> (i.e. fragments ARE allowed).</li>
+ * <li>Variable <code>allowallschemes</code> can be set to <code>true</code> or
+ * <code>false</code> to control if all schemes are allowed - default
+ * is <code>false</code> (i.e. all schemes are NOT allowed).</li>
+ * <li>Variable <code>schemes</code> can be set to a comma delimited list of
+ * valid schemes. This value is ignored if <code>allowallschemes</code>
+ * is set to <code>true</code>. Default schemes allowed are "http",
+ * "https" and "ftp" if this variable is not specified.</li>
+ * </ul>
*
* @param bean The bean validation is being performed on.
* @param va The <code>ValidatorAction</code> that is currently being performed.
@@ -850,11 +868,55 @@
value = ValidatorUtils.getValueAsString(bean, field.getProperty());
}
- if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isUrl(value)) {
+ if (GenericValidator.isBlankOrNull(value)) {
+ return true;
+ }
+
+ // Get the options and schemes Vars
+ boolean allowallschemes = "true".equalsIgnoreCase(field.getVarValue("allowallschemes"));
+ int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES : 0;
+
+ if ("true".equalsIgnoreCase(field.getVarValue("allow2slashes"))) {
+ options += UrlValidator.ALLOW_2_SLASHES;
+ }
+
+ if ("true".equalsIgnoreCase(field.getVarValue("nofragments"))) {
+ options += UrlValidator.NO_FRAGMENTS;
+ }
+
+ String schemesVar = allowallschemes ? null : field.getVarValue("schemes");
+
+ // No options or schemes - use GenericValidator as default
+ if (options == 0 && schemesVar == null) {
+ if (GenericValidator.isUrl(value)) {
+ return true;
+ } else {
+ errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
+ return false;
+ }
+ }
+
+ // Parse comma delimited list of schemes into a String[]
+ String[] schemes = null;
+ if (schemesVar != null) {
+
+ StringTokenizer st = new StringTokenizer(schemesVar, ",");
+ schemes = new String[st.countTokens()];
+
+ int i = 0;
+ while (st.hasMoreTokens()) {
+ schemes[i++] = st.nextToken().trim();
+ }
+
+ }
+
+ // Create UrlValidator and validate with options/schemes
+ UrlValidator urlValidator = new UrlValidator(schemes, options);
+ if (urlValidator.isValid(value)) {
+ return true;
+ } else {
errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
return false;
- } else {
- return true;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org