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

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

Introduction

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

Prototype

public UrlValidator(RegexValidator authorityValidator, long options) 

Source Link

Document

Initialize a UrlValidator with the given validation options.

Usage

From source file:org.apache.cxf.fediz.service.oidc.clients.ClientRegistrationService.java

private boolean isValidURI(String uri, boolean requireHttps) {

    UrlValidator urlValidator = null;/*from  w  w w. j a  va 2s  .c om*/

    if (requireHttps) {
        String[] schemes = { "https" };
        urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS);
    } else {
        urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS + UrlValidator.ALLOW_ALL_SCHEMES);
    }

    if (!urlValidator.isValid(uri)) {
        return false;
    }

    // Do additional checks on the URI
    try {
        URI parsedURI = new URI(uri);
        // The URI can't have a fragment according to the OAuth 2.0 spec (+ audience spec)
        if (parsedURI.getFragment() != null) {
            return false;
        }
    } catch (URISyntaxException ex) {
        return false;
    }

    return true;
}

From source file:org.apache.rave.portal.web.validator.WidgetValidator.java

public WidgetValidator() {
    super();/*from   ww w .  j  a  va  2s. co m*/
    RegexValidator regex = new RegexValidator(new String[] { "http", "https", "((localhost)(:[0-9]+))" });
    urlValidator = new UrlValidator(regex, 0);
}

From source file:org.apache.rave.portal.web.validator.WidgetValidatorTest.java

@Test
public void testValidation() throws Exception {
    RegexValidator regex = new RegexValidator(
            new String[] { "http", "https", "((localhost)(:[0-9]+))", ".*\\.linux-server(:[0-9]+)" });
    UrlValidator validator = new UrlValidator(regex, 0);
    assertTrue("localhost URL should validate",
            validator.isValid("http://localhost:8080/demogadgets/CTSSResourcesMapView.xml"));
    assertTrue("127.0.0.1 should validate",
            validator.isValid("http://127.0.0.1:8080/demogadgets/CTSSResourcesMapView.xml"));
    assertTrue("my.linux-server should validate",
            validator.isValid("http://my.linux-server:8080/demogadgets/CTSSResourcesMapView.xml"));

    assertFalse("broke.my-test should not validate", validator.isValid("http://broke.my-test/test/index.html"));

    assertTrue("www.apache.org should still validate",
            validator.isValid("http://www.apache.org/test/index.html"));
}

From source file:org.apache.stratos.cli.StratosApplication.java

/**
 * @return {@code true} if required properties are loaded
 *//*w w  w .j  a v a 2s.com*/
private boolean loadRequiredProperties() {
    if (logger.isDebugEnabled()) {
        logger.debug("Loading properties...");
    }
    // Load properties
    String stratosURL = null;
    String username = null;
    String password = null;

    stratosURL = System.getenv(CliConstants.STRATOS_URL_ENV_PROPERTY);
    username = System.getenv(CliConstants.STRATOS_USERNAME_ENV_PROPERTY);
    password = System.getenv(CliConstants.STRATOS_PASSWORD_ENV_PROPERTY);

    int slashCount = StringUtils.countMatches(stratosURL, "/");
    int colonCount = StringUtils.countMatches(stratosURL, ":");

    if (!(colonCount == 2 && (slashCount == 3 || slashCount == 2))) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invalid STRATOS_URL");
        }

        System.out.println("Invalid STRATOS_URL. Please enter correct STRATOS_URL");
        return false;
    }
    if (StringUtils.isBlank(stratosURL)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Required configuration not found.");
        }
        // Stratos Controller details are not set.
        System.out.format("Could not find required \"%s\" variable in your environment.%n",
                CliConstants.STRATOS_URL_ENV_PROPERTY);
        return false;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Required configuration found. Validating {}", stratosURL);
        }
        UrlValidator urlValidator = new UrlValidator(new String[] { "https" }, UrlValidator.ALLOW_LOCAL_URLS);
        if (!urlValidator.isValid(stratosURL)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Stratos Controller URL {} is not valid", stratosURL);
            }
            System.out.format(
                    "The \"%s\" variable in your environment is not a valid URL. You have provided \"%s\".%n"
                            + "Please provide the Stratos Controller URL as follows%nhttps://<host>:<port>%n",
                    CliConstants.STRATOS_URL_ENV_PROPERTY, stratosURL);
            return false;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Stratos Controller URL {} is valid.", stratosURL);
            logger.debug("Adding the values to context.");
        }
        context.put(CliConstants.STRATOS_URL_ENV_PROPERTY, stratosURL);
        context.put(CliConstants.STRATOS_USERNAME_ENV_PROPERTY, username);
        context.put(CliConstants.STRATOS_PASSWORD_ENV_PROPERTY, password);
        return true;
    }
}

