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

The following examples show how to use org.apache.commons.validator.routines.InetAddressValidator#isValidInet4Address() . 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: 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: 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 4
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 5
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 6
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 7
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();
}