Example usage for java.net InetSocketAddress getHostName

List of usage examples for java.net InetSocketAddress getHostName

Introduction

In this page you can find the example usage for java.net InetSocketAddress getHostName.

Prototype

public final String getHostName() 

Source Link

Document

Gets the hostname .

Usage

From source file:com.twitter.common.net.InetSocketAddressHelper.java

/**
 * Attempts to return a usable String given an InetSocketAddress.
 *
 * @param value the InetSocketAddress./*from   w w w. jav  a 2  s  .  co m*/
 * @return the String representation of the InetSocketAddress.
 */
public static String toString(InetSocketAddress value) {
    Preconditions.checkNotNull(value);
    return value.getHostName() + ":" + value.getPort();
}

From source file:eu.esdihumboldt.util.http.client.fluent.FluentProxyUtil.java

/**
 * setup the given request object to go via proxy
 * //from  www  . j  a va  2s .c o m
 * @param request A fluent request
 * @param proxy applying proxy to the fluent request
 * @return Executor, for a fluent request
 */
public static Executor setProxy(Request request, Proxy proxy) {
    ProxyUtil.init();
    Executor executor = Executor.newInstance();

    if (proxy != null && proxy.type() == Type.HTTP) {
        InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();

        // set the proxy
        HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort());
        request.viaProxy(proxyHost);

        String userName = System.getProperty("http.proxyUser");
        String password = System.getProperty("http.proxyPassword");

        boolean useProxyAuth = userName != null && !userName.isEmpty();

        if (useProxyAuth) {
            Credentials cred = ClientProxyUtil.createCredentials(userName, password);

            executor.auth(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), cred);
        }
    }
    return executor;
}

From source file:jp.go.nict.langrid.management.logic.service.HttpClientUtil.java

/**
 * /*from   w w w .j a v a 2 s.  c om*/
 * 
 */
public static HttpClient createHttpClientWithHostConfig(URL url) {
    HttpClient client = new HttpClient();
    int port = url.getPort();
    if (port == -1) {
        port = url.getDefaultPort();
        if (port == -1) {
            port = 80;
        }
    }
    if ((url.getProtocol().equalsIgnoreCase("https")) && (sslSocketFactory != null)) {
        Protocol https = new Protocol("https",
                (ProtocolSocketFactory) new SSLSocketFactorySSLProtocolSocketFactory(sslSocketFactory), port);
        client.getHostConfiguration().setHost(url.getHost(), url.getPort(), https);
    } else {
        Protocol http = new Protocol("http", new SocketFactoryProtocolSocketFactory(SocketFactory.getDefault()),
                port);
        client.getHostConfiguration().setHost(url.getHost(), url.getPort(), http);
    }
    try {
        List<Proxy> proxies = ProxySelector.getDefault().select(url.toURI());
        for (Proxy p : proxies) {
            if (p.equals(Proxy.NO_PROXY))
                continue;
            if (!p.type().equals(Proxy.Type.HTTP))
                continue;
            InetSocketAddress addr = (InetSocketAddress) p.address();
            client.getHostConfiguration().setProxy(addr.getHostName(), addr.getPort());
            break;
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return client;
}

From source file:SystemUtils.java

public static final String getRawAddress(InetSocketAddress inetSocketAddress) {
    InetAddress address = inetSocketAddress.getAddress();
    return address != null ? address.getHostAddress() : inetSocketAddress.getHostName();
}

From source file:jp.go.nict.langrid.servicesupervisor.invocationprocessor.executor.intragrid.HttpClientUtil.java

/**
 * /*from  w w  w.jav  a2 s. c om*/
 * 
 */
public static HttpClient createHttpClientWithHostConfig(URL url) {
    HttpClient client = new HttpClient();
    int port = url.getPort();
    if (port == -1) {
        port = url.getDefaultPort();
        if (port == -1) {
            port = 80;
        }
    }
    if ((url.getProtocol().equalsIgnoreCase("https")) && (sslSocketFactory != null)) {
        Protocol https = new Protocol("https",
                (ProtocolSocketFactory) new SSLSocketFactorySSLProtocolSocketFactory(sslSocketFactory), port);
        client.getHostConfiguration().setHost(url.getHost(), url.getPort(), https);
    } else {
        Protocol http = new Protocol("http", new SocketFactoryProtocolSocketFactory(SocketFactory.getDefault()),
                port);
        client.getHostConfiguration().setHost(url.getHost(), url.getPort(), http);
    }
    try {
        List<Proxy> proxies = ProxySelector.getDefault().select(url.toURI());
        for (Proxy p : proxies) {
            if (p.equals(Proxy.NO_PROXY))
                continue;
            if (!p.type().equals(Proxy.Type.HTTP))
                continue;
            InetSocketAddress addr = (InetSocketAddress) p.address();
            client.getHostConfiguration().setProxy(addr.getHostName(), addr.getPort());
            client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
            break;
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return client;
}

From source file:org.elasticsearch.bwcompat.StaticIndexBackwardCompatibilityTest.java

protected static HttpRequestBuilder httpClient() {
    NodeInfo info = nodeInfo(client());/*  ww w. jav  a 2  s.c o m*/
    info.getHttp().address().boundAddress();
    TransportAddress publishAddress = info.getHttp().address().publishAddress();
    assertEquals(1, publishAddress.uniqueAddressTypeId());
    InetSocketAddress address = ((InetSocketTransportAddress) publishAddress).address();
    return new HttpRequestBuilder(HttpClients.createDefault()).host(address.getHostName())
            .port(address.getPort());
}

From source file:com.navercorp.pinpoint.plugin.thrift.ThriftUtils.java

/**
 * Returns the hostname information retrieved from the given {@link SocketAddress}.
 * /*from  w ww.j  ava 2s .  co m*/
 * @param inetSocketAddress the <tt>InetSocketAddress</tt> instance to retrieve the host information from
 * @return the host retrieved from the given <tt>socketAddress</tt>,
 *      or {@literal ThriftConstants.UNKNOWN_ADDRESS} if it cannot be retrieved
 */
// TODO should probably be pulled up as a common API
public static String getHost(SocketAddress socketAddress) {
    if (socketAddress == null) {
        return ThriftConstants.UNKNOWN_ADDRESS;
    }
    if (socketAddress instanceof InetSocketAddress) {
        InetSocketAddress addr = (InetSocketAddress) socketAddress;
        return addr.getHostName();
    }
    return getSocketAddress(socketAddress);
}

From source file:com.navercorp.pinpoint.plugin.thrift.ThriftUtils.java

/**
 * Returns the hostname and port information retrieved from the given {@link SocketAddress}.
 * /* w ww. j  a  v  a  2 s.  c  o  m*/
 * @param inetSocketAddress the <tt>InetSocketAddress</tt> instance to retrieve the host/port information from
 * @return the host/port retrieved from the given <tt>socketAddress</tt>,
 *      or {@literal ThriftConstants.UNKNOWN_ADDRESS} if it cannot be retrieved
 */
// TODO should probably be pulled up as a common API
public static String getHostPort(SocketAddress socketAddress) {
    if (socketAddress == null) {
        return ThriftConstants.UNKNOWN_ADDRESS;
    }
    if (socketAddress instanceof InetSocketAddress) {
        InetSocketAddress addr = (InetSocketAddress) socketAddress;
        return addr.getHostName() + ":" + addr.getPort();
    }
    return getSocketAddress(socketAddress);
}

From source file:org.elasticsearch.rest.action.admin.indices.upgrade.UpgradeReallyOldIndexTest.java

static HttpRequestBuilder httpClient() {
    NodeInfo info = nodeInfo(client());/*from   w w w  .  j a v  a  2 s.c  o m*/
    info.getHttp().address().boundAddress();
    TransportAddress publishAddress = info.getHttp().address().publishAddress();
    assertEquals(1, publishAddress.uniqueAddressTypeId());
    InetSocketAddress address = ((InetSocketTransportAddress) publishAddress).address();
    return new HttpRequestBuilder(HttpClients.createDefault()).host(address.getHostName())
            .port(address.getPort());
}

From source file:org.apache.hadoop.corona.ClusterManagerAvailabilityChecker.java

/**
 * Used for getting a client to the CoronaProxyJobTracker
 * @param conf/*from w w w  .j a v  a  2s.  co m*/
 * @return Returns a client to the CPJT
 * @throws IOException
 */
public static CoronaProxyJobTrackerService.Client getPJTClient(CoronaConf conf) throws IOException {
    InetSocketAddress address = NetUtils.createSocketAddr(conf.getProxyJobTrackerThriftAddress());
    TFramedTransport transport = new TFramedTransport(new TSocket(address.getHostName(), address.getPort()));
    CoronaProxyJobTrackerService.Client client = new CoronaProxyJobTrackerService.Client(
            new TBinaryProtocol(transport));
    try {
        transport.open();
    } catch (TException e) {
        LOG.info("Transport Exception: ", e);
    }
    return client;
}