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:io.tourniquet.junit.http.rules.HttpExchangeTest.java

@Test
public void testGetSourceAddress() throws Exception {
    //prepare//from  w w w  . j  a  v  a  2  s . c om
    exchange.setSourceAddress(InetSocketAddress.createUnresolved("somehost", 12345));

    //act
    InetSocketAddress source = subject.getSourceAddress();

    //assert
    assertNotNull(source);
    assertEquals("somehost", source.getHostName());
    assertEquals(12345, source.getPort());
}

From source file:io.tourniquet.junit.http.rules.HttpExchangeTest.java

@Test
public void testGetDestinationAddress() throws Exception {
    //prepare/*from ww w.  ja v a  2  s .co  m*/
    exchange.setDestinationAddress(InetSocketAddress.createUnresolved("somehost", 12345));

    //act
    InetSocketAddress dest = subject.getDestinationAddress();

    //assert
    assertNotNull(dest);
    assertEquals("somehost", dest.getHostName());
    assertEquals(12345, dest.getPort());
}

From source file:io.crate.integrationtests.BlobHttpIntegrationTest.java

public List<String> getRedirectLocations(CloseableHttpClient client, String uri, InetSocketAddress address)
        throws IOException {
    CloseableHttpResponse response = null;
    try {/*from  w  w w.j  a v a  2 s.c  o m*/
        HttpClientContext context = HttpClientContext.create();
        HttpHead httpHead = new HttpHead(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s",
                address.getHostName(), address.getPort(), uri));
        response = client.execute(httpHead, context);

        List<URI> redirectLocations = context.getRedirectLocations();
        if (redirectLocations == null) {
            // client might not follow redirects automatically
            if (response.containsHeader("location")) {
                List<String> redirects = new ArrayList<>(1);
                for (Header location : response.getHeaders("location")) {
                    redirects.add(location.getValue());
                }
                return redirects;
            }
            return Collections.emptyList();
        }

        List<String> redirects = new ArrayList<>(1);
        for (URI redirectLocation : redirectLocations) {
            redirects.add(redirectLocation.toString());
        }
        return redirects;
    } finally {
        if (response != null) {
            IOUtils.closeWhileHandlingException(response);
        }
    }
}

From source file:org.apache.flink.yarn.CliFrontendYarnAddressConfigurationTest.java

@Test
public void testManualOptionsOverridesYarn() throws Exception {

    File emptyFolder = temporaryFolder.newFolder();
    File testConfFile = new File(emptyFolder.getAbsolutePath(), "flink-conf.yaml");
    Files.createFile(testConfFile.toPath());

    TestCLI frontend = new TestCLI(emptyFolder.getAbsolutePath());

    RunOptions options = CliFrontendParser.parseRunCommand(new String[] { "-m", "10.221.130.22:7788" });

    frontend.retrieveClient(options);/*ww  w.j av  a  2 s  .  c  om*/

    Configuration config = frontend.getConfiguration();

    InetSocketAddress expectedAddress = InetSocketAddress.createUnresolved("10.221.130.22", 7788);

    checkJobManagerAddress(config, expectedAddress.getHostName(), expectedAddress.getPort());

}

From source file:org.mule.transport.http.HttpServerConnection.java

/**
 * @return the uri for the request including scheme, host, port and path. i.e: http://192.168.1.1:7777/service/orders
 * @throws IOException/*from ww  w .  j a va2  s . c  om*/
 */
