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

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

Introduction

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

Prototype

public boolean isValid(String value) 

Source Link

Document

Checks if a field has a valid url address.

Usage

From source file:org.sakaiproject.portlets.PortletIFrame.java

public boolean validateURL(String urlToValidate) {
    // return FormattedText.validateURL(urlToValidate); // KNL-1105
    if (StringUtils.isBlank(urlToValidate))
        return false;

    if (ABOUT_BLANK.equals(urlToValidate))
        return true;

    // Check if the url is "Escapable" - run through the URL-URI-URL gauntlet
    String escapedURL = sanitizeHrefURL(urlToValidate);
    if (escapedURL == null)
        return false;

    // For a protocol-relative URL, we validate with protocol attached 
    // RFC 1808 Section 4
    if ((urlToValidate.startsWith("//")) && (urlToValidate.indexOf("://") == -1)) {
        urlToValidate = PROTOCOL_PREFIX + urlToValidate;
    }//from  w  ww. ja  v  a2  s. c o m

    // For a site-relative URL, we validate with host name and protocol attached 
    // SAK-13787 SAK-23752
    if ((urlToValidate.startsWith("/")) && (urlToValidate.indexOf("://") == -1)) {
        urlToValidate = HOST_PREFIX + urlToValidate;
    }

    // Validate the url
    UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
    return urlValidator.isValid(urlToValidate);
}

From source file:org.sakaiproject.util.impl.FormattedTextImpl.java

public boolean validateURL(String urlToValidate) {
    if (StringUtils.isBlank(urlToValidate))
        return false;

    if (ABOUT_BLANK.equals(urlToValidate))
        return true;

    // Check if the url is "Escapable" - run through the URL-URI-URL gauntlet
    String escapedURL = sanitizeHrefURL(urlToValidate);
    if (escapedURL == null)
        return false;

    // For a protocol-relative URL, we validate with protocol attached 
    // RFC 1808 Section 4
    if ((urlToValidate.startsWith("//")) && (urlToValidate.indexOf("://") == -1)) {
        urlToValidate = PROTOCOL_PREFIX + urlToValidate;
    }//ww  w  .  j a v  a 2s .c  o  m

    // For a site-relative URL, we validate with host name and protocol attached 
    // SAK-13787 SAK-23752
    if ((urlToValidate.startsWith("/")) && (urlToValidate.indexOf("://") == -1)) {
        urlToValidate = HOST_PREFIX + urlToValidate;
    }

    // Validate the url
    UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
    return urlValidator.isValid(urlToValidate);
}

From source file:org.shadowmask.core.discovery.rules.UrlRule.java

@Override
public boolean evaluate() {
    if (value == null) {
        throw new DataDiscoveryException("Should fill the column value before fire inspect rules.");
    }//from   ww w .ja va 2  s.co  m

    String urlValue = value;
    if (!(value.startsWith("http://") || value.startsWith("https://") || value.startsWith("ftp://"))) {
        urlValue = "http://" + value;
    }
    UrlValidator urlValidator = new UrlValidator();
    return urlValidator.isValid(urlValue);
}

From source file:org.slc.sli.api.validation.impl.SimpleURLValidator.java

@Override
public boolean validate(URI url) {
    String[] schemes = { "http", "https" };
    UrlValidator validator = new UrlValidator(schemes);

    return validator.isValid(url.toString());
}

From source file:org.trustedanalytics.h2oscoringengine.publisher.restapi.validation.HostValidationRule.java

@Override
public void validate(MultiValueMap<String, String> request) {
    try {/*  ww  w. j  a  v  a 2 s  .c  o m*/
        formDataValidator.validateField(request, HOST_KEY);
        UrlValidator urlValidator = new UrlValidator();
        if (!urlValidator.isValid(request.get(HOST_KEY).get(0))) {
            throw new ValidationException("Url given in " + HOST_KEY + " form field is invalid");
        }

    } catch (IllegalArgumentException e) {
        throw new ValidationException(e);
    }
}

