Search in sources :

Example 1 with UrlValidator

use of org.apache.commons.validator.routines.UrlValidator in project ORCID-Source by ORCID.

the class DeveloperToolsController method validateRedirectUri.

/**
     * Checks if a redirect uri contains a valid URI associated to it
     * 
     * @param redirectUri
     * @return null if there are no errors, an List of strings containing error
     *         messages if any error happens
     * */
private List<String> validateRedirectUri(RedirectUri redirectUri) {
    List<String> errors = null;
    String[] schemes = { "http", "https" };
    UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS);
    if (!PojoUtil.isEmpty(redirectUri.getValue())) {
        try {
            String redirectUriString = redirectUri.getValue().getValue();
            if (!urlValidator.isValid(redirectUriString)) {
                errors = new ArrayList<String>();
                errors.add(getMessage("manage.developer_tools.invalid_redirect_uri"));
            }
        } catch (NullPointerException npe) {
            errors = new ArrayList<String>();
            errors.add(getMessage("manage.developer_tools.empty_redirect_uri"));
        }
    } else {
        errors = new ArrayList<String>();
        errors.add(getMessage("manage.developer_tools.empty_redirect_uri"));
    }
    return errors;
}
Also used : UrlValidator(org.apache.commons.validator.routines.UrlValidator) ArrayList(java.util.ArrayList)

Example 2 with UrlValidator

use of org.apache.commons.validator.routines.UrlValidator in project Asqatasun by Asqatasun.

the class SignUpFormValidator method checkSiteUrl.

/**
     *
     * @param userSubscriptionCommand
     * @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;
}
Also used : UrlValidator(org.apache.commons.validator.routines.UrlValidator)

Example 3 with UrlValidator

use of org.apache.commons.validator.routines.UrlValidator in project ice by JBEI.

the class WebPartners method isValidUrl.

/**
     * Validates the url by pre-pending "https://" as the scheme
     *
     * @param url without scheme
     * @return true if url validates successfully, false otherwise
     */
protected boolean isValidUrl(String url) {
    url = "https://" + url;
    UrlValidator validator = new UrlValidator();
    return validator.isValid(url);
}
Also used : UrlValidator(org.apache.commons.validator.routines.UrlValidator)

Example 4 with UrlValidator

use of org.apache.commons.validator.routines.UrlValidator in project ORCID-Source by ORCID.

the class DeveloperToolsController method validateSSOCredentials.

/**
     * Validates the ssoCredentials object
     * 
     * @param ssoCredentials
     * @return true if any error is found in the ssoCredentials object
     * */
private boolean validateSSOCredentials(SSOCredentials ssoCredentials) {
    boolean hasErrors = false;
    Set<RedirectUri> redirectUris = ssoCredentials.getRedirectUris();
    if (PojoUtil.isEmpty(ssoCredentials.getClientName())) {
        if (ssoCredentials.getClientName() == null) {
            ssoCredentials.setClientName(new Text());
        }
        ssoCredentials.getClientName().setErrors(Arrays.asList(getMessage("manage.developer_tools.name_not_empty")));
        hasErrors = true;
    } else if (ssoCredentials.getClientName().getValue().length() > CLIENT_NAME_LENGTH) {
        ssoCredentials.getClientName().setErrors(Arrays.asList(getMessage("manage.developer_tools.name_too_long")));
        hasErrors = true;
    } else if (OrcidStringUtils.hasHtml(ssoCredentials.getClientName().getValue())) {
        ssoCredentials.getClientName().setErrors(Arrays.asList(getMessage("manage.developer_tools.name.html")));
        hasErrors = true;
    } else {
        ssoCredentials.getClientName().setErrors(new ArrayList<String>());
    }
    if (PojoUtil.isEmpty(ssoCredentials.getClientDescription())) {
        if (ssoCredentials.getClientDescription() == null) {
            ssoCredentials.setClientDescription(new Text());
        }
        ssoCredentials.getClientDescription().setErrors(Arrays.asList(getMessage("manage.developer_tools.description_not_empty")));
        hasErrors = true;
    } else if (OrcidStringUtils.hasHtml(ssoCredentials.getClientDescription().getValue())) {
        ssoCredentials.getClientDescription().setErrors(Arrays.asList(getMessage("manage.developer_tools.description.html")));
        hasErrors = true;
    } else {
        ssoCredentials.getClientDescription().setErrors(new ArrayList<String>());
    }
    if (PojoUtil.isEmpty(ssoCredentials.getClientWebsite())) {
        if (ssoCredentials.getClientWebsite() == null) {
            ssoCredentials.setClientWebsite(new Text());
        }
        ssoCredentials.getClientWebsite().setErrors(Arrays.asList(getMessage("manage.developer_tools.website_not_empty")));
        hasErrors = true;
    } else {
        List<String> errors = new ArrayList<String>();
        String[] schemes = { "http", "https", "ftp" };
        UrlValidator urlValidator = new UrlValidator(schemes);
        String websiteString = ssoCredentials.getClientWebsite().getValue();
        if (!urlValidator.isValid(websiteString))
            websiteString = "http://" + websiteString;
        // test validity again
        if (!urlValidator.isValid(websiteString)) {
            errors.add(getMessage("manage.developer_tools.invalid_website"));
        }
        ssoCredentials.getClientWebsite().setErrors(errors);
    }
    if (redirectUris == null || redirectUris.isEmpty()) {
        List<String> errors = new ArrayList<String>();
        errors.add(getMessage("manage.developer_tools.at_least_one"));
        ssoCredentials.setErrors(errors);
        hasErrors = true;
    } else {
        for (RedirectUri redirectUri : redirectUris) {
            List<String> errors = validateRedirectUri(redirectUri);
            if (errors != null) {
                redirectUri.setErrors(errors);
                hasErrors = true;
            }
        }
    }
    return hasErrors;
}
Also used : ArrayList(java.util.ArrayList) UrlValidator(org.apache.commons.validator.routines.UrlValidator) RedirectUri(org.orcid.pojo.ajaxForm.RedirectUri) Text(org.orcid.pojo.ajaxForm.Text)

Example 5 with UrlValidator

use of org.apache.commons.validator.routines.UrlValidator in project Asqatasun by Asqatasun.

the class CreateContractFormValidator method checkContractUrl.

/**
     *
     * @param userSubscriptionCommand
     * @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, new RegexValidator(".*"), validatorOptions);
    if (!urlValidator.isValid(url)) {
        errors.rejectValue(CONTRACT_URL_KEY, INVALID_URL_KEY);
        return false;
    }
    return true;
}
Also used : RegexValidator(org.apache.commons.validator.routines.RegexValidator) UrlValidator(org.apache.commons.validator.routines.UrlValidator)

Aggregations

UrlValidator (org.apache.commons.validator.routines.UrlValidator)7 ArrayList (java.util.ArrayList)2 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RegexValidator (org.apache.commons.validator.routines.RegexValidator)1 RedirectUri (org.orcid.pojo.ajaxForm.RedirectUri)1 Text (org.orcid.pojo.ajaxForm.Text)1