Example usage for java.net InetAddress getHostAddress

List of usage examples for java.net InetAddress getHostAddress

Introduction

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

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation.

Usage

From source file:com.adito.agent.client.DirectTCPSocketFactory.java

public Socket createSocket(InetAddress host, int port) throws IOException {
    try {//  w w w. j a v a2 s.co  m
        return new LocalForwardingChannelSocket(agent, host.getHostAddress(), port);
    } catch (ChannelOpenException e) {
        rethrow(e);
    }
    return null;
}

From source file:com.codemacro.jcm.JCMConfig.java

public String getServerAddr() throws UnknownHostException {
    InetAddress addr = serverConfig.getAddress();
    if (addr == null) {
        addr = InetAddress.getLocalHost();
    }/*from www  .  j a v a 2  s .  c o m*/
    return addr.getHostAddress();
}

From source file:com.doculibre.constellio.filters.LocalRequestFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    Set<String> acceptedAndDynamicHosts = new HashSet<String>();
    acceptedAndDynamicHosts.addAll(Arrays.asList(ACCEPTED_HOSTS));

    // Solution adapted from http://www.exampledepot.com/egs/java.net/Local.html
    String hostName = InetAddress.getLocalHost().getHostName();
    InetAddress addrs[] = InetAddress.getAllByName(hostName);
    for (InetAddress addr : addrs) {
        String hostAddress = addr.getHostAddress();
        acceptedAndDynamicHosts.add(hostAddress);
    }/*from   w  w  w  .j  a  va 2  s .  co  m*/

    boolean valid;
    if (isIgnoredRequest(request) || isFileRequest(request)) {
        valid = true;
    } else {
        valid = false;
        String remoteHost = request.getRemoteHost();
        for (String acceptedHost : acceptedAndDynamicHosts) {
            if (remoteHost.equals(acceptedHost)) {
                valid = true;
            }
        }
    }

    if (valid) {
        // Pass control on to the next filter
        chain.doFilter(request, response);
    } else {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        throw new ServletException("Cannot send request to a servlet from outside the Web application : "
                + httpRequest.getRequestURL());
    }
}

From source file:com.stratio.cassandra.index.schema.ColumnMapperInet.java

/** {@inheritDoc} */
@Override/*from   ww  w. j  av  a2  s . co  m*/
public String indexValue(String name, Object value) {
    if (value == null) {
        return null;
    } else if (value instanceof InetAddress) {
        InetAddress inetAddress = (InetAddress) value;
        return inetAddress.getHostAddress();
    } else if (value instanceof String) {
        String svalue = (String) value;
        if (IPV4_PATTERN.matcher(svalue).matches() || IPV6_PATTERN.matcher(svalue).matches()
                || IPV6_COMPRESSED_PATTERN.matcher(svalue).matches()) {
            try {
                return InetAddress.getByName(svalue).getHostAddress();
            } catch (UnknownHostException e) {
                Log.error(e, e.getMessage());
            }
        }
    }
    throw new IllegalArgumentException(String.format("Value '%s' cannot be cast to InetAddress", value));
}

From source file:com.stratio.cassandra.index.schema.ColumnMapperInet.java

/** {@inheritDoc} */
@Override//w w  w.ja va2 s . c o m
public String queryValue(String name, Object value) {
    if (value == null) {
        return null;
    } else if (value instanceof InetAddress) {
        InetAddress inetAddress = (InetAddress) value;
        return inetAddress.getHostAddress();
    } else if (value instanceof String) {
        String svalue = (String) value;
        if (IPV4_PATTERN.matcher(svalue).matches() || IPV6_PATTERN.matcher(svalue).matches()
                || IPV6_COMPRESSED_PATTERN.matcher(svalue).matches()) {
            try {
                return InetAddress.getByName(svalue).getHostAddress();
            } catch (UnknownHostException e) {
                Log.error(e, e.getMessage());
            }
        } else {
            return svalue;
        }
    }
    throw new IllegalArgumentException(String.format("Value '%s' cannot be cast to InetAddress", value));
}

