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

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

Introduction

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

Prototype

public boolean isValid(String inetAddress) 

Source Link

Document

Checks if the specified string is a valid IP address.

Usage

From source file:org.mule.modules.validation.ValidationModule.java

/**
 * If the specified <code>ipAddress</code> is not a valid one throw an exception.
 * <p/>//from  w w w .j  ava  2s  .co  m
 * {@sample.xml ../../../doc/mule-module-validation.xml.sample validation:validate-ip-address}
 *
 * @param ipAddress                IP address to validate
 * @param customExceptionClassName Class name of the exception to throw
 * @throws Exception if not valid
 */
@Processor
public void validateIpAddress(String ipAddress,
        @Optional @Default("org.mule.modules.validation.InvalidException") String customExceptionClassName)
        throws Exception {
    InetAddressValidator validator = InetAddressValidator.getInstance();

    if (!validator.isValid(ipAddress)) {
        throw buildException(customExceptionClassName);
    }
}

From source file:org.orcid.frontend.web.forms.validate.OrcidUrlValidator.java

protected boolean isValidAuthority(String authority) {
    if (authority == null) {
        return false;
    }//  ww w .  ja  v  a2s .c o m

    Matcher authorityMatcher = AUTHORITY_PATTERN.matcher(authority);
    if (!authorityMatcher.matches()) {
        return false;
    }

    String hostLocation = authorityMatcher.group(PARSE_AUTHORITY_HOST_IP);
    // check if authority is hostname or IP address:
    // try a hostname first since that's much more likely
    OrcidDomainValidator domainValidator = new OrcidDomainValidator();
    if (!domainValidator.isValid(hostLocation)) {
        // try an IP address
        InetAddressValidator inetAddressValidator = InetAddressValidator.getInstance();
        if (!inetAddressValidator.isValid(hostLocation)) {
            // isn't either one, so the URL is invalid
            return false;
        }
    }

    String port = authorityMatcher.group(PARSE_AUTHORITY_PORT);
    if (port != null) {
        if (!PORT_PATTERN.matcher(port).matches()) {
            return false;
        }
    }

    String extra = authorityMatcher.group(PARSE_AUTHORITY_EXTRA);
    if (extra != null && extra.trim().length() > 0) {
        return false;
    }

    return true;
}