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.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 w w  w  .  ja  v a2 s. co 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;
}

From source file:eu.esdihumboldt.util.http.client.ClientProxyUtil.java

/**
 * Set-up the given HTTP client to use the given proxy
 * /* w  w w .j  av a 2  s  .c  o  m*/
 * @param builder the HTTP client builder
 * @param proxy the proxy
 * @return the client builder adapted with the proxy settings
 */
public static HttpClientBuilder applyProxy(HttpClientBuilder builder, Proxy proxy) {
    ProxyUtil.init();

    // check if proxy shall be used
    if (proxy != null && proxy.type() == Type.HTTP) {
        InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();

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

        String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$
        String password = System.getProperty("http.proxyPassword"); //$NON-NLS-1$
        boolean useProxyAuth = user != null && !user.isEmpty();

        if (useProxyAuth) {
            // set the proxy credentials
            CredentialsProvider credsProvider = new BasicCredentialsProvider();

            credsProvider.setCredentials(new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort()),
                    createCredentials(user, password));
            builder = builder.setDefaultCredentialsProvider(credsProvider)
                    .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
        }

        _log.trace("Set proxy to " + proxyAddress.getHostName() + ":" + //$NON-NLS-1$ //$NON-NLS-2$
                proxyAddress.getPort() + ((useProxyAuth) ? (" as user " + user) : (""))); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return builder;
}

From source file:com.kixeye.chassis.transport.shared.JettyConnectorRegistry.java

/**
 * Register to listen to HTTP./*from   www  . j  a  va  2s .  c  om*/
 * 
 * @param server
 * @param address
 */
public static void registerHttpConnector(Server server, InetSocketAddress address) {
    ServerConnector connector = new ServerConnector(server);
    connector.setHost(address.getHostName());
    connector.setPort(address.getPort());

    server.addConnector(connector);
}

From source file:org.stem.utils.Utils.java

public static String extractHostAddr(InetSocketAddress address) {
    if (null != address.getAddress())
        return address.getAddress().getHostAddress();
    else if (null != address.getHostName()) {
        if (address.getHostName().contains("/")) {
            int index = address.getHostName().indexOf("/");
            return address.getHostName().substring(index + 1);
        } else/*www.j ava2 s.  com*/
            return address.getHostName();
    } else
        throw new RuntimeException("Can not extract ip address");
}

From source file:com.helger.httpclient.HttpClientHelper.java

@Nullable
public static HttpHost createHttpHost(@Nullable final Proxy aProxy) {
    if (aProxy != null && aProxy.type() == Proxy.Type.HTTP) {
        if (aProxy.address() instanceof InetSocketAddress) {
            final InetSocketAddress aISA = (InetSocketAddress) aProxy.address();
            return new HttpHost(aISA.getHostName(), aISA.getPort());
        }/*from   w  w  w .  j a  va2  s  . c o m*/
    }
    return null;
}

From source file:org.apache.hadoop.hdfs.web.TestWebHdfsWithMultipleNameNodes.java

private static void setupCluster(final int nNameNodes, final int nDataNodes) throws Exception {
    LOG.info("nNameNodes=" + nNameNodes + ", nDataNodes=" + nDataNodes);

    conf.setBoolean(DFSConfigKeys.DFS_WEBHDFS_ENABLED_KEY, true);

    cluster = new MiniDFSCluster.Builder(conf).nnTopology(MiniDFSNNTopology.simpleHOPSTopology(nNameNodes))
            .numDataNodes(nDataNodes).format(true).build();
    cluster.waitActive();/* w w  w .j a  v a  2  s. c  om*/

    webhdfs = new WebHdfsFileSystem[nNameNodes];
    for (int i = 0; i < webhdfs.length; i++) {
        final InetSocketAddress addr = cluster.getNameNode(i).getHttpAddress();
        final String uri = WebHdfsFileSystem.SCHEME + "://" + addr.getHostName() + ":" + addr.getPort() + "/";
        webhdfs[i] = (WebHdfsFileSystem) FileSystem.get(new URI(uri), conf);
    }
}