public String getFullUri() throws IOException {
    String scheme = "http";
    if (socket instanceof SSLSocket) {
        scheme = "https";
    }
    InetSocketAddress localSocketAddress = (InetSocketAddress) socket.getLocalSocketAddress();
    return String.format("%s://%s:%d%s", scheme, localSocketAddress.getHostName(), localSocketAddress.getPort(),
            readRequest().getUrlWithoutParams());
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestDatanodeJsp.java

private static void testViewingFile(MiniDFSCluster cluster, String filePath) throws IOException {
    FileSystem fs = cluster.getFileSystem();

    Path testPath = new Path(filePath);
    if (!fs.exists(testPath)) {
        DFSTestUtil.writeFile(fs, testPath, FILE_DATA);
    }//from   w  ww  .  j  av a 2s.  com

    InetSocketAddress nnIpcAddress = cluster.getNameNode().getNameNodeAddress();
    InetSocketAddress nnHttpAddress = cluster.getNameNode().getHttpAddress();
    String base = JspHelper.Url.url("http", cluster.getDataNodes().get(0).getDatanodeId());

    URL url = new URL(base + "/" + "browseDirectory.jsp"
            + JspHelper.getUrlParam("dir", URLEncoder.encode(testPath.toString(), "UTF-8"), true)
            + JspHelper.getUrlParam("namenodeInfoPort", Integer.toString(nnHttpAddress.getPort()))
            + JspHelper.getUrlParam("nnaddr", "localhost:" + nnIpcAddress.getPort()));

    viewFilePage = StringEscapeUtils.unescapeHtml(DFSTestUtil.urlGet(url));

    assertTrue("page should show preview of file contents, got: " + viewFilePage,
            viewFilePage.contains(FILE_DATA));

    assertTrue("page should show link to download file", viewFilePage.contains(
            "/streamFile" + ServletUtil.encodePath(filePath) + "?nnaddr=localhost:" + nnIpcAddress.getPort()));

    // check whether able to tail the file
    String regex = "<a.+href=\"(.+?)\">Tail\\s*this\\s*file\\<\\/a\\>";
    assertFileContents(regex, "Tail this File");

    // check whether able to 'Go Back to File View' after tailing the file
    regex = "<a.+href=\"(.+?)\">Go\\s*Back\\s*to\\s*File\\s*View\\<\\/a\\>";
    assertFileContents(regex, "Go Back to File View");

    regex = "<a href=\"///" + nnHttpAddress.getHostName() + ":" + nnHttpAddress.getPort()
            + "/dfshealth.jsp\">Go back to DFS home</a>";
    assertTrue("page should generate DFS home scheme without explicit scheme", viewFilePage.contains(regex));
}

From source file:org.cloudata.core.common.ipc.CServer.java

/**
 * A convience method to bind to a given address and report 
 * better exceptions if the address is not a valid host.
 * @param socket the socket to bind/*from   w w w . ja  v  a2s.c om*/
 * @param address the address to bind to
 * @param backlog the number of connections allowed in the queue
 * @throws BindException if the address can't be bound
 * @throws UnknownHostException if the address isn't a valid host name
 * @throws IOException other random errors from bind
 */
static void bind(ServerSocket socket, InetSocketAddress address, int backlog) throws IOException {
    try {
        socket.bind(address, backlog);
    } catch (BindException e) {
        throw new BindException("Problem binding to " + address + "," + e.getMessage());
    } catch (SocketException e) {
        // If they try to bind to a different host's address, give a better
        // error message.
        if ("Unresolved address".equals(e.getMessage())) {
            throw new UnknownHostException("Invalid hostname for server: " + address.getHostName());
        } else {
            throw e;
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.ha.BootstrapStandby.java

@Override
public int run(String[] args) throws Exception {
    parseArgs(args);//from  www .  j a va 2s.co m
    parseConfAndFindOtherNN();
    NameNode.checkAllowFormat(conf);

    InetSocketAddress myAddr = NameNode.getAddress(conf);
    SecurityUtil.login(conf, DFS_NAMENODE_KEYTAB_FILE_KEY, DFS_NAMENODE_KERBEROS_PRINCIPAL_KEY,
            myAddr.getHostName());

    return SecurityUtil.doAsLoginUserOrFatal(new PrivilegedAction<Integer>() {
        @Override
        public Integer run() {
            try {
                return doRun();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}

From source file:org.apache.tez.dag.app.rm.TaskSchedulerEventHandler.java

@Override
public synchronized void serviceStart() {
    // FIXME hack alert how is this supposed to support multiple DAGs?
    // Answer: this is shared across dags. need job==app-dag-master
    // TODO set heartbeat value from conf here
    InetSocketAddress serviceAddr = clientService.getBindAddress();
    taskScheduler = new TaskScheduler(this, serviceAddr.getHostName(), serviceAddr.getPort(), "");
    taskScheduler.init(getConfig());/*from   ww w. j  a  va2  s . c  o m*/

    dagAppMaster = appContext.getAppMaster();
    taskScheduler.start();
    this.eventHandlingThread = new Thread() {
        @Override
        public void run() {

            AMSchedulerEvent event;

            while (!stopEventHandling && !Thread.currentThread().isInterrupted()) {
                try {
                    event = TaskSchedulerEventHandler.this.eventQueue.take();
                } catch (InterruptedException e) {
                    LOG.error("Returning, interrupted : " + e);
                    continue;
                }

                try {
                    handleEvent(event);
                } catch (Throwable t) {
                    LOG.error("Error in handling event type " + event.getType() + " to the TaskScheduler", t);
                    // Kill the AM.
                    sendEvent(new DAGAppMasterEvent(DAGAppMasterEventType.INTERNAL_ERROR));
                    return;
                }
            }
        }
    };
    this.eventHandlingThread.start();
}

From source file:org.apache.hadoop.yarn.factories.impl.pb.RpcServerFactoryPBImpl.java

private Server createServer(Class<?> pbProtocol, InetSocketAddress addr, Configuration conf,
        SecretManager<? extends TokenIdentifier> secretManager, int numHandlers,
        BlockingService blockingService, String portRangeConfig) throws IOException {
    RPC.setProtocolEngine(conf, pbProtocol, ProtobufRpcEngine.class);
    RPC.Server server = new RPC.Builder(conf).setProtocol(pbProtocol).setInstance(blockingService)
            .setBindAddress(addr.getHostName()).setPort(addr.getPort()).setNumHandlers(numHandlers)
            .setVerbose(false).setSecretManager(secretManager).setPortRangeConfig(portRangeConfig).build();
    LOG.info("Adding protocol " + pbProtocol.getCanonicalName() + " to the server");
    server.addProtocol(RPC.RpcKind.RPC_PROTOCOL_BUFFER, pbProtocol, blockingService);
    return server;
}