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

The following examples show how to use org.apache.commons.validator.routines.InetAddressValidator#getInstance() . 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: HostnameUtils.java    From kafka-message-tool with MIT License 6 votes vote down vote up
private void assignLocalhostIpV4Addresses() {

        final InetAddressValidator validator = InetAddressValidator.getInstance();
        try {
            Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                NetworkInterface n = (NetworkInterface) enumeration.nextElement();
                Enumeration ee = n.getInetAddresses();
                while (ee.hasMoreElements()) {
                    InetAddress i = (InetAddress) ee.nextElement();

                    final String hostAddress = i.getHostAddress();
                    if (!validator.isValidInet4Address(hostAddress)) {
                        continue;
                    }
                    localhostIpAddresses.add(hostAddress);
                }
            }
        } catch (SocketException exception) {
            Logger.error("Could not fetch localhost ip addresses", exception);
        }
    }
 
Example 2
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 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: NetUtils.java    From cosmic with Apache License 2.0 5 votes vote down vote up
public static boolean isValidIp4(final String ip) {
    if (ip == null) {
        return false;
    }

    final InetAddressValidator validator = InetAddressValidator.getInstance();
    return validator.isValidInet4Address(ip);
}
 
Example 5
Source File: NetUtils.java    From cosmic with Apache License 2.0 5 votes vote down vote up
public static boolean isValidIp6(final String ip) {
    if (ip == null) {
        return false;
    }

    final InetAddressValidator validator = InetAddressValidator.getInstance();
    return validator.isValidInet6Address(ip);
}
 
Example 6
Source File: RemoteAddressStrategyFactory.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public MultipleRemoteAddressStrategy(String[] strArray) {
    InetAddressValidator validator = InetAddressValidator.getInstance();
    for (String netaddress : strArray) {
        if (validator.isValidInet4Address(netaddress)) {
            multipleSet.add(netaddress);
        } else if (validator.isValidInet6Address(netaddress)) {
            multipleSet.add(AclUtils.expandIP(netaddress, 8));
        } else {
            throw new AclException(String.format("Netaddress examine Exception netaddress is %s", netaddress));
        }
    }
}
 
Example 7
Source File: RemoteAddressStrategyFactory.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public boolean match(PlainAccessResource plainAccessResource) {
    InetAddressValidator validator = InetAddressValidator.getInstance();
    String whiteRemoteAddress = plainAccessResource.getWhiteRemoteAddress();
    if (validator.isValidInet6Address(whiteRemoteAddress)) {
        whiteRemoteAddress = AclUtils.expandIP(whiteRemoteAddress, 8);
    }
    return multipleSet.contains(whiteRemoteAddress);
}
 
Example 8
Source File: RemoteAddressStrategyFactory.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public OneRemoteAddressStrategy(String netaddress) {
    this.netaddress = netaddress;
    InetAddressValidator validator = InetAddressValidator.getInstance();
    if (!(validator.isValidInet4Address(netaddress) || validator.isValidInet6Address(netaddress))) {
        throw new AclException(String.format("Netaddress examine Exception netaddress is %s", netaddress));
    }
}
 
Example 9
Source File: UtilAll.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static boolean ipV6Check(byte[] ip) {
    if (ip.length != 16) {
        throw new RuntimeException("illegal ipv6 bytes");
    }

    InetAddressValidator validator = InetAddressValidator.getInstance();
    return validator.isValidInet6Address(ipToIPv6Str(ip));
}
 
Example 10
Source File: NetUtils.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static boolean isValidIp4(final String ip) {
    if (ip == null)
        return false;

    final InetAddressValidator validator = InetAddressValidator.getInstance();
    return validator.isValidInet4Address(ip);
}
 
Example 11
Source File: NetUtils.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static boolean isValidIp6(final String ip) {
    if (ip == null)
        return  false;

    final InetAddressValidator validator = InetAddressValidator.getInstance();
    return validator.isValidInet6Address(ip);
}
 
Example 12
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 13
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 14
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);
}