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:com.github.fge.jsonschema.format.common.IPv6Attribute.java

@Override
public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data)
        throws ProcessingException {
    final JsonNode instance = data.getInstance().getNode();
    final String ipaddr = instance.textValue();

    if (InetAddresses.isInetAddress(ipaddr)
            && InetAddresses.forString(ipaddr).getAddress().length == IPV6_LENGTH)
        return;//  w ww .  jav  a  2 s  . c om

    report.error(newMsg(data, bundle, "err.format.invalidIPV6Address").putArgument("value", ipaddr));
}

From source file:org.adaway.util.RegexUtils.java

/**
 * Check if the given ip is a valid IP address.
 *
 * @param ip The IP to validate./*from   w  w  w .  j  a  v a  2s  .c o  m*/
 * @return {@code true} if the IP is valid, {@code false} otherwise.
 */
public static boolean isValidIP(String ip) {
    try {
        InetAddresses.forString(ip);
        return true;
    } catch (IllegalArgumentException exception) {
        Log.d(Constants.TAG, "Invalid IP address: " + ip, exception);
        return false;
    }
}

From source file:google.registry.tools.CreateHostCommand.java

@Override
protected void initMutatingEppToolCommand() {
    setSoyTemplate(HostCreateSoyInfo.getInstance(), HostCreateSoyInfo.HOSTCREATE);
    ImmutableList.Builder<String> ipv4Addresses = new ImmutableList.Builder<>();
    ImmutableList.Builder<String> ipv6Addresses = new ImmutableList.Builder<>();
    for (String address : nullToEmpty(addresses)) {
        InetAddress inetAddress = InetAddresses.forString(address);
        if (inetAddress instanceof Inet4Address) {
            ipv4Addresses.add(inetAddress.getHostAddress());
        } else if (inetAddress instanceof Inet6Address) {
            ipv6Addresses.add(inetAddress.getHostAddress());
        } else {//from ww  w. j  a v  a  2  s  . c  om
            throw new IllegalArgumentException(String.format("IP address in unknown format: %s", address));
        }
    }
    addSoyRecord(clientId, new SoyMapData("hostname", hostName, "ipv4addresses", ipv4Addresses.build(),
            "ipv6addresses", ipv6Addresses.build()));
}

From source file:google.registry.model.host.InetAddressAdapter.java

@Override
public InetAddress unmarshal(AddressShim shim) throws IpVersionMismatchException {
    InetAddress inetAddress = InetAddresses.forString(shim.ipAddress);
    // Enforce that "v6" is used iff the address is ipv6. (For ipv4, "v4" is allowed to be missing.)
    if (inetAddress instanceof Inet6Address != "v6".equals(shim.ipVersion)) {
        throw new IpVersionMismatchException();
    }/*from   w ww  .j  a v a2  s  .  c  o m*/
    return inetAddress;
}

From source file:com.proofpoint.http.server.ClientAddressExtractor.java

public String clientAddressFor(HttpServletRequest request) {
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    for (Enumeration<String> e = request.getHeaders("X-FORWARDED-FOR"); e != null && e.hasMoreElements();) {
        String forwardedFor = e.nextElement();
        builder.addAll(Splitter.on(',').trimResults().omitEmptyStrings().split(forwardedFor));
    }//from w w  w  .j  ava2s.c om
    if (request.getRemoteAddr() != null) {
        builder.add(request.getRemoteAddr());
    }
    String clientAddress = null;
    ImmutableList<String> clientAddresses = builder.build();
    for (String address : Lists.reverse(clientAddresses)) {
        try {
            if (!trustedNetworks.containsAddress(InetAddresses.forString(address))) {
                clientAddress = address;
                break;
            }
            clientAddress = address;
        } catch (IllegalArgumentException ignored) {
            break;
        }
    }
    if (clientAddress == null) {
        clientAddress = request.getRemoteAddr();
    }
    return clientAddress;
}

From source file:org.opendaylight.infrautils.diagstatus.internal.ClusterMemberInfoImpl.java

@Override
public InetAddress getSelfAddress() {
    Object clusterStatusMBeanValue;
    try {//  w w  w. j av  a  2 s.c  o  m
        clusterStatusMBeanValue = MBeanUtils.getMBeanAttribute("akka:type=Cluster", "ClusterStatus");
    } catch (JMException e) {
        throw new IllegalStateException("getMBeanAttribute(\"akka:type=Cluster\", \"ClusterStatus\") failed",
                e);
    }
    if (clusterStatusMBeanValue != null) {
        String clusterStatusText = clusterStatusMBeanValue.toString();
        String selfAddressMbean = requireNonNull(
                StringUtils.substringBetween(clusterStatusText, "\"self-address\": ", ","),
                "null substringBetween() for: " + clusterStatusText);
        return InetAddresses.forString(extractAddressFromAkka(selfAddressMbean));
    } else {
        throw new IllegalStateException(
                "getMBeanAttribute(\"akka:type=Cluster\", \"ClusterStatus\") == null?!");
    }
}

From source file:pl.coffeepower.blog.messagebus.aeron.AeronPublisher.java

@Inject
private AeronPublisher(@NonNull Aeron aeron, @NonNull Configuration configuration) {
    Preconditions.checkArgument(/*w  w  w.j a  v a 2  s.com*/
            InetAddresses.forString(configuration.getMulticastAddress()).getAddress()[3] % 2 != 0,
            "Lowest byte in multicast address has to be odd");
    String channel = "aeron:udp?endpoint=" + configuration.getMulticastAddress() + ":"
            + configuration.getMulticastPort() + "|interface=" + configuration.getBindAddress();
    this.aeron = aeron;
    this.publication = this.aeron.addPublication(channel, configuration.getTopicId());
    this.opened.set(true);
    log.info("Created Publisher: channel={}, streamId={}", channel, configuration.getTopicId());
}

From source file:nl.sidn.dnslib.util.IPUtil.java

public static byte[] ipv6tobytes(String ip) {
    int parts = StringUtils.countMatches(ip, ":");
    int missing = 7 - parts;

    for (int i = 0; i < missing; i++) {
        ip = ip + ":0000";
    }/*from ww  w.java  2 s.  c  om*/

    return InetAddresses.forString(ip).getAddress();
}

From source file:org.graylog.plugins.pipelineprocessor.functions.ips.IpAddressConversion.java

@Override
public IpAddress evaluate(FunctionArgs args, EvaluationContext context) {
    final String ipString = String.valueOf(ipParam.required(args, context));

    try {// www  .  java  2 s .  co  m
        final InetAddress inetAddress = InetAddresses.forString(ipString);
        return new IpAddress(inetAddress);
    } catch (IllegalArgumentException e) {
        final Optional<String> defaultValue = defaultParam.optional(args, context);
        if (!defaultValue.isPresent()) {
            return new IpAddress(ANYV4);
        }
        try {
            return new IpAddress(InetAddresses.forString(defaultValue.get()));
        } catch (IllegalFormatException e1) {
            log.warn("Parameter `default` for to_ip() is not a valid IP address: {}", defaultValue.get());
            throw e1;
        }
    }
}

From source file:org.bitcoinj.core.NetAddress.java

public static NetAddress localhost(NetworkParameters params) {
    return new NetAddress(InetAddresses.forString("127.0.0.1"));
}