From source file:org.wattdepot.client.http.api.collector.MultiThreadedCollector.java

/**
 * @throws BadSensorUriException if the Sensor's URI isn't valid.
 * @throws IdNotFoundException if there is a problem with the Collector
 *         Process Definition.//from  www . j  a  va  2s  . c  om
 */
private void validate() throws BadSensorUriException, IdNotFoundException {
    Sensor s = client.getSensor(definition.getSensorId());
    String[] schemes = { "http", "https" };
    UrlValidator urlValidator = new UrlValidator(schemes);
    if (!urlValidator.isValid(s.getUri())) {
        throw new BadSensorUriException(s.getUri() + " is not a valid URI.");
    }
}

From source file:org.wso2.developerstudio.eclipse.platform.ui.validator.CommonFieldValidator.java

public static void isValidUrl(String url, String field) throws FieldValidationException {
    if (url.contains(":") && !url.startsWith(":")) {
        if (url.startsWith("http:") || url.startsWith("https:") || url.startsWith("ftp:")) {
            UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
            if (!urlValidator.isValid(url)) {
                if (!isParameter(url, true)) {
                    throw new FieldValidationException(field + ": Invalid URL provided");
                }//from  w w  w  .  j  av  a  2  s.c  om

            }
        }
    } else {
        if (url.startsWith(":")) {
            throw new FieldValidationException(field + ": Invalid URL provided");
        } else {
            if (!isParameter(url, false)) {
                throw new FieldValidationException(field + ": Invalid URL provided");
            }
        }
    }
}

From source file:org.zaizi.manifoldcf.agents.transformation.stanbol.StanbolEnhancer.java

private String getURILocalName(String uri) {
    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(uri)) {
        URIImpl uriImpl = new URIImpl(uri);
        return uriImpl.getLocalName();
    } else {/* ww w .j ava2 s . c om*/
        return uri;
    }
}

From source file:pt.ua.bioinformatics.coeus.nanopub.NanopubParser.java

/**
 * Do the nanopublication generation/parsing
 *
 *//*  w w w  . j a  v  a 2 s .c o m*/
public void parse() {

    for (String string : concept_childs) {
        System.err.println("\t[COEUS][NanopubParser] " + string);
    }
    Logger.getLogger(NanopubParser.class.getName()).log(Level.INFO, "[COEUS][NanopubParser] Start parsing: {0}",
            concept_root);

    Boot.start();

    String queryConcepts = "SELECT * {?item coeus:hasConcept " + concept_root + " FILTER NOT EXISTS {"
            + concept_root + " coeus:builtNp true }}";
    ResultSet rs = Boot.getAPI().selectRS(queryConcepts, false);
    if (!rs.hasNext() && Config.isDebug()) {
        Logger.getLogger(NanopubParser.class.getName()).log(Level.INFO,
                "[COEUS][NanopubParser] Already parsed the concept {0}", concept_root);
    }
    UrlValidator urlValidator = new UrlValidator();
    while (rs.hasNext()) {
        QuerySolution row = rs.next();

        String item = row.get("item").toString();
        //System.out.println(item);
        String queryItems = "SELECT * { <" + item + "> ?p ?o}";
        ResultSet rs_item = Boot.getAPI().selectRS(queryItems, false);

        //Build Assertion
        String np_item = item + "_Nanopub";
        Assertion a = new Assertion(np_item + "_Assertion");
        //Add Items to Assertion field
        while (rs_item.hasNext()) {
            QuerySolution row_item = rs_item.next();
            String p = row_item.get("p").toString();
            String o = row_item.get("o").toString();
            //System.out.println(item + " " + p + " " + o);

            if (urlValidator.isValid(o)) {
                a.add(new Triple(Node.createURI(item), Node.createURI(p), Node.createURI(o)));
            } else {
                a.add(new Triple(Node.createURI(item), Node.createURI(p), Node.createLiteral(o)));
            }
            if (p.endsWith("isAssociatedTo")) {
                a = addAssociations(a, o);
            }
        }

        //Build Provenance
        Provenance p = new Provenance(np_item + "_Provenance");
        p.add(new Triple(Node.createURI(a.getUri()), Node.createURI("http://www.w3.org/ns/prov#wasDerivedFrom"),
                Node.createURI("http://bioinformatics.ua.pt/coeus/")));
        for (JSONObject json : prov) {
            String predicate = (String) json.get("predicate");
            String object = (String) json.get("object");
            if (urlValidator.isValid(object)) {
                p.add(new Triple(Node.createURI(a.getUri()), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createURI(object)));
            } else {
                p.add(new Triple(Node.createURI(a.getUri()), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createLiteral(object)));
            }
        }

        //Build PublicationInfo
        PublicationInfo i = new PublicationInfo(np_item + "_PubInfo");
        i.add(new Triple(Node.createURI(np_item), Node.createURI("http://www.w3.org/ns/prov#generatedAtTime"),
                Node.createLiteral(new Date().toString(), null, XSDDatatype.XSDdateTime)));
        i.add(new Triple(Node.createURI(np_item), Node.createURI("http://www.w3.org/ns/prov#wasAttributedTo"),
                Node.createLiteral(Config.getName())));
        i.add(new Triple(Node.createURI(np_item), Node.createURI("http://www.w3.org/ns/prov#wasGeneratedBy"),
                Node.createURI(PrefixFactory.decode(concept_root))));
        for (JSONObject json : info) {
            String predicate = (String) json.get("predicate");
            String object = (String) json.get("object");
            if (urlValidator.isValid(object)) {
                i.add(new Triple(Node.createURI(np_item), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createURI(object)));
            } else {
                i.add(new Triple(Node.createURI(np_item), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createLiteral(object)));
            }
        }

        /**/
        Nanopublication np = new Nanopublication(np_item, a, p, i);
        //System.out.println(np.writeNQuads());
        Boot.getAPI().storeNanopub(np, concept_root);

    }
    save();
}

From source file:pt.ua.bioinformatics.coeus.nanopub.NanopubParser.java

/**
 * Add all objects (recursive function) with the property
 * (coeus:isAssociatedTo) to the Assertion
 *
 * @param a/*from   w w  w.  j a  va  2  s .  c  o m*/
 * @param object
 * @return
 */
public Assertion addAssociations(Assertion a, String object) {
    String queryItems = "SELECT * { <" + object + "> ?p ?o}";
    ResultSet rs_item = Boot.getAPI().selectRS(queryItems, false);
    ResultSet rs_item_test = Boot.getAPI().selectRS(queryItems, false);
    Boolean load = false;

    //Test if the data concept is to load into the assertion
    while (rs_item_test.hasNext()) {
        QuerySolution row_item = rs_item_test.next();
        String p = row_item.get("p").toString();
        String o = row_item.get("o").toString();

        if ((p.endsWith("hasConcept")) && (concept_childs.contains(PrefixFactory.encode(o).split(":")[1]))) {
            load = true;
            Logger.getLogger(NanopubParser.class.getName()).log(Level.INFO,
                    PrefixFactory.encode(o).split(":")[1]);
        }
    }

    while (rs_item.hasNext()) {
        QuerySolution row_item = rs_item.next();
        String p = row_item.get("p").toString();
        String o = row_item.get("o").toString();
        //Add all or add only the association
        if (load || p.endsWith("isAssociatedTo")) {
            UrlValidator urlValidator = new UrlValidator();
            if (urlValidator.isValid(o)) {
                a.add(new Triple(Node.createURI(object), Node.createURI(p), Node.createURI(o)));
            } else {
                a.add(new Triple(Node.createURI(object), Node.createURI(p), Node.createLiteral(o)));
            }
        }
        if (p.endsWith("isAssociatedTo")) {
            Triple t = new Triple(Node.createURI(o), Node.createURI(p), Node.createURI(object));
            if (!a.getContent().contains(t)) {
                a = addAssociations(a, o);
            }
        }
    }
    return a;
}