Example usage for java.net InetSocketAddress getPort

List of usage examples for java.net InetSocketAddress getPort

Introduction

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

Prototype

public final int getPort() 

Source Link

Document

Gets the port number.

Usage

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

/**
 * Attempts to return a usable String given an InetSocketAddress.
 *
 * @param value the InetSocketAddress./*  w  w  w  .ja  va 2 s. c  o 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:jp.go.nict.langrid.management.logic.service.HttpClientUtil.java

/**
 * /*w w w  . j a  v  a  2s .  c o  m*/
 * 
 */
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:eu.esdihumboldt.util.http.client.fluent.FluentProxyUtil.java

/**
 * setup the given request object to go via proxy
 * //from w w  w.jav  a2  s  . 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.servicesupervisor.invocationprocessor.executor.intragrid.HttpClientUtil.java

/**
 * //from   w  ww .j  a v a  2  s.c  o  m
 * 
 */
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:com.navercorp.pinpoint.plugin.thrift.ThriftUtils.java

/**
 * Returns the hostname and port information retrieved from the given {@link SocketAddress}.
 * // w ww  . j  av  a 2 s  .  c om
 * @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.apache.hadoop.corona.ClusterManagerAvailabilityChecker.java

/**
 * Used for getting a client to the CoronaProxyJobTracker
 * @param conf//from w w  w. j  av  a  2  s  .  c  o  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;
}

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

/**
 * Returns the ip and port information retrieved from the given {@link SocketAddress}.
 * /*  w  w  w .j a v a2  s.c o m*/
 * @param socketAddress the <tt>SocketAddress</tt> instance to retrieve the ip/port information from
 * @return the ip/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 getIpPort(SocketAddress socketAddress) {
    if (socketAddress == null) {
        return ThriftConstants.UNKNOWN_ADDRESS;
    }
    if (socketAddress instanceof InetSocketAddress) {
        InetSocketAddress addr = (InetSocketAddress) socketAddress;
        return addr.getAddress().getHostAddress() + ":" + addr.getPort();
    }
    return getSocketAddress(socketAddress);
}

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

protected static HttpRequestBuilder httpClient() {
    NodeInfo info = nodeInfo(client());//from w  w w .  j  a v  a  2 s . co  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.elasticsearch.rest.action.admin.indices.upgrade.UpgradeReallyOldIndexTest.java

static HttpRequestBuilder httpClient() {
    NodeInfo info = nodeInfo(client());//from w w  w.ja va2 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.blackducksoftware.integration.hub.jenkins.helper.BuildHelper.java

public static HubIntRestService getRestService(final IntLogger logger, final String serverUrl,
        final String username, final String password, final int hubTimeout) throws BDJenkinsHubPluginException,
        HubIntegrationException, URISyntaxException, MalformedURLException, BDRestException {
    final HubIntRestService service = new HubIntRestService(serverUrl);
    service.setLogger(logger);//from  www. j  a va2  s  . c  o m
    service.setTimeout(hubTimeout);
    final Jenkins jenkins = Jenkins.getInstance();
    if (jenkins != null) {
        final ProxyConfiguration proxyConfig = jenkins.proxy;
        if (proxyConfig != null) {

            final URL actualUrl = new URL(serverUrl);

            final Proxy proxy = ProxyConfiguration.createProxy(actualUrl.getHost(), proxyConfig.name,
                    proxyConfig.port, proxyConfig.noProxyHost);

            if (proxy.address() != null) {
                final InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
                if (StringUtils.isNotBlank(proxyAddress.getHostName()) && proxyAddress.getPort() != 0) {
                    if (StringUtils.isNotBlank(jenkins.proxy.getUserName())
                            && StringUtils.isNotBlank(jenkins.proxy.getPassword())) {
                        service.setProxyProperties(proxyAddress.getHostName(), proxyAddress.getPort(), null,
                                jenkins.proxy.getUserName(), jenkins.proxy.getPassword());
                    } else {
                        service.setProxyProperties(proxyAddress.getHostName(), proxyAddress.getPort(), null,
                                null, null);
                    }
                    if (logger != null) {
                        logger.debug("Using proxy: '" + proxyAddress.getHostName() + "' at Port: '"
                                + proxyAddress.getPort() + "'");
                    }
                }
            }
        }
    }
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        service.setCookies(username, password);
    }

    return service;
}