Example usage for com.google.common.net InetAddresses isInetAddress

List of usage examples for com.google.common.net InetAddresses isInetAddress

Introduction

In this page you can find the example usage for com.google.common.net InetAddresses isInetAddress.

Prototype

public static boolean isInetAddress(String ipString) 

Source Link

Document

Returns true if the supplied string is a valid IP string literal, false otherwise.

Usage

From source file:org.apache.bookkeeper.tools.cli.helpers.CommandHelpers.java

public static String getBookieSocketAddrStringRepresentation(BookieSocketAddress bookieId) {
    String hostname = bookieId.getHostName();
    boolean isHostNameIpAddress = InetAddresses.isInetAddress(hostname);
    String otherFormOfHostname = null;
    if (isHostNameIpAddress) {
        otherFormOfHostname = bookieId.getSocketAddress().getAddress().getCanonicalHostName();
    } else {// www  .  j ava  2s . c om
        otherFormOfHostname = bookieId.getSocketAddress().getAddress().getHostAddress();
    }
    String bookieSocketAddrStringRepresentation = hostname + "(" + otherFormOfHostname + ")" + ":"
            + bookieId.getSocketAddress().getPort();
    return bookieSocketAddrStringRepresentation;
}

From source file:org.apache.activemq.artemis.utils.IPV6Util.java

/**
 * It will enclose an IPV6 host with [], that we need before building URIs
 *//*  w w w  .  j  a  v  a 2 s. c  o m*/
public static String encloseHost(final String host) {
    // if the host contains a ':' then we know it's not IPv4
    if (host != null && host.contains(":")) {
        String hostToCheck = host;

        /* strip off zone index since com.google.common.net.InetAddresses.isInetAddress() doesn't support it
         * see https://en.wikipedia.org/wiki/IPv6_address#Link-local_addresses_and_zone_indices for more info
         */
        if (host.contains("%")) {
            hostToCheck = host.substring(0, host.indexOf("%"));
        }

        if (InetAddresses.isInetAddress(hostToCheck)) {
            return "[" + host + "]";
        }
    }

    return host;
}

From source file:com.yahoo.athenz.common.server.util.ServletRequestUtil.java

/**
  * Return the remote client IP address.
  * Detect if connection is from local proxy server by looking at XFF header.
  * If XFF header, return the last address therein since it was added by
  * the proxy server.//from   w ww  .  j  a v a  2  s. c  o  m
  * @param request http servlet request
  * @return client remote address string
 **/
public static String getRemoteAddress(final HttpServletRequest request) {
    String addr = request.getRemoteAddr();
    if (LOOPBACK_ADDRESS.equals(addr)) {
        String xff = request.getHeader(XFF_HEADER);
        if (xff != null) {
            String[] addrs = xff.split(",");
            final String xffAddr = addrs[addrs.length - 1].trim();
            if (InetAddresses.isInetAddress(xffAddr)) {
                addr = xffAddr;
            }
        }
    }
    return addr;
}

From source file:zipkin.storage.elasticsearch.http.PseudoAddressRecordSet.java

static Dns create(List<String> urls, Dns actualDns) {
    Set<String> schemes = Sets.newLinkedHashSet();
    Set<String> hosts = Sets.newLinkedHashSet();
    Set<InetAddress> ipAddresses = Sets.newLinkedHashSet();
    Set<Integer> ports = Sets.newLinkedHashSet();

    for (String url : urls) {
        HttpUrl httpUrl = HttpUrl.parse(url);
        schemes.add(httpUrl.scheme());/*  ww w  .ja  v  a 2 s. c  om*/
        if (InetAddresses.isInetAddress(httpUrl.host())) {
            ipAddresses.add(InetAddresses.forString(httpUrl.host()));
        } else {
            hosts.add(httpUrl.host());
        }
        ports.add(httpUrl.port());
    }

    checkArgument(ports.size() == 1, "Only one port supported with multiple hosts %s", urls);
    checkArgument(schemes.size() == 1 && schemes.iterator().next().equals("http"),
            "Only http supported with multiple hosts %s", urls);

    if (hosts.isEmpty())
        return new StaticDns(ipAddresses);
    return new ConcatenatingDns(ipAddresses, hosts, actualDns);
}

From source file:com.brighttag.agathon.security.Netmask.java

public static @Nullable Netmask fromCidr(String cidr) {
    String[] parts = cidr.split("/");
    if (parts.length == 2 && InetAddresses.isInetAddress(parts[0])) {
        Integer prefixLength = Ints.tryParse(parts[1]);
        if (prefixLength != null) {
            return new Netmask(parts[0], prefixLength);
        }/* w w  w. j  a  v  a  2s.  c om*/
    }
    return null;
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.match.AbstractOxmIpv6AddressSerializer.java

protected void writeIpv6Address(final String textAddress, final ByteBuf outBuffer) {
    if (InetAddresses.isInetAddress(textAddress)) {
        byte[] binaryAddress = InetAddresses.forString(textAddress).getAddress();
        outBuffer.writeBytes(binaryAddress);
    } else {/*  w  w w  .  ja  va  2  s. com*/
        throw new IllegalArgumentException("Invalid ipv6 address received: " + textAddress);
    }
}

From source file:com.forerunnergames.tools.net.NetworkTools.java

public static boolean isValidIpAddress(@Nullable final String address) {
    return address != null && InetAddresses.isInetAddress(address);
}

From source file:org.dcache.nfs.HostEntryComparator.java

private static int preferIp(String s1, String s2) {

    String addr1 = stripNetmask(s1);
    String addr2 = stripNetmask(s2);

    if (InetAddresses.isInetAddress(addr1) && InetAddresses.isInetAddress(addr2)) {
        return Integer.compare(
                // Reverse order to prefer longer (IPv6) addresses
                InetAddresses.forString(addr2).getAddress().length,
                InetAddresses.forString(addr1).getAddress().length);
    } else if (InetAddresses.isInetAddress(addr1)) {
        return -1;
    } else if (InetAddresses.isInetAddress(addr2)) {
        return 1;
    }//w  w w  .  ja  va  2  s. c o m

    // both are full host names
    return 0;
}

From source file:org.mule.extension.validation.internal.validator.IpValidator.java

@Override
public ValidationResult validate(MuleEvent event) {
    return InetAddresses.isInetAddress(ip) ? ok() : fail();
}

From source file:io.bigfs.network.NodeAddress.java

private boolean validateHostAddress(String host) {
    return InetAddresses.isInetAddress(host);
}