From source file:de.zib.gndms.gndms.security.HostAndUserDetailsService.java

private boolean reverseDNSLookup(final String hostName, final Object details) throws UnknownHostException {

    String requestSourceIp = null;

    if (details instanceof WebAuthenticationDetails) {
        requestSourceIp = ((WebAuthenticationDetails) details).getRemoteAddress();
        InetAddress addr = InetAddress.getByName(hostName);
        if (!addr.getHostAddress().equals(requestSourceIp)) {
            logger.info("Revers dns lookup fail for host: \"" + hostName + "\" got: " + requestSourceIp);
            return false;
        }/*from   w w  w.  j av a  2  s  . c  om*/
        return true;
    }

    return false;
}

From source file:eu.betaas.betaasandroidapp.configuration.Configuration.java

private void setDeviceIp() {
    try {//  ww  w. j av  a  2  s.  c  o  m
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (isIPv4) {
                        deviceIP = sAddr;
                    }
                }
            }
        }
    } catch (Exception ex) {
        deviceIP = null;
    }
}

From source file:com.github.tomakehurst.wiremock.BindAddressTest.java

private String getIpAddressOtherThan(String lopbackAddress) throws SocketException {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netInterface : Collections.list(networkInterfaces)) {
        Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
        for (InetAddress address : Collections.list(inetAddresses)) {
            if (address instanceof Inet4Address && !address.getHostAddress().equals(lopbackAddress)) {
                return address.getHostAddress();
            }//  www  .  j  av a2s .c o m
        }
    }
    return null;
}

From source file:com.aerospike.hadoop.mapreduce.AerospikeInputFormat.java

private List<Host> getAliases(Host host) {
    InetAddress[] addresses;/*from  w w  w .  j a va2 s .  com*/

    try {
        addresses = InetAddress.getAllByName(host.name);
    } catch (UnknownHostException uhe) {
        throw new AerospikeException.Connection("Invalid host: " + host);
    }

    if (addresses.length == 0) {
        throw new AerospikeException.Connection("Failed to find addresses for " + host);
    }

    // Add capacity for current address aliases plus IPV6 address and hostname.
    List<Host> aliases = new ArrayList<Host>(addresses.length + 2);

    for (InetAddress address : addresses) {
        aliases.add(new Host(address.getHostAddress(), host.tlsName, host.port));
    }

    return aliases;
}

From source file:com.digitalpebble.storm.crawler.util.URLPartitioner.java

/**
 * Returns the host, domain, IP of a URL so that it can be partitioned for
 * politeness/*from  w ww.  j  a v a 2 s .c om*/
 **/
public String getPartition(String url, Metadata metadata) {

    String partitionKey = null;
    String host = "";

    // IP in metadata?
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP)) {
        String ip_provided = metadata.getFirstValue("ip");
        if (StringUtils.isNotBlank(ip_provided)) {
            partitionKey = ip_provided;
        }
    }

    if (partitionKey == null) {
        URL u;
        try {
            u = new URL(url);
            host = u.getHost();
        } catch (MalformedURLException e1) {
            LOG.warn("Invalid URL: {}", url);
            return null;
        }
    }

    // partition by hostname
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_HOST))
        partitionKey = host;

    // partition by domain : needs fixing
    else if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_DOMAIN)) {
        partitionKey = PaidLevelDomain.getPLD(host);
    }

    // partition by IP
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP) && partitionKey == null) {
        try {
            long start = System.currentTimeMillis();
            final InetAddress addr = InetAddress.getByName(host);
            partitionKey = addr.getHostAddress();
            long end = System.currentTimeMillis();
            LOG.debug("Resolved IP {} in {} msec for : {}", partitionKey, (end - start), url);
        } catch (final Exception e) {
            LOG.warn("Unable to resolve IP for: {}", host);
            return null;
        }
    }

    LOG.debug("Partition Key for: {} > {}", url, partitionKey);

    return partitionKey;
}