From source file:org.asqatasun.webapp.validator.CreateContractFormValidator.java

/**
 *
 * @param userSubscriptionCommand/*w ww.  j  a  va  2  s.  c  om*/
 * @param errors
 * @return
 */
private boolean checkContractUrl(CreateContractCommand createContractCommand, Errors errors) {
    String url = createContractCommand.getContractUrl().trim();
    if (StringUtils.isBlank(url)) {
        return true;
    }
    String[] schemes = { "http", "https" };
    long validatorOptions = UrlValidator.ALLOW_2_SLASHES + UrlValidator.ALLOW_LOCAL_URLS;
    UrlValidator urlValidator = new UrlValidator(schemes, validatorOptions);
    if (!urlValidator.isValid(url)) {
        errors.rejectValue(CONTRACT_URL_KEY, INVALID_URL_KEY);
        return false;
    }
    return true;
}

From source file:org.asqatasun.webapp.validator.CreateUserFormValidator.java

/**
 *
 * @param userSubscriptionCommand//from   ww w  . ja v  a  2s .  com
 * @param errors
 * @return
 */
private boolean checkSiteUrl(CreateUserCommand userSubscriptionCommand, Errors errors) {
    if (!checkSiteUrl) {
        return true;
    }
    if (userSubscriptionCommand.getSiteUrl() == null || userSubscriptionCommand.getSiteUrl().trim().isEmpty()) {
        errors.rejectValue(SITE_URL_KEY, MISSING_URL_KEY);
        return false;
    } else {
        String url = userSubscriptionCommand.getSiteUrl().trim();
        String[] schemes = { "http", "https" };
        UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_2_SLASHES);
        if (!urlValidator.isValid(url)) {
            errors.rejectValue(SITE_URL_KEY, INVALID_URL_KEY);
            return false;
        }
    }
    return true;
}

From source file:org.asqatasun.websnapshot.urlmanager.utils.UrlUtils.java

/**
 *
 * @param url/* w w  w. j a v a2  s . c  om*/
 * @return
 */
public static boolean checkIfURLIsValid(String url) {
    String[] schemes = { "http", "https" };
    UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_2_SLASHES);
    return urlValidator.isValid(url);
}

From source file:org.codehaus.griffon.runtime.validation.constraints.UrlConstraint.java

@Override
@SuppressWarnings("SuspiciousToArrayCall")
public void setParameter(@Nonnull Object constraintParameter) {
    RegexValidator domainValidator = null;

    if (constraintParameter instanceof Boolean) {
        url = (Boolean) constraintParameter;
    } else if (constraintParameter instanceof String) {
        url = true;/*from w ww . jav a 2 s .  com*/
        domainValidator = new RegexValidator((String) constraintParameter);
    } else if (constraintParameter instanceof List<?>) {
        url = true;
        List<?> regexpList = (List<?>) constraintParameter;
        domainValidator = new RegexValidator(regexpList.toArray(new String[regexpList.size()]));
    } else {
        throw new IllegalArgumentException("Parameter for constraint [" + VALIDATION_DSL_NAME
                + "] of property [" + constraintPropertyName + "] of class [" + constraintOwningClass
                + "] must be a boolean, string, or list value");
    }

    validator = new UrlValidator(domainValidator,
            UrlValidator.ALLOW_ALL_SCHEMES + UrlValidator.ALLOW_2_SLASHES);

    super.setParameter(constraintParameter);
}

From source file:org.geoserver.security.iride.util.factory.util.UrlValidatorFactory.java

@Override
protected final UrlValidator newInstance() {
    return new UrlValidator(new String[] { "http", "https" },
            UrlValidator.NO_FRAGMENTS + UrlValidator.ALLOW_LOCAL_URLS);
}

From source file:org.geoserver.security.iride.util.factory.validator.UrlValidatorFactory.java

@Override
protected final UrlValidator newInstance() {
    return new UrlValidator(new String[] { "http", "https" }, UrlValidator.NO_FRAGMENTS);
}