From source file:co.cask.tigon.test.SQLFlowTestBase.java

/**
 * This function deploys an instance of the flowClass.
 * Recommended, make this function call from a {@link org.junit.BeforeClass} annotated method.
 * @param flowClass Class of the {@link co.cask.tigon.api.flow.Flow} to be deployed
 * @throws Exception/*from  w w w  .  j a v  a2s . c  o  m*/
 */
public static void setupFlow(Class<? extends Flow> flowClass) throws Exception {
    Map<String, String> runtimeArgs = Maps.newHashMap();
    handler = new TestHandler();
    service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(handler))
            .setPort(Networks.getRandomPort()).build();
    service.startAndWait();
    InetSocketAddress address = service.getBindAddress();
    serviceURL = "http://" + address.getHostName() + ":" + address.getPort() + "/queue";
    runtimeArgs.put("baseURL", serviceURL);
    runtimeArgs.put(Constants.HTTP_PORT, Integer.toString(httpPort));
    flowManager = deployFlow(flowClass, runtimeArgs);
    int maxWait = 100;
    // Waiting for the Tigon SQL Flow initialization
    while ((!flowManager.discover(Constants.HTTP_PORT).iterator().hasNext()) && (maxWait > 0)) {
        TimeUnit.SECONDS.sleep(1);
        maxWait = maxWait - 1;
    }
    if (maxWait <= 0) {
        throw new TimeoutException("Timeout Error, Tigon SQL flow took too long to initiate");
    }
}

From source file:eu.esdihumboldt.util.http.ProxyUtil.java

/**
 * Set-up the given HTTP client to use the given proxy
 * /* w w  w  .  j  a  va  2  s  . c o  m*/
 * @param builder the HTTP client builder
 * @param proxy the proxy
 * @return the client builder adapted with the proxy settings
 */
public static HttpClientBuilder applyProxy(HttpClientBuilder builder, Proxy proxy) {
    init();

    // check if proxy shall be used
    if (proxy != null && proxy.type() == Type.HTTP) {
        InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();

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

        String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$
        String password = System.getProperty("http.proxyPassword"); //$NON-NLS-1$
        boolean useProxyAuth = user != null && !user.isEmpty();

        if (useProxyAuth) {
            // set the proxy credentials
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort()),
                    new UsernamePasswordCredentials(user, password));
            builder = builder.setDefaultCredentialsProvider(credsProvider)
                    .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
        }

        _log.trace("Set proxy to " + proxyAddress.getHostName() + ":" + //$NON-NLS-1$ //$NON-NLS-2$
                proxyAddress.getPort() + ((useProxyAuth) ? (" as user " + user) : (""))); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return builder;
}

From source file:org.elasticsearch.network.DirectBufferNetworkTests.java

private static HttpRequestBuilder httpClient() {
    HttpServerTransport httpServerTransport = internalCluster().getDataNodeInstance(HttpServerTransport.class);
    InetSocketAddress address = ((InetSocketTransportAddress) httpServerTransport.boundAddress()
            .publishAddress()).address();
    return new HttpRequestBuilder(HttpClients.createDefault()).host(address.getHostName())
            .port(address.getPort());/*  w  w w  . j  ava  2 s . co  m*/
}

From source file:org.springframework.cloud.aws.core.env.ec2.AmazonEc2InstanceDataPropertySourceTest.java

@BeforeClass
public static void setupHttpServer() throws Exception {
    InetSocketAddress address = new InetSocketAddress(HTTP_SERVER_TEST_PORT);
    httpServer = HttpServer.create(address, -1);
    httpServer.start();// w w w.j  ava2s  .  c o m
    overwriteMetadataEndpointUrl("http://" + address.getHostName() + ":" + address.getPort());
}