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.bitsofproof.supernode.model.JpaStore.java

@Override
@Transactional(propagation = Propagation.REQUIRED)
public KnownPeer findPeer(InetAddress address) {
    QKnownPeer kp = QKnownPeer.knownPeer;
    JPAQuery q = new JPAQuery(entityManager);
    return q.from(kp).where(kp.address.eq(address.getHostAddress())).uniqueResult(kp);
}

From source file:de.ingrid.interfaces.csw.server.impl.GenericServer.java

@Override
public Document process(GetCapabilitiesRequest request, String variant) throws CSWException {

    final String documentKey = ConfigurationKeys.CAPABILITIES_DOC;

    Document doc = this.getDocument(documentKey, variant);

    // try to replace the interface URLs
    NodeList nodes = this.xpath.getNodeList(doc, "//ows:Operation/*/ows:HTTP/*/@xlink:href");
    // get host//from   w  ww. j  av a 2 s.  c  om
    String host = ApplicationProperties.get(ConfigurationKeys.SERVER_INTERFACE_HOST, null);
    if (host == null) {
        log.info("The interface host address is not specified, use local hosts address instead.");
        try {
            InetAddress addr = InetAddress.getLocalHost();
            host = addr.getHostAddress();
        } catch (UnknownHostException e) {
            log.error("Unable to get interface host address.", e);
            throw new RuntimeException("Unable to get interface host address.", e);
        }
    }
    // get the port (defaults to 80)
    String port = ApplicationProperties.get(ConfigurationKeys.SERVER_INTERFACE_PORT, "80");
    // get the port (defaults to 80)
    String path = ApplicationProperties.get(ConfigurationKeys.SERVER_INTERFACE_PATH, "80");
    // replace interface host and port
    for (int idx = 0; idx < nodes.getLength(); idx++) {
        String s = nodes.item(idx).getTextContent();
        s = s.replaceAll(ConfigurationKeys.VARIABLE_INTERFACE_HOST, host);
        s = s.replaceAll(ConfigurationKeys.VARIABLE_INTERFACE_PORT, port);
        s = s.replaceAll(ConfigurationKeys.VARIABLE_INTERFACE_PATH, path);
        nodes.item(idx).setTextContent(s);
    }

    return doc;
}

From source file:org.peercast.core.PeerCastServiceController.java

private String getIpAddress() {
    Enumeration<NetworkInterface> netIFs;
    try {// w w w  .  j  a va2s .  c  o m
        netIFs = NetworkInterface.getNetworkInterfaces();
        while (netIFs.hasMoreElements()) {
            NetworkInterface netIF = netIFs.nextElement();
            Enumeration<InetAddress> ipAddrs = netIF.getInetAddresses();
            while (ipAddrs.hasMoreElements()) {
                InetAddress ip = ipAddrs.nextElement();
                if (!ip.isLoopbackAddress() && !ip.isLinkLocalAddress() && ip.isSiteLocalAddress()) {
                    String ipStr = ip.getHostAddress().toString();
                    Log.d(TAG, "IP: " + ipStr);
                    return ipStr;
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.maxmind.geoip2.WebServiceClient.java

private GenericUrl createUri(String path, InetAddress ipAddress) {
    return new GenericUrl("https://" + this.host + "/geoip/v2.1/" + path + "/"
            + (ipAddress == null ? "me" : ipAddress.getHostAddress()));

}

From source file:com.hypersocket.netty.NettyServer.java

protected void bindInterface(Integer port, Set<Channel> channels) throws IOException {

    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();

    Set<String> interfacesToBind = new HashSet<String>(
            Arrays.asList(configurationService.getValues("listening.interfaces")));

    if (interfacesToBind.isEmpty()) {

        if (log.isInfoEnabled()) {
            log.info("Binding server to all interfaces on port " + port);
        }/*from   w ww  .jav a2 s .  c  o m*/
        Channel ch = serverBootstrap.bind(new InetSocketAddress(port));
        channels.add(ch);

        if (log.isInfoEnabled()) {
            log.info("Bound to port " + ((InetSocketAddress) ch.getLocalAddress()).getPort());
        }
    } else {
        while (e.hasMoreElements()) {

            NetworkInterface i = e.nextElement();

            Enumeration<InetAddress> inetAddresses = i.getInetAddresses();

            for (InetAddress inetAddress : Collections.list(inetAddresses)) {

                if (interfacesToBind.contains(inetAddress.getHostAddress())) {
                    try {
                        if (log.isInfoEnabled()) {
                            log.info("Binding server to interface " + i.getDisplayName() + " "
                                    + inetAddress.getHostAddress() + ":" + port);
                        }

                        Channel ch = serverBootstrap.bind(new InetSocketAddress(inetAddress, port));
                        channels.add(ch);

                        if (log.isInfoEnabled()) {
                            log.info("Bound to " + inetAddress.getHostAddress() + ":"
                                    + ((InetSocketAddress) ch.getLocalAddress()).getPort());
                        }

                    } catch (ChannelException ex) {
                        log.error("Failed to bind port", ex);
                    }
                }
            }
        }
    }
}

From source file:net.sf.mpaxs.spi.computeHost.Settings.java

private String getOwnIP() {

    InetAddress inet2 = null;
    try {//from   w ww .ja  v  a2  s .  c o m
        InetAddress inet1 = InetAddress.getLocalHost();
        inet2 = InetAddress.getByName(inet1.getHostName());
        return inet2.getHostAddress();
    } catch (UnknownHostException ex) {
        Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.alibaba.otter.shared.communication.core.impl.DefaultCommunicationClientImpl.java

private CommunicationParam buildParams(String addr) {
    CommunicationParam params = new CommunicationParam();
    String[] strs = StringUtils.split(addr, ":");
    if (strs == null || strs.length != 2) {
        throw new IllegalArgumentException("addr example: 127.0.0.1:1099");
    }/*from  w ww. j  av  a 2  s  .  c om*/
    InetAddress address = null;
    try {
        address = InetAddress.getByName(strs[0]);
    } catch (UnknownHostException e) {
        throw new CommunicationException("addr_error", "addr[" + addr + "] is unknow!");
    }
    params.setIp(address.getHostAddress());
    params.setPort(Integer.valueOf(strs[1]));
    return params;
}

From source file:com.evolveum.midpoint.task.quartzimpl.cluster.NodeRegistrar.java

private String getMyAddress() {

    if (taskManager.getConfiguration().getJmxHostName() != null) {
        return taskManager.getConfiguration().getJmxHostName();
    } else {/*from ww  w .  ja va  2  s  .  c o  m*/
        try {
            InetAddress address = InetAddress.getLocalHost();
            return address.getHostAddress();
        } catch (UnknownHostException e) {
            LoggingUtils.logException(LOGGER, "Cannot get local IP address", e);
            return "unknown-host";
        }
    }
}

From source file:com.roncoo.pay.controller.common.BaseController2.java

/**
 * ?IP?//from w ww .j  a va2 s .co  m
 * 
 * @return
 */
public String getIpAddr(HttpServletRequest request) {
    String ipAddress = null;
    ipAddress = request.getHeader("x-forwarded-for");
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
        ipAddress = request.getHeader("Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
        ipAddress = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
        ipAddress = request.getRemoteAddr();
        if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
            // ????IP
            InetAddress inet = null;
            try {
                inet = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                log.error("", e);
            }
            ipAddress = inet.getHostAddress();
        }

    }

    // ?IPIP,IP','
    if (ipAddress != null && ipAddress.length() > 15) {
        if (ipAddress.indexOf(",") > 0) {
            ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
        }
    }
    return ipAddress;
}

From source file:ddf.ldap.ldaplogin.LdapLoginConfig.java

private void auditRemoteConnection(String host) {
    try {//from w w w .j av a 2s . c o m
        InetAddress inetAddress = InetAddress.getByName(host);
        SecurityLogger.audit("Setting up remote connection to LDAP [{}].", inetAddress.getHostAddress());
    } catch (Exception e) {
        LOGGER.debug(
                "Unhandled exception while attempting to determine the IP address for an LDAP, might be a DNS issue.",
                e);
        SecurityLogger.audit("Unable to determine the IP address for an LDAP [{}], might be a DNS issue.",
                host);
    }
}