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:ch.cyberduck.core.http.HttpConnectionPoolBuilder.java

public HttpConnectionPoolBuilder(final Host host, final ThreadLocalHostnameDelegatingTrustManager trust,
        final X509KeyManager key, final ProxyFinder proxy) {
    this(host, new PlainConnectionSocketFactory() {
        @Override/*  www  . j a v  a 2s .  co m*/
        public Socket createSocket(final HttpContext context) throws IOException {
            // Return socket factory with disabled support for HTTP tunneling as provided internally
            return new ProxySocketFactory(host.getProtocol(), new TrustManagerHostnameCallback() {
                @Override
                public String getTarget() {
                    return context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST).toString();
                }
            }, proxy).disable(Proxy.Type.HTTP).disable(Proxy.Type.HTTPS).createSocket();
        }
    }, new SSLConnectionSocketFactory(new CustomTrustSSLProtocolSocketFactory(trust, key),
            new DisabledX509HostnameVerifier()) {
        @Override
        public Socket createSocket(final HttpContext context) throws IOException {
            return new ProxySocketFactory(host.getProtocol(), new TrustManagerHostnameCallback() {
                @Override
                public String getTarget() {
                    return context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST).toString();
                }
            }, proxy).disable(Proxy.Type.HTTP).disable(Proxy.Type.HTTPS).createSocket();
        }

        @Override
        public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host,
                final InetSocketAddress remoteAddress, final InetSocketAddress localAddress,
                final HttpContext context) throws IOException {
            trust.setTarget(remoteAddress.getHostName());
            return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
        }
    }, proxy);
}

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

HttpRequestBuilder httpClient() {
    InetSocketAddress[] addresses = cluster().httpAddresses();
    InetSocketAddress address = addresses[randomInt(addresses.length - 1)];
    return new HttpRequestBuilder(HttpClients.createDefault()).host(address.getHostName())
            .port(address.getPort());//from  w  w w  .  ja v a 2 s  . c  o  m
}

From source file:oz.hadoop.yarn.api.core.AbstractApplicationMasterLauncher.java

/**
 * /*from   w w w.  ja v  a  2s.c o  m*/
 */
private void initApplicationContainerServer(int applicationContainerCount, boolean finite) {
    this.clientServer = this.buildClientServer(applicationContainerCount, finite);
    InetSocketAddress address = clientServer.start();
    this.applicationSpecification.put(YayaConstants.CLIENT_HOST, address.getHostName());
    this.applicationSpecification.put(YayaConstants.CLIENT_PORT, address.getPort());
}

From source file:org.apache.hadoop.hbase.rest.TestTableResource.java

void checkTableInfo(TableInfoModel model) {
    assertEquals(model.getName(), TABLE);
    Iterator<TableRegionModel> regions = model.getRegions().iterator();
    assertTrue(regions.hasNext());/*  w  w w.  j av  a2 s .c o  m*/
    while (regions.hasNext()) {
        TableRegionModel region = regions.next();
        boolean found = false;
        for (Map.Entry<HRegionInfo, ServerName> e : regionMap.entrySet()) {
            HRegionInfo hri = e.getKey();
            String hriRegionName = hri.getRegionNameAsString();
            String regionName = region.getName();
            if (hriRegionName.equals(regionName)) {
                found = true;
                byte[] startKey = hri.getStartKey();
                byte[] endKey = hri.getEndKey();
                InetSocketAddress sa = new InetSocketAddress(e.getValue().getHostname(),
                        e.getValue().getPort());
                String location = sa.getHostName() + ":" + Integer.valueOf(sa.getPort());
                assertEquals(hri.getRegionId(), region.getId());
                assertTrue(Bytes.equals(startKey, region.getStartKey()));
                assertTrue(Bytes.equals(endKey, region.getEndKey()));
                assertEquals(location, region.getLocation());
                break;
            }
        }
        assertTrue(found);
    }
}

From source file:immf.MyWiser.java

/**
 * Create a new SMTP server with this class as the listener.
 * The default port is 25. Call setPort()/setHostname() before
 * calling start().//ww  w .ja v  a 2s .c o m
 */
public MyWiser(UsernamePasswordValidator userPass, int port, MyWiserMailListener listener,
        final String tlsKeyStore, final String tlsKeyType, final String tlsKeyPasswd) {
    if (tlsKeyStore == null) {
        log.info("SMTP Server disable TLS");
        this.server = new SMTPServer(this, new EasyAuthenticationHandlerFactory(userPass));
        this.server.setHideTLS(true); // TLS?

    } else {
        // TLS
        log.info("SMTP Server enable TLS");
        this.server = new SMTPServer(this, new EasyAuthenticationHandlerFactory(userPass)) {
            public SSLSocket createSSLSocket(Socket socket) throws IOException {
                SSLSocketFactory sf = createSslSocketFactory(tlsKeyStore, tlsKeyType, tlsKeyPasswd);
                InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
                SSLSocket s = (SSLSocket) (sf.createSocket(socket, remoteAddress.getHostName(),
                        socket.getPort(), true));

                s.setUseClientMode(false);

                s.setEnabledCipherSuites(s.getSupportedCipherSuites());

                return s;
            }
        };
        this.server.setRequireTLS(true); // TLS
    }
    this.server.setPort(port);
    this.listener = listener;
}

From source file:org.talend.core.nexus.HttpClientTransport.java

private IProxySelectorProvider addProxy(final DefaultHttpClient httpClient, URI requestURI) {
    IProxySelectorProvider proxySelectorProvider = null;
    try {// w ww  .  ja va  2s  .c o m
        if (Boolean.valueOf(
                System.getProperty(PROP_PROXY_HTTP_CLIENT_USE_DEFAULT_SETTINGS, Boolean.FALSE.toString()))) {
            return proxySelectorProvider;
        }
        final List<Proxy> proxyList = TalendProxySelector.getInstance().getDefaultProxySelector()
                .select(requestURI);
        Proxy usedProxy = null;
        if (proxyList != null && !proxyList.isEmpty()) {
            usedProxy = proxyList.get(0);
        }

        if (usedProxy != null) {
            if (Type.DIRECT.equals(usedProxy.type())) {
                return proxySelectorProvider;
            }
            final Proxy finalProxy = usedProxy;
            InetSocketAddress address = (InetSocketAddress) finalProxy.address();
            String proxyServer = address.getHostName();
            int proxyPort = address.getPort();
            PasswordAuthentication proxyAuthentication = Authenticator.requestPasswordAuthentication(
                    proxyServer, address.getAddress(), proxyPort, "Http Proxy", "Http proxy authentication",
                    null);
            if (proxyAuthentication != null) {
                String proxyUser = proxyAuthentication.getUserName();
                if (StringUtils.isNotBlank(proxyUser)) {
                    String proxyPassword = "";
                    char[] passwordChars = proxyAuthentication.getPassword();
                    if (passwordChars != null) {
                        proxyPassword = new String(passwordChars);
                    }
                    httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyServer, proxyPort),
                            new UsernamePasswordCredentials(proxyUser, proxyPassword));
                }
            }
            HttpHost proxyHost = new HttpHost(proxyServer, proxyPort);
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);
            proxySelectorProvider = createProxySelectorProvider();
        }
        return proxySelectorProvider;
    } finally {
        if (proxySelectorProvider != null) {
            TalendProxySelector.getInstance().addProxySelectorProvider(proxySelectorProvider);
        }
    }
}

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

/**
 * Using {@link GetConf} methods get the list of given {@code type} of
 * addresses//  w  w  w.  j  a v  a2s. c om
 * 
 * @param type, TestType
 * @param conf, configuration
 * @param checkPort, If checkPort is true, verify NNPRCADDRESSES whose 
 *      expected value is hostname:rpc-port.  If checkPort is false, the 
 *      expected is hostname only.
 * @param expected, expected addresses
 */
private void getAddressListFromTool(TestType type, HdfsConfiguration conf, boolean checkPort,
        List<InetSocketAddress> expected) throws Exception {
    String out = getAddressListFromTool(type, conf, expected.size() != 0);
    List<String> values = new ArrayList<String>();

    // Convert list of addresses returned to an array of string
    StringTokenizer tokenizer = new StringTokenizer(out);
    while (tokenizer.hasMoreTokens()) {
        String s = tokenizer.nextToken().trim();
        values.add(s);
    }
    String[] actual = values.toArray(new String[values.size()]);

    // Convert expected list to String[] of hosts
    int i = 0;
    String[] expectedHosts = new String[expected.size()];
    for (InetSocketAddress addr : expected) {
        if (!checkPort) {
            expectedHosts[i++] = addr.getHostName();
        } else {
            expectedHosts[i++] = addr.getHostName() + ":" + addr.getPort();
        }
    }

    // Compare two arrays
    assertTrue(Arrays.equals(expectedHosts, actual));
}

From source file:org.apache.tajo.ws.rs.netty.NettyRestHandlerContainer.java

private URI getBaseUri(ChannelHandlerContext ctx, FullHttpRequest request) {
    URI baseUri;//  w  w  w.ja v a2 s. c  o  m
    String scheme;

    if (ctx.pipeline().get(SslHandler.class) == null) {
        scheme = "http";
    } else {
        scheme = "https";
    }

    List<String> hosts = request.headers().getAll(HttpHeaders.Names.HOST);
    try {
        if (hosts != null && hosts.size() > 0) {
            baseUri = new URI(scheme + "://" + hosts.get(0) + rootPath);
        } else {
            InetSocketAddress localAddress = (InetSocketAddress) ctx.channel().localAddress();
            baseUri = new URI(scheme, null, localAddress.getHostName(), localAddress.getPort(), rootPath, null,
                    null);
        }
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }

    return baseUri;
}

From source file:org.apache.tajo.cli.tools.TajoHAAdmin.java

public void runCommand(String[] args) throws Exception {
    if (args.length == 1 && (args[0].equalsIgnoreCase("-transitionToActive")
            || args[0].equalsIgnoreCase("-transitionToBackup") || args[0].equalsIgnoreCase("-getState"))) {
        writer.write("Not enough arguments: expected 1 but got 0\n");
        writer.flush();//w  w  w. j  a  v  a  2  s .  c om
        return;
    }

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);

    String param = "";
    int cmdType = 0;

    String hostName = null;
    Integer port = null;
    if (cmd.hasOption("h")) {
        hostName = cmd.getOptionValue("h");
    }
    if (cmd.hasOption("p")) {
        port = Integer.parseInt(cmd.getOptionValue("p"));
    }

    if (cmd.hasOption("transitionToActive")) {
        cmdType = 1;
        param = cmd.getOptionValue("transitionToActive");
    } else if (cmd.hasOption("transitionToBackup")) {
        cmdType = 2;
        param = cmd.getOptionValue("transitionToBackup");
    } else if (cmd.hasOption("getState")) {
        cmdType = 3;
        param = cmd.getOptionValue("getState");
    } else if (cmd.hasOption("formatHA")) {
        cmdType = 4;
    }

    // if there is no "-h" option,
    InetSocketAddress address = tajoConf.getSocketAddrVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS,
            TajoConf.ConfVars.TAJO_MASTER_UMBILICAL_RPC_ADDRESS);

    if (hostName == null) {
        hostName = address.getHostName();
    }

    if (port == null) {
        port = address.getPort();
    }

    if (cmdType == 0) {
        printUsage();
        return;
    }

    if ((hostName == null) ^ (port == null)) {
        System.err.println("ERROR: cannot find valid Tajo server address");
        return;
    } else if (hostName != null && port != null) {
        tajoConf.setVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS,
                NetUtils.getHostPortString(hostName, port));
    }

    if (!tajoConf.getBoolVar(TajoConf.ConfVars.TAJO_MASTER_HA_ENABLE)) {
        writer.write("HA is not enabled for this tajo cluster.");
    } else {
        switch (cmdType) {
        case 1:
            writer.write("Not Yet Implemented\n");
            break;
        case 2:
            writer.write("Not Yet Implemented\n");
            break;
        case 3:
            getState(writer, param);
            break;
        case 4:
            formatHA(writer);
            break;
        default:
            printUsage();
            break;
        }
    }

    writer.flush();
}

From source file:co.rsk.net.discovery.PeerExplorer.java

public PongPeerMessage sendPong(InetSocketAddress nodeAddress, String id) {
    InetSocketAddress localAddress = this.localNode.getAddress();
    PongPeerMessage pongPeerMessage = PongPeerMessage.create(localAddress.getHostName(), localAddress.getPort(),
            id, this.key);
    udpChannel.write(new DiscoveryEvent(pongPeerMessage, nodeAddress));
    return pongPeerMessage;
}