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.atrium.atriumutil.Ip6Address.java

/**
 * Converts an IPv6 string literal (e.g., "1111:2222::8888") into an IP
 * address./*from   w w w.ja  v  a2 s  .c  o m*/
 *
 * @param value an IPv6 address value in string form
 * @return an IPv6 address
 * @throws IllegalArgumentException if the argument is invalid
 */
public static Ip6Address valueOf(String value) {
    InetAddress inetAddress = null;
    try {
        inetAddress = InetAddresses.forString(value);
    } catch (IllegalArgumentException e) {
        final String msg = "Invalid IP address string: " + value;
        throw new IllegalArgumentException(msg);
    }
    return valueOf(inetAddress);
}

From source file:at.alladin.rmbt.controlServer.ContextListener.java

@SuppressWarnings("unused")
private void getGeoIPs() {
    scheduler.submit(new Runnable() {
        @Override/*  w ww  . ja  v  a2  s  .  c om*/
        public void run() {
            try {
                System.out.println("getting geoips");
                final Connection conn = DbConnection.getConnection();

                final boolean oldAutoCommitState = conn.getAutoCommit();
                conn.setAutoCommit(false);
                // allow update only 2min after test was started
                final PreparedStatement psUpd = conn.prepareStatement(
                        "UPDATE test SET country_geoip=? WHERE uid=? and (now() - time  < interval '2' minute)");
                final PreparedStatement ps = conn.prepareStatement(
                        "SELECT uid,client_public_ip FROM test WHERE client_public_ip IS NOT NULL AND country_geoip IS NULL");
                ps.execute();
                final ResultSet rs = ps.getResultSet();
                int count = 0;
                while (rs.next()) {
                    Thread.sleep(5);
                    count++;
                    if ((count % 1000) == 0)
                        System.out.println(count + " geoips updated");
                    final long uid = rs.getLong("uid");
                    final String ip = rs.getString("client_public_ip");
                    final InetAddress ia = InetAddresses.forString(ip);
                    final String country = GeoIPHelper.lookupCountry(ia);
                    if (country != null) {
                        psUpd.setString(1, country);
                        psUpd.setLong(2, uid);
                        psUpd.executeUpdate();
                    }
                }
                psUpd.close();
                ps.close();
                conn.commit();
                conn.setAutoCommit(oldAutoCommitState);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

From source file:org.n52.sos.request.RequestContext.java

private static IPAddress getIPAddress(HttpServletRequest req) {
    InetAddress addr = null;/*from  www.ja  va 2 s. c  o m*/
    try {
        addr = InetAddresses.forString(req.getRemoteAddr());
    } catch (IllegalArgumentException e) {
        LOG.warn("Ignoring invalid IP address: " + req.getRemoteAddr(), e);
    }

    if (addr instanceof Inet4Address) {
        Inet4Address inet4Address = (Inet4Address) addr;
        return new IPAddress(inet4Address);
    } else if (addr instanceof Inet6Address) {
        Inet6Address inet6Address = (Inet6Address) addr;
        if (InetAddresses.isCompatIPv4Address(inet6Address)) {
            return new IPAddress(InetAddresses.getCompatIPv4Address(inet6Address));
        } else if (InetAddresses.toAddrString(addr).equals("::1")) {
            // ::1 is not handled by InetAddresses.isCompatIPv4Address()
            return new IPAddress("127.0.0.1");
        } else {
            LOG.warn("Ignoring not v4 compatible IP address: {}", req.getRemoteAddr());
        }
    } else {
        LOG.warn("Ignoring unknown InetAddress: {}", addr);
    }
    return null;
}

From source file:org.dcache.gridsite.DelegationHandler.java

private Subject login() throws DelegationException {
    try {/*  www . ja  va  2s .c o m*/
        Subject subject = new Subject();
        X509Certificate[] chain = Axis.getCertificateChain()
                .orElseThrow(() -> new DelegationException("User supplied no certificate."));
        subject.getPublicCredentials().add(cf.generateCertPath(asList(chain)));
        subject.getPrincipals().add(new Origin(InetAddresses.forString(Axis.getRemoteAddress())));
        return loginStrategy.login(subject).getSubject();
    } catch (CertificateException e) {
        throw new DelegationException("Failed to process certificate chain.");
    } catch (PermissionDeniedCacheException e) {
        throw new DelegationException("User is not authorized.");
    } catch (TimeoutCacheException e) {
        throw new DelegationException("Internal timeout.");
    } catch (CacheException e) {
        throw new DelegationException(e.getMessage());
    }
}

From source file:org.opensaml.profile.logic.IPRangePredicate.java

/** {@inheritDoc} */
@Override/* w w w. j  a  v a2  s. c  om*/
public boolean apply(@Nullable final BaseContext input) {
    String address = httpRequest != null ? httpRequest.getRemoteAddr() : null;
    if (address == null || !InetAddresses.isInetAddress(address)) {
        return false;
    }

    for (IPRange range : addressRanges) {
        if (range.contains(InetAddresses.forString(address))) {
            return true;
        }
    }

    return false;
}

From source file:net.shibboleth.idp.authn.impl.ExtractUserAgentAddress.java

/** {@inheritDoc} */
@Override/*from   www. ja  va  2 s  .  com*/
protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext,
        @Nonnull final AuthenticationContext authenticationContext) {

    final HttpServletRequest request = getHttpServletRequest();
    if (request == null) {
        log.debug("{} Profile action does not contain an HttpServletRequest", getLogPrefix());
        ActionSupport.buildEvent(profileRequestContext, AuthnEventIds.NO_CREDENTIALS);
        return;
    }

    final String addressString = applyTransforms(request.getRemoteAddr());
    if (addressString == null || !InetAddresses.isInetAddress(addressString)) {
        log.debug("{} User agent's address, {}, is not a valid IP address", getLogPrefix(), addressString);
        ActionSupport.buildEvent(profileRequestContext, AuthnEventIds.NO_CREDENTIALS);
        return;
    }

    authenticationContext.getSubcontext(UserAgentContext.class, true)
            .setAddress(InetAddresses.forString(addressString));
}

From source file:org.opendaylight.atrium.util.AtriumIp6Address.java

/**
 * Converts an IPv6 string literal (e.g., "1111:2222::8888") into an IP
 * address.//w w  w. j av a  2 s .  co m
 *
 * @param value an IPv6 address value in string form
 * @return an IPv6 address
 * @throws IllegalArgumentException if the argument is invalid
 */
public static AtriumIp6Address valueOf(String value) {
    InetAddress inetAddress = null;
    try {
        inetAddress = InetAddresses.forString(value);
    } catch (IllegalArgumentException e) {
        final String msg = "Invalid IP address string: " + value;
        throw new IllegalArgumentException(msg);
    }
    return valueOf(inetAddress);
}

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

@Override
public List<InetAddress> getClusterMembers() {
    Object clusterMemberMBeanValue;
    try {//  w  ww.j a  va2 s  .c o  m
        clusterMemberMBeanValue = MBeanUtils.getMBeanAttribute("akka:type=Cluster", "Members");
    } catch (JMException e) {
        LOG.error("Problem to getMBeanAttribute(\"akka:type=Cluster\", \"Members\"); returning empty List", e);
        return Collections.emptyList();
    }

    List<InetAddress> clusterIPAddresses = new ArrayList<>();
    if (clusterMemberMBeanValue != null) {
        String[] clusterMembers = ((String) clusterMemberMBeanValue).split(",", -1);
        for (String clusterMember : clusterMembers) {
            String nodeIp = extractAddressFromAkka(clusterMember);
            clusterIPAddresses.add(InetAddresses.forString(nodeIp));
        }
    }
    return clusterIPAddresses;
}

From source file:com.dmdirc.addons.dns.DNSCommand.java

private void resolve(@Nonnull final WindowModel origin, final boolean isSilent, final String arg) {
    try {//ww w  .j a v  a  2s  . c  om
        final InetAddress address = InetAddresses.forString(arg);
        showOutput(origin, isSilent, "Resolved: " + arg + ": " + address.getCanonicalHostName());
    } catch (IllegalArgumentException ex) {
        showOutput(origin, isSilent, "Resolved: " + arg + ": " + getIPs(arg));
    }
}

From source file:org.eclipse.neoscada.protocol.iec60870.client.AutoConnectClient.java

private InetSocketAddress makeAddress(final String host, final int port) {
    try {/*  w  ww .  ja  v a 2 s . c  om*/
        // try numeric ip address first
        return new InetSocketAddress(InetAddresses.forString(host), port);
    } catch (final IllegalArgumentException e) {
        // assume as hostname
        return InetSocketAddress.createUnresolved(host, port);
    }
}