Example usage for org.apache.commons.validator UrlValidator ALLOW_ALL_SCHEMES

List of usage examples for org.apache.commons.validator UrlValidator ALLOW_ALL_SCHEMES

Introduction

In this page you can find the example usage for org.apache.commons.validator UrlValidator ALLOW_ALL_SCHEMES.

Prototype

int ALLOW_ALL_SCHEMES

To view the source code for org.apache.commons.validator UrlValidator ALLOW_ALL_SCHEMES.

Click Source Link

Document

Allows all validly formatted schemes to pass validation instead of supplying a set of valid schemes.

Usage

From source file:de.knurt.fam.template.util.TemplateHtml.java

/** construct TemplateHtml */
private TemplateHtml() {
    String[] schemes = { "http", "https" };
    urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_ALL_SCHEMES);
}

From source file:com.serli.maven.plugin.quality.mojo.LicenseMojo.java

/**
 * @param project/*  w w  w .j av  a 2s .co m*/
 *          not null
 * @param url
 *          not null
 * @return a valid URL object from the url string
 * @throws IOException
 *           if any
 */
protected static URL getLicenseURL(MavenProject project, String url) throws IOException {
    URL licenseUrl = null;
    UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_ALL_SCHEMES);
    // UrlValidator does not accept file URLs because the file
    // URLs do not contain a valid authority (no hostname).
    // As a workaround accept license URLs that start with the
    // file scheme.
    if (urlValidator.isValid(url) || StringUtils.defaultString(url).startsWith("file://")) {
        try {
            licenseUrl = new URL(url);
        } catch (MalformedURLException e) {
            throw new MalformedURLException(
                    "The license url '" + url + "' seems to be invalid: " + e.getMessage());
        }
    } else {
        File licenseFile = new File(project.getBasedir(), url);
        if (!licenseFile.exists()) {
            // Workaround to allow absolute path names while
            // staying compatible with the way it was...
            licenseFile = new File(url);
        }
        if (!licenseFile.exists()) {
            throw new IOException("Maven can't find the file '" + licenseFile + "' on the system.");
        }
        try {
            licenseUrl = licenseFile.toURI().toURL();
        } catch (MalformedURLException e) {
            throw new MalformedURLException(
                    "The license url '" + url + "' seems to be invalid: " + e.getMessage());
        }
    }

    return licenseUrl;
}

From source file:jp.terasoluna.fw.validation.ValidationUtil.java

/**
 * ?URL??????????/* w w  w. j  ava  2 s .  c o  m*/
 *
 * <code>null</code> ???????
 *
 * @param value 
 * @param allowallschemes ????????
 * @param allow2slashes ??????
 * @param nofragments URL??????
 * @param schemesVar ??
 * ?????
 * @return
 *            ?URL??????
 *            <code>true</code>?
 *            ?????<code>false</code>?
 */
public static boolean isUrl(String value, boolean allowallschemes, boolean allow2slashes, boolean nofragments,
        String schemesVar) {

    if (StringUtils.isEmpty(value)) {
        return true;
    }

    // ?
    int options = 0;
    if (allowallschemes) {
        options += UrlValidator.ALLOW_ALL_SCHEMES;
    }
    if (allow2slashes) {
        options += UrlValidator.ALLOW_2_SLASHES;
    }
    if (nofragments) {
        options += UrlValidator.NO_FRAGMENTS;
    }

    // ??????GenericValidator
    if (options == 0 && schemesVar == null) {
        if (GenericValidator.isUrl(value)) {
            return true;
        }
        return false;
    }

    // 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();
        }
    }

    // ????UrlValidator
    UrlValidator urlValidator = new UrlValidator(schemes, options);
    if (urlValidator.isValid(value)) {
        return true;
    }
    return false;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a field has a valid url. Four optional variables can be
 * specified to configure url validation.
 *
 * <ul>//w  ww  .  j  a v  a  2  s . c  o  m
 *
 * <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.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if valid, false otherwise.
 */
public static boolean validateUrl(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        return true;
    }

    // Get the options and schemes Vars
    String allowallschemesVar = Resources.getVarValue("allowallschemes", field, validator, request, false);
    boolean allowallschemes = "true".equalsIgnoreCase(allowallschemesVar);
    int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES : 0;

    String allow2slashesVar = Resources.getVarValue("allow2slashes", field, validator, request, false);

    if ("true".equalsIgnoreCase(allow2slashesVar)) {
        options += UrlValidator.ALLOW_2_SLASHES;
    }

    String nofragmentsVar = Resources.getVarValue("nofragments", field, validator, request, false);

    if ("true".equalsIgnoreCase(nofragmentsVar)) {
        options += UrlValidator.NO_FRAGMENTS;
    }

    String schemesVar = allowallschemes ? null
            : Resources.getVarValue("schemes", field, validator, request, false);

    // 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(validator, 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(validator, request, va, field));

        return false;
    }
}