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:au.com.redboxresearchdata.harvester.httpclient.BasicHttpClient.java

/**
 * Gets an HTTP client. If authentication is required, the authenticate()
 * method must be called prior to this method.
 * /*from w w  w  .j av  a2  s. co  m*/
 * @param auth set true to use authentication, false to skip authentication
 * @return an HTTP client
 */
public HttpClient getHttpClient(boolean auth) {
    HttpClient client = new HttpClient();
    try {
        URL url = new URL(baseUrl);
        //        log.info(baseUrl + "----------------------------1111------------");
        Proxy proxy = ProxySelector.getDefault().select(url.toURI()).get(0);
        if (!proxy.type().equals(Proxy.Type.DIRECT)) {
            InetSocketAddress address = (InetSocketAddress) proxy.address();
            String proxyHost = address.getHostName();
            int proxyPort = address.getPort();
            client.getHostConfiguration().setProxy(proxyHost, proxyPort);
            //          log.trace("Using proxy {}:{}", proxyHost, proxyPort);
        }
    } catch (Exception e) {
        //    log.warn("Failed to get proxy settings: " + e.getMessage());
    }
    if (auth && credentials != null) {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, credentials);
        //  log.trace("Credentials: username={}", credentials.getUserName());
    }
    return client;
}

From source file:org.apache.htrace.impl.TestHTracedReceiverConf.java

@Test(timeout = 60000)
public void testParseHostPort() throws Exception {
    InetSocketAddress addr = new Conf(
            HTraceConfiguration.fromKeyValuePairs(Conf.ADDRESS_KEY, "example.com:8080")).endpoint;
    Assert.assertEquals("example.com", addr.getHostName());
    Assert.assertEquals(8080, addr.getPort());

    addr = new Conf(HTraceConfiguration.fromKeyValuePairs(Conf.ADDRESS_KEY, "127.0.0.1:8081")).endpoint;
    Assert.assertEquals("127.0.0.1", addr.getHostName());
    Assert.assertEquals(8081, addr.getPort());

    addr = new Conf(
            HTraceConfiguration.fromKeyValuePairs(Conf.ADDRESS_KEY, "[ff02:0:0:0:0:0:0:12]:9096")).endpoint;
    Assert.assertEquals("ff02:0:0:0:0:0:0:12", addr.getHostName());
    Assert.assertEquals(9096, addr.getPort());
}

From source file:com.ksc.http.conn.ssl.SdkTLSSocketFactory.java

/**
 * Invalidates all SSL/TLS sessions in {@code sessionContext} associated with {@code remoteAddress}.
 *
 * @param sessionContext collection of SSL/TLS sessions to be (potentially) invalidated
 * @param remoteAddress  associated with sessions to invalidate
 *///w  w w . ja  va  2  s.  c om
private void clearSessionCache(final SSLSessionContext sessionContext, final InetSocketAddress remoteAddress) {
    final String hostName = remoteAddress.getHostName();
    final int port = remoteAddress.getPort();
    final Enumeration<byte[]> ids = sessionContext.getIds();

    if (ids == null) {
        return;
    }

    while (ids.hasMoreElements()) {
        final byte[] id = ids.nextElement();
        final SSLSession session = sessionContext.getSession(id);
        if (session != null && session.getPeerHost() != null && session.getPeerHost().equalsIgnoreCase(hostName)
                && session.getPeerPort() == port) {
            session.invalidate();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Invalidated session " + session);
            }
        }
    }
}

From source file:com.facebook.infrastructure.net.io.ContentStreamState.java

public byte[] read() throws IOException, ReadNotCompleteException {
    SocketChannel socketChannel = stream_.getStream();
    InetSocketAddress remoteAddress = (InetSocketAddress) socketChannel.socket().getRemoteSocketAddress();
    String remoteHost = remoteAddress.getHostName();
    createFileChannel();/*from ww w.  j a  v  a  2 s .  c o m*/
    if (streamContext_ != null) {
        try {
            bytesRead_ += fc_.transferFrom(socketChannel, bytesRead_, ContentStreamState.count_);
            if (bytesRead_ != streamContext_.getExpectedBytes())
                throw new ReadNotCompleteException(
                        "Specified number of bytes have not been read from the Socket Channel");
        } catch (IOException ex) {
            /* Ask the source node to re-stream this file. */
            streamStatus_.setAction(StreamContextManager.StreamCompletionAction.STREAM);
            handleStreamCompletion(remoteHost);
            /* Delete the orphaned file. */
            File file = new File(streamContext_.getTargetFile());
            file.delete();
            throw ex;
        }
        if (bytesRead_ == streamContext_.getExpectedBytes()) {
            logger_.debug("Removing stream context " + streamContext_);
            handleStreamCompletion(remoteHost);
            bytesRead_ = 0L;
            fc_.close();
            morphState();
        }
    }

    return ArrayUtils.EMPTY_BYTE_ARRAY;
}

From source file:org.apache.hama.bsp.message.HamaAsyncMessageManagerImpl.java

private final void startRPCServer(Configuration conf, InetSocketAddress peerAddress) {
    try {/*from   ww w .ja va  2s. co m*/
        startServer(peerAddress.getHostName(), peerAddress.getPort());
    } catch (IOException ioe) {
        LOG.error("Fail to start RPC server!", ioe);
        throw new RuntimeException("RPC Server could not be launched!");
    }
}

From source file:com.marvelution.hudson.plugins.apiv2.client.connectors.HttpClient3Connector.java

/**
 * Obtains a host from an {@link InetSocketAddress}.
 * // w ww.  j  a v  a  2s  .  c  o m
 * @param isa the socket address
 * @return a host string, either as a symbolic name or as a literal IP address string
 */
protected String getHost(InetSocketAddress isa) {
    return isa.isUnresolved() ? isa.getHostName() : isa.getAddress().getHostAddress();
}

From source file:at.bitfire.davdroid.webdav.TlsSniSocketFactoryTest.java

public void testUntrustedCertificate() throws IOException {
    try {/*from   ww  w.  ja  va 2s  . c  o  m*/
        // host with certificate that is not trusted by default
        InetSocketAddress host = new InetSocketAddress("cacert.org", 443);

        @Cleanup
        Socket socket = factory.connectSocket(0, null, new HttpHost(host.getHostName()), host, null, null);
        fail();
    } catch (SSLHandshakeException e) {
        Log.i(TAG, "Expected exception", e);
        assertFalse(ExceptionUtils.indexOfType(e, CertPathValidatorException.class) == -1);
    }
}

From source file:org.apache.hadoop.hdfs.tools.DFSZKFailoverController.java

@Override
protected byte[] targetToData(HAServiceTarget target) {
    InetSocketAddress addr = target.getAddress();

    return ActiveNodeInfo.newBuilder().setHostname(addr.getHostName()).setPort(addr.getPort())
            .setZkfcPort(target.getZKFCAddress().getPort()).setNameserviceId(localNNTarget.getNameServiceId())
            .setNamenodeId(localNNTarget.getNameNodeId()).build().toByteArray();
}

From source file:com.sonian.elasticsearch.http.jetty.HttpClient.java

public HttpClient(TransportAddress transportAddress, String username, String password) {
    InetSocketAddress address = ((InetSocketTransportAddress) transportAddress).address();
    try {//from   w  ww  . j a v a  2s . c om
        baseUrl = new URL("http", address.getHostName(), address.getPort(), "/");
    } catch (MalformedURLException e) {
        throw new ElasticsearchException("", e);
    }
    if (username != null) {
        BASE64Encoder enc = new BASE64Encoder();
        String userPassword = username + ":" + password;
        encodedAuthorization = enc.encode(userPassword.getBytes());
    } else {
        encodedAuthorization = null;
    }
}

From source file:com.datatorrent.stram.engine.StreamContext.java

public InetSocketAddress getBufferServerAddress() {
    InetSocketAddress isa = get(BUFFER_SERVER_ADDRESS);
    return new InetSocketAddress(isa.getHostName(), isa.getPort());
}