Example usage for java.net InetAddress isAnyLocalAddress

List of usage examples for java.net InetAddress isAnyLocalAddress

Introduction

In this page you can find the example usage for java.net InetAddress isAnyLocalAddress.

Prototype

public boolean isAnyLocalAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a wildcard address.

Usage

From source file:com.alibaba.jstorm.utils.NetWorkUtils.java

public static List<String> host2Ip(List<String> servers) {
    if (servers == null || servers.size() == 0) {
        return new ArrayList<String>();
    }/*from   w  w  w  . java  2 s.c  o m*/

    Set<String> ret = new HashSet<String>();
    for (String server : servers) {
        if (StringUtils.isBlank(server)) {
            continue;
        }

        InetAddress ia;
        try {
            ia = InetAddress.getByName(server);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            LOG.info("Fail to get address of ", server);
            continue;
        }
        if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {
            ret.add(NetWorkUtils.ip());
        } else {
            ret.add(ia.getHostAddress());
        }
    }

    return JStormUtils.mk_list(ret);
}

From source file:com.cloud.utils.UriUtils.java

public static Pair<String, Integer> validateUrl(String format, String url) throws IllegalArgumentException {
    try {// w  ww.j  a  v a2s.com
        URI uri = new URI(url);
        if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http")
                && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) {
            throw new IllegalArgumentException("Unsupported scheme for url: " + url);
        }
        int port = uri.getPort();
        if (!(port == 80 || port == 8080 || port == 443 || port == -1)) {
            throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed");
        }

        if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) {
            port = 443;
        } else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) {
            port = 80;
        }

        String host = uri.getHost();
        try {
            InetAddress hostAddr = InetAddress.getByName(host);
            if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress()
                    || hostAddr.isMulticastAddress()) {
                throw new IllegalArgumentException("Illegal host specified in url");
            }
            if (hostAddr instanceof Inet6Address) {
                throw new IllegalArgumentException(
                        "IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")");
            }
        } catch (UnknownHostException uhe) {
            throw new IllegalArgumentException("Unable to resolve " + host);
        }

        // verify format
        if (format != null) {
            String uripath = uri.getPath();
            checkFormat(format, uripath);
        }
        return new Pair<String, Integer>(host, port);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid URL: " + url);
    }
}

From source file:org.apache.hadoop.yarn.webapp.util.WebAppUtils.java

private static String getResolvedAddress(InetSocketAddress address) {
    address = NetUtils.getConnectAddress(address);
    StringBuilder sb = new StringBuilder();
    InetAddress resolved = address.getAddress();
    if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) {
        String lh = address.getHostName();
        try {// w ww . j ava  2 s  . c o  m
            lh = InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException e) {
            //Ignore and fallback.
        }
        sb.append(lh);
    } else {
        sb.append(address.getHostName());
    }
    sb.append(":").append(address.getPort());
    return sb.toString();
}

From source file:org.ow2.proactive.scheduler.util.SchedulerStarter.java

public static boolean isThisMyIpAddress(InetAddress addr) {
    // Check if the address is a valid special local or loop back
    if (addr.isAnyLocalAddress() || addr.isLoopbackAddress())
        return true;

    // Check if the address is defined on any interface
    try {/*from w w w . j  ava 2  s .  c  om*/
        return NetworkInterface.getByInetAddress(addr) != null;
    } catch (SocketException e) {
        return false;
    }
}

From source file:com.datatorrent.stram.client.StramClientUtils.java

public static InetSocketAddress getRMWebAddress(Configuration conf, boolean sslEnabled, String rmId) {
    rmId = (rmId == null) ? "" : ("." + rmId);
    InetSocketAddress address;//from  w  w w.ja va 2s.  c o m
    if (sslEnabled) {
        address = conf.getSocketAddr(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + rmId,
                YarnConfiguration.DEFAULT_RM_WEBAPP_HTTPS_ADDRESS,
                YarnConfiguration.DEFAULT_RM_WEBAPP_HTTPS_PORT);
    } else {
        address = conf.getSocketAddr(YarnConfiguration.RM_WEBAPP_ADDRESS + rmId,
                YarnConfiguration.DEFAULT_RM_WEBAPP_ADDRESS, YarnConfiguration.DEFAULT_RM_WEBAPP_PORT);
    }
    LOG.info("rm webapp address setting {}", address);
    LOG.debug("rm setting sources {}", conf.getPropertySources(YarnConfiguration.RM_WEBAPP_ADDRESS));
    InetSocketAddress resolvedSocketAddress = NetUtils.getConnectAddress(address);
    InetAddress resolved = resolvedSocketAddress.getAddress();
    if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) {
        try {
            resolvedSocketAddress = InetSocketAddress
                    .createUnresolved(InetAddress.getLocalHost().getCanonicalHostName(), address.getPort());
        } catch (UnknownHostException e) {
            //Ignore and fallback.
        }
    }
    return resolvedSocketAddress;
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Given an InetAddress, checks to see if the address is a local address, by comparing the address with all the
 * interfaces on the node.//  w  ww .  ja va 2s  .co m
 *
 * @param addr address to check if it is local node's address
 *
 * @return true if the address corresponds to the local node
 */
public static boolean isLocalAddress(InetAddress addr) {
    // Check if the address is any local or loop back
    boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();

    // Check if the address is defined on any interface
    if (!local) {
        try {
            local = NetworkInterface.getByInetAddress(addr) != null;
        } catch (SocketException e) {
            local = false;
        }
    }
    return local;
}

From source file:at.alladin.rmbt.shared.Helperfunctions.java

public static String IpType(InetAddress inetAddress) {
    try {/*from w  w w .ja  va 2 s.  c o  m*/
        final String ipVersion;
        if (inetAddress instanceof Inet4Address)
            ipVersion = "ipv4";
        else if (inetAddress instanceof Inet6Address)
            ipVersion = "ipv6";
        else
            ipVersion = "ipv?";

        if (inetAddress.isAnyLocalAddress())
            return "wildcard_" + ipVersion;
        if (inetAddress.isSiteLocalAddress())
            return "site_local_" + ipVersion;
        if (inetAddress.isLinkLocalAddress())
            return "link_local_" + ipVersion;
        if (inetAddress.isLoopbackAddress())
            return "loopback_" + ipVersion;
        return "public_" + ipVersion;

    } catch (final IllegalArgumentException e) {
        return "illegal_ip";
    }
}

From source file:com.datatorrent.stram.client.StramClientUtils.java

public static String getSocketConnectString(InetSocketAddress socketAddress) {
    String host;//from   w w  w .  j a  va 2s.co  m
    InetAddress address = socketAddress.getAddress();
    if (address == null) {
        host = socketAddress.getHostString();
    } else if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
        host = address.getCanonicalHostName();
    } else {
        host = address.getHostName();
    }
    return host + ":" + socketAddress.getPort();
}

From source file:org.apache.metron.enrichment.adapters.geo.GeoAdapter.java

@SuppressWarnings("unchecked")
@Override//from  ww w . j  a v a 2s  . co  m
public JSONObject enrich(CacheKey value) {
    JSONObject enriched = new JSONObject();
    if (!resetConnectionIfNecessary()) {
        _LOG.error(
                "Enrichment failure, cannot maintain a connection to JDBC.  Please check connection.  In the meantime, I'm not enriching.");
        return enriched;
    }
    try {
        InetAddress addr = InetAddress.getByName(value.getValue(String.class));
        if (addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
                || addr.isMulticastAddress()
                || !ipvalidator.isValidInet4Address(value.getValue(String.class))) {
            return new JSONObject();
        }
        String locidQuery = "select IPTOLOCID(\"" + value.getValue() + "\") as ANS";
        ResultSet resultSet = statement.executeQuery(locidQuery);
        String locid = null;
        if (resultSet.next()) {
            locid = resultSet.getString("ANS");
        }
        resultSet.close();
        if (locid == null)
            return new JSONObject();
        String geoQuery = "select * from location where locID = " + locid;
        resultSet = statement.executeQuery(geoQuery);
        if (resultSet.next()) {
            enriched.put("locID", resultSet.getString("locID"));
            enriched.put("country", resultSet.getString("country"));
            enriched.put("city", resultSet.getString("city"));
            enriched.put("postalCode", resultSet.getString("postalCode"));
            enriched.put("latitude", resultSet.getString("latitude"));
            enriched.put("longitude", resultSet.getString("longitude"));
            enriched.put("dmaCode", resultSet.getString("dmaCode"));
            enriched.put("location_point", enriched.get("latitude") + "," + enriched.get("longitude"));
        }
        resultSet.close();
    } catch (Exception e) {
        _LOG.error("Enrichment failure: " + e.getMessage(), e);
        return new JSONObject();
    }
    return enriched;
}

From source file:org.apache.metron.enrichment.adapters.geo.GeoLiteDatabase.java

private boolean isIneligibleAddress(String ipStr, InetAddress addr) {
    return addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
            || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(ipStr);
}