Java Code Examples for org.apache.commons.validator.routines.InetAddressValidator#isValid()

The following examples show how to use org.apache.commons.validator.routines.InetAddressValidator#isValid() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OzoneSecurityUtil.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Iterates through network interfaces and return all valid ip's not
 * listed in CertificateSignRequest#INVALID_IPS.
 *
 * @return List<InetAddress>
 * @throws IOException if no network interface are found or if an error
 * occurs.
 */
public static List<InetAddress> getValidInetsForCurrentHost()
    throws IOException {
  List<InetAddress> hostIps = new ArrayList<>();
  InetAddressValidator ipValidator = InetAddressValidator.getInstance();

  Enumeration<NetworkInterface> enumNI =
      NetworkInterface.getNetworkInterfaces();
  if (enumNI == null) {
    throw new IOException("Unable to get network interfaces.");
  }

  while (enumNI.hasMoreElements()) {
    NetworkInterface ifc = enumNI.nextElement();
    if (ifc.isUp()) {
      Enumeration<InetAddress> enumAdds = ifc.getInetAddresses();
      while (enumAdds.hasMoreElements()) {
        InetAddress addr = enumAdds.nextElement();

        String hostAddress = addr.getHostAddress();
        if (!INVALID_IPS.contains(hostAddress)
            && ipValidator.isValid(hostAddress)) {
          LOG.info("Adding ip:{},host:{}", hostAddress, addr.getHostName());
          hostIps.add(addr);
        } else {
          LOG.info("ip:{} not returned.", hostAddress);
        }
      }
    }
  }

  return hostIps;
}
 
Example 2
Source File: PackageData.java    From aliyun-log-producer-java with Apache License 2.0 5 votes vote down vote up
static String GetLocalMachineIp() {
    InetAddressValidator validator = new InetAddressValidator();
    String candidate = "";
    try {
        for (Enumeration<NetworkInterface> ifaces = NetworkInterface
                .getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
            NetworkInterface iface = ifaces.nextElement();

            if (iface.isUp()) {
                for (Enumeration<InetAddress> addresses = iface
                        .getInetAddresses(); addresses.hasMoreElements(); ) {

                    InetAddress address = addresses.nextElement();

                    if (!address.isLinkLocalAddress() && address.getHostAddress() != null) {
                        String ipAddress = address.getHostAddress();
                        if (ipAddress.equals(Consts.CONST_LOCAL_IP)) {
                            continue;
                        }
                        if (validator.isValidInet4Address(ipAddress)) {
                            return ipAddress;
                        }
                        if (validator.isValid(ipAddress)) {
                            candidate = ipAddress;
                        }
                    }
                }
            }
        }
    } catch (SocketException e) {
        LOGGER.error("Failed to get local machine IP.", e);
    }
    return candidate;
}
 
Example 3
Source File: EmailValidator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the domain component of an email address is valid.
 * @param domain being validated.
 * @return true if the email address's domain is valid.
 */
protected boolean isValidDomain(String domain) {
    boolean symbolic = false;

    // see if domain is an IP address in brackets
    Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain);

    if (ipDomainMatcher.matches()) {
        InetAddressValidator inetAddressValidator =
                InetAddressValidator.getInstance();
        if (inetAddressValidator.isValid(ipDomainMatcher.group(1))) {
            return true;
        }
    } else {
        // Domain is symbolic name
        symbolic = DOMAIN_PATTERN.matcher(domain).matches();
    }

    if (symbolic) {
        if (!isValidSymbolicDomain(domain)) {
            return false;
        }
    } else {
        return false;
    }

    return true;
}
 
Example 4
Source File: UrlValidator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns true if the authority is properly formatted.  An authority is the combination
 * of hostname and port.  A <code>null</code> authority value is considered invalid.
 * @param authority Authority value to validate.
 * @return true if authority (hostname and port) is valid.
 */
protected boolean isValidAuthority(String authority) {
    if (authority == null) {
        return false;
    }

    InetAddressValidator inetAddressValidator =
            InetAddressValidator.getInstance();

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

    boolean hostname = false;
    // check if authority is IP address or hostname
    String hostIP = authorityMatcher.group(PARSE_AUTHORITY_HOST_IP);
    boolean ipV4Address = inetAddressValidator.isValid(hostIP);

    if (!ipV4Address) {
        // Domain is hostname name
        hostname = DOMAIN_PATTERN.matcher(hostIP).matches();
    }

    //rightmost hostname will never start with a digit.
    if (hostname) {
        // LOW-TECH FIX FOR VALIDATOR-202
        // TODO: Rewrite to use ArrayList and .add semantics: see VALIDATOR-203
        char[] chars = hostIP.toCharArray();
        int size = 1;
        for(int i=0; i<chars.length; i++) {
            if(chars[i] == '.') {
                size++;
            }
        }
        String[] domainSegment = new String[size];
        boolean match = true;
        int segmentCount = 0;
        int segmentLength = 0;

        while (match) {
            Matcher atomMatcher = ATOM_PATTERN.matcher(hostIP);
            match = atomMatcher.matches();
            if (match) {
                domainSegment[segmentCount] = atomMatcher.group(1);
                segmentLength = domainSegment[segmentCount].length() + 1;
                hostIP =
                        (segmentLength >= hostIP.length())
                        ? ""
                        : hostIP.substring(segmentLength);

                segmentCount++;
            }
        }
        String topLevel = domainSegment[segmentCount - 1];
        if (topLevel.length() < 2 || topLevel.length() > 4) { // CHECKSTYLE IGNORE MagicNumber (deprecated code)
            return false;
        }

        // First letter of top level must be a alpha
        if (!ALPHA_PATTERN.matcher(topLevel.substring(0, 1)).matches()) {
            return false;
        }

        // Make sure there's a host name preceding the authority.
        if (segmentCount < 2) {
            return false;
        }
    }

    if (!hostname && !ipV4Address) {
        return false;
    }

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

    String extra = authorityMatcher.group(PARSE_AUTHORITY_EXTRA);
    if (!GenericValidator.isBlankOrNull(extra)) {
        return false;
    }

    return true;
}
 
Example 5
Source File: AppiumServiceBuilder.java    From java-client with Apache License 2.0 4 votes vote down vote up
@Override
protected ImmutableList<String> createArgs() {
    List<String> argList = new ArrayList<>();
    loadPathToMainScript();
    argList.add(appiumJS.getAbsolutePath());
    argList.add("--port");
    argList.add(String.valueOf(getPort()));

    if (StringUtils.isBlank(ipAddress)) {
        ipAddress = BROADCAST_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(capabilitiesToCmdlineArg());
    }

    return new ImmutableList.Builder<String>().addAll(argList).build();
}
 
Example 6
Source File: HIPAAMatcherAttributeValue.java    From arx with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(String value) {
    InetAddressValidator validator = InetAddressValidator.getInstance();
    return validator.isValid(value);
}