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:url.Path.java

public String reverseDomains(String str) {
    String revDomain = "";
    try {//from w w  w  .  j  a va2s . com

        UrlValidator defaultValidator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
        //System.out.println("URL: "+str);
        if (defaultValidator.isValid("http://" + str)) {

            URL host_path = new URL("http://" + str);

            String host = host_path.getHost().toLowerCase();
            host = host.replace(",", "");
            String path = host_path.getPath();

            String out[] = host.split("\\.");
            boolean www = false;

            //  System.out.println("Host: "+host);
            // System.out.println("Path: "+path);
            for (int j = out.length - 1; j >= 0; j--) {
                if (out[j].trim().equals("WWW") || out[j].trim().equals("www")) {
                    www = true;
                } else {

                    revDomain += "\"" + out[j] + "\"" + "#";

                }
            }
            if (revDomain.endsWith("#")) {
                revDomain = revDomain.substring(0, revDomain.length() - 1);
            }
            //System.out.println("Host reverse: "+revDomain);
            if (www) {
                revDomain += "#" + "\"" + "www" + "\"";
                www = false;
            }
            revDomain += ":";
            String path_split[] = path.split("[/]");
            //System.out.println("Path Split: "+ Arrays.toString(path_split));
            for (int i = 1; i < path_split.length; i++) {

                revDomain += "\"" + path_split[i].replace(",", "") + "\"" + "#";
            }

            if (revDomain.endsWith("#")) {
                revDomain = revDomain.substring(0, revDomain.length() - 1);
            }

            //System.out.println("URL: "+str);
            //System.out.println("reverse URL: "+revDomain);
        } else {
            System.out.println("Not valid: " + str);
        }

    } catch (MalformedURLException ex) {
        //Logger.getLogger(URLprocess_path.class.getName()).log(Level.SEVERE, null, ex);
    }
    return revDomain;
}

From source file:ws.argo.probe.Probe.java

/**
 * Add a URL that specifies where the Responder should send the response.
 * Provide a label as a hint to the Responder about why this URL was included.
 * For example: give a "internal" label for a respondToURL that is only
 * accessible from inside a NATed network.
 * /*from w  w  w .  ja v  a2  s. c o  m*/
 * @param label - hint about the URL
 * @param respondToURL - the URL the responder will POST a payload to
 * @throws MalformedURLException if the URL is bad
 */
public void addRespondToURL(String label, String respondToURL) throws MalformedURLException {

    // Sanity check on the respondToURL
    // The requirement for the respondToURL is a REST POST call, so that means
    // only HTTP and HTTPS schemes.
    // Localhost is allowed as well as a valid response destination
    String[] schemes = { "http", "https" };
    UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS);
    if (!urlValidator.isValid(respondToURL))
        throw new MalformedURLException("The probe respondTo URL is invalid: " + respondToURL);

    _probe.addRespondToURL(label, respondToURL);

}