Search in sources :

Example 1 with InetAddressValidator

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

the class OrcidUrlValidator method isValidAuthority.

protected boolean isValidAuthority(String authority) {
    if (authority == null) {
        return false;
    }
    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;
}
Also used : Matcher(java.util.regex.Matcher) InetAddressValidator(org.apache.commons.validator.routines.InetAddressValidator)

Example 2 with InetAddressValidator

use of org.apache.commons.validator.routines.InetAddressValidator in project java-client by appium.

the class ServerBuilderTest method getLocalIP.

private static String getLocalIP(NetworkInterface intf) {
    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
        InetAddress inetAddress = enumIpAddr.nextElement();
        if (!inetAddress.isLoopbackAddress()) {
            InetAddressValidator validator = InetAddressValidator.getInstance();
            String calculated = inetAddress.getHostAddress();
            if (validator.isValid(calculated)) {
                return calculated;
            }
        }
    }
    return null;
}
Also used : InetAddressValidator(org.apache.commons.validator.routines.InetAddressValidator) InetAddress(java.net.InetAddress)

Example 3 with InetAddressValidator

use of org.apache.commons.validator.routines.InetAddressValidator in project java-client by appium.

the class AppiumServiceBuilder method createArgs.

@Override
protected ImmutableList<String> createArgs() {
    List<String> argList = new ArrayList<>();
    checkAppiumJS();
    argList.add(appiumJS.getAbsolutePath());
    argList.add("--port");
    argList.add(String.valueOf(getPort()));
    if (StringUtils.isBlank(ipAddress)) {
        ipAddress = DEFAULT_LOCAL_IP_ADDRESS;
    } else {
        InetAddressValidator validator = InetAddressValidator.getInstance();
        if (!validator.isValid(ipAddress) && !validator.isValidInet4Address(ipAddress) && !validator.isValidInet6Address(ipAddress)) {
            throw new IllegalArgumentException("The invalid IP address " + ipAddress + " is defined");
        }
    }
    argList.add("--address");
    argList.add(ipAddress);
    File log = getLogFile();
    if (log != null) {
        argList.add("--log");
        argList.add(log.getAbsolutePath());
    }
    Set<Map.Entry<String, String>> entries = serverArguments.entrySet();
    for (Map.Entry<String, String> entry : entries) {
        String argument = entry.getKey();
        String value = entry.getValue();
        if (StringUtils.isBlank(argument) || value == null) {
            continue;
        }
        argList.add(argument);
        if (!StringUtils.isBlank(value)) {
            argList.add(value);
        }
    }
    if (capabilities != null) {
        argList.add("--default-capabilities");
        argList.add(parseCapabilities());
    }
    return new ImmutableList.Builder<String>().addAll(argList).build();
}
Also used : ArrayList(java.util.ArrayList) InetAddressValidator(org.apache.commons.validator.routines.InetAddressValidator) File(java.io.File) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

InetAddressValidator (org.apache.commons.validator.routines.InetAddressValidator)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1