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

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

Introduction

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

Prototype

public static InetAddress forString(String ipString) 

Source Link

Document

Returns the InetAddress having the given string representation.

Usage

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 {// ww w  .ja v a 2s.  com
        throw new IllegalArgumentException("Invalid ipv6 address received: " + textAddress);
    }
}

From source file:org.opendaylight.infrautils.diagstatus.spi.NoClusterMemberInfo.java

public NoClusterMemberInfo() {
    this(InetAddresses.forString("127.0.0.1"));
}

From source file:com.gemini.mapper.IPAddressCustomConverter.java

@Override
public Object convert(Object destValue, Object sourceValue, Class<?> destClass, Class<?> sourceClass)
        throws MappingException {
    if (sourceValue == null) {
        Logger.error("IP Address Custom Converter used incorrectly, NULL values passed");
        return null;
    }/*from w  w  w. j  a va 2 s.  co m*/
    if (sourceValue instanceof String) {
        InetAddress dest = InetAddresses.forString((String) sourceValue);
        return dest;
    } else if (sourceValue instanceof InetAddress) {
        return ((InetAddress) sourceValue).getHostAddress();
    } else {
        Logger.error(
                "IP Address Custom Converter used incorrectly. Arguments passed in were: source\n {} \n destination\n {}",
                ToStringBuilder.reflectionToString(sourceValue, ToStringStyle.MULTI_LINE_STYLE),
                ToStringBuilder.reflectionToString(destValue, ToStringStyle.MULTI_LINE_STYLE));
        throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:"
                + ToStringBuilder.reflectionToString(destValue, ToStringStyle.MULTI_LINE_STYLE) + " and "
                + ToStringBuilder.reflectionToString(sourceValue, ToStringStyle.MULTI_LINE_STYLE));
    }
}

From source file:no.nb.nna.broprox.frontier.worker.CrawlHostGroupCalculator.java

private static BigInteger ipAsInteger(String ip) {
    return new BigInteger(InetAddresses.forString(ip).getAddress());
}

From source file:org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils.java

/**
 * This implementation does not use nameservice lookups (e.g. no DNS).
 *
 * @param cidr - format must be valid for regex in {@link Ipv4Prefix} or {@link Ipv6Prefix}
 * @return the {@link IpPrefix} having the given cidr string representation
 * @throws IllegalArgumentException - if the argument is not a valid CIDR string
 *///from  w w w  .jav  a 2s  .  c  o m
public static IpPrefix createIpPrefix(String cidr) {
    checkArgument(!Strings.isNullOrEmpty(cidr), "Cannot be null or empty.");
    String[] ipAndPrefix = cidr.split("/");
    checkArgument(ipAndPrefix.length == 2, "Bad format.");
    InetAddress ip = InetAddresses.forString(ipAndPrefix[0]);
    if (ip instanceof Inet4Address) {
        return new IpPrefix(new Ipv4Prefix(cidr));
    }
    return new IpPrefix(new Ipv6Prefix(cidr));
}

From source file:org.zenoss.zep.utils.IpUtils.java

/**
 * Parses the string into an InetAddress ensuring that no DNS lookup is performed
 * on the address (it must be specified as a literal IPv4 or IPv6 address).
 * //www  .  j  a v  a  2 s.c om
 * @param value String IP address.
 * @return InetAddress for the specified address.
 * @throws IllegalArgumentException If the address is invalid.
 */
public static InetAddress parseAddress(String value) throws IllegalArgumentException {
    // Looks for an IPv6 or IPv4 address. Discards anything that looks like a hostname
    // to prevent long running hostname lookups.
    final InetAddress addr;
    if (value.indexOf(':') != -1) {
        if (value.startsWith("[") && value.endsWith("]")) {
            // We expect an IPv6 address
            value = value.substring(1, value.length() - 1);
        }
        // Use Guava IPv6 parsing - previous parsing performed DNS lookups
        addr = InetAddresses.forString(value);
    } else {
        Matcher matcher = IPV4_PATTERN.matcher(value);
        if (matcher.matches()) {
            final byte[] bytes = new byte[4];
            for (int i = 1; i <= matcher.groupCount(); i++) {
                int octet = Integer.parseInt(matcher.group(i));
                if (octet > 255) {
                    throw new IllegalArgumentException("Invalid IP address: " + value);
                }
                bytes[i - 1] = (byte) octet;
            }
            try {
                addr = InetAddress.getByAddress(bytes);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException(e.getLocalizedMessage(), e);
            }
        } else {
            throw new IllegalArgumentException("Invalid IP address: " + value);
        }
    }
    return addr;
}

From source file:google.registry.model.translators.InetAddressTranslatorFactory.java

@Override
SimpleTranslator<InetAddress, String> createTranslator() {
    return new SimpleTranslator<InetAddress, String>() {
        @Override//w  ww.  j  a  va  2s .c o m
        public InetAddress loadValue(String datastoreValue) {
            return InetAddresses.forString(datastoreValue);
        }

        @Override
        public String saveValue(InetAddress pojoValue) {
            return pojoValue.getHostAddress();
        }
    };
}

From source file:org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil.java

public static InetAddress selectItrRloc(MapRequest request) {
    if (request.getItrRloc() == null) {
        return null;
    }/*w  w  w .  j a v a  2  s .  co  m*/
    InetAddress selectedItrRloc = null;
    for (ItrRloc itr : request.getItrRloc()) {
        Address addr = itr.getRloc().getAddress();
        if (addr instanceof Ipv4) {
            selectedItrRloc = InetAddresses.forString(((Ipv4) addr).getIpv4().getValue());
            break;
        }
        if (addr instanceof Ipv6) {
            selectedItrRloc = InetAddresses.forString(((Ipv6) addr).getIpv6().getValue());
            break;
        }
    }
    return selectedItrRloc;
}

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;
    }/*  ww  w.  j  a va 2  s .  c o m*/

    // both are full host names
    return 0;
}

From source file:org.everit.json.schema.internal.IPAddressValidator.java

/**
 * Creates an {@link InetAddress} instance if possible and returns it, or on failure it returns
 * {@code Optional.empty()}.//from  w  ww . j  av a2s .c o  m
 *
 * @param subject the string to be validated.
 * @return the optional validation failure message
 */
protected Optional<InetAddress> asInetAddress(final String subject) {
    try {
        if (InetAddresses.isInetAddress(subject)) {
            return Optional.of(InetAddresses.forString(subject));
        } else {
            return Optional.empty();
        }
    } catch (NullPointerException e) {
        return Optional.empty();
    }
}