Example usage for java.net InetSocketAddress createUnresolved

List of usage examples for java.net InetSocketAddress createUnresolved

Introduction

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

Prototype

public static InetSocketAddress createUnresolved(String host, int port) 

Source Link

Document

Creates an unresolved socket address from a hostname and a port number.

Usage

From source file:org.tinymediamanager.scraper.http.TmmHttpClient.java

private static void setProxy(OkHttpClient.Builder builder) {
    Proxy proxyHost;/*from w  w w.j av  a  2s .  co  m*/

    if (ProxySettings.INSTANCE.getPort() > 0) {
        proxyHost = new Proxy(Proxy.Type.HTTP, InetSocketAddress
                .createUnresolved(ProxySettings.INSTANCE.getHost(), ProxySettings.INSTANCE.getPort()));
    } else if (StringUtils.isNotBlank(ProxySettings.INSTANCE.getHost())) {
        proxyHost = new Proxy(Proxy.Type.HTTP,
                InetSocketAddress.createUnresolved(ProxySettings.INSTANCE.getHost(), 80));
    } else {
        // no proxy settings found. return
        return;
    }

    builder.proxy(proxyHost);
    // authenticate
    if (StringUtils.isNotBlank(ProxySettings.INSTANCE.getUsername())
            && StringUtils.isNotBlank(ProxySettings.INSTANCE.getPassword())) {
        builder.authenticator(new Authenticator() {
            @Override
            public Request authenticate(Route route, Response response) throws IOException {
                String credential = Credentials.basic(ProxySettings.INSTANCE.getUsername(),
                        ProxySettings.INSTANCE.getPassword());
                return response.request().newBuilder().header("Proxy-Authorization", credential).build();
            }
        });
    }
}

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

/**
 * Attempts to parse an endpoint spec into an InetSocketAddress.
 *
 * @param value the endpoint spec/*from w  ww .  ja v  a 2  s  . co m*/
 * @return a parsed InetSocketAddress
 * @throws NullPointerException     if {@code value} is {@code null}
 * @throws IllegalArgumentException if {@code value} cannot be parsed
 */
public static InetSocketAddress parse(String value) {
    Preconditions.checkNotNull(value);

    String[] spec = value.split(":", 2);
    if (spec.length != 2) {
        throw new IllegalArgumentException("Invalid socket address spec: " + value);
    }

    String host = spec[0];
    int port = asPort(spec[1]);

    return StringUtils.isEmpty(host) ? new InetSocketAddress(port)
            : InetSocketAddress.createUnresolved(host, port);
}

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

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

    //act
    String host = subject.getHost();

    //assert
    assertEquals("somehost", host);
}

From source file:org.apache.qpid.server.management.plugin.HttpManagementUtil.java

public static SocketAddress getSocketAddress(HttpServletRequest request) {
    return InetSocketAddress.createUnresolved(request.getServerName(), request.getServerPort());
}

From source file:dk.alexandra.fresco.suite.tinytables.prepro.TinyTablesPreproConfiguration.java

public static ProtocolSuiteConfiguration fromCmdLine(SCEConfiguration sceConf, CommandLine cmd)
        throws ParseException, IllegalArgumentException {

    Options options = new Options();

    TinyTablesPreproConfiguration configuration = new TinyTablesPreproConfiguration();

    /*//from w  w w .  java2  s.  c  om
     * Parse TinyTables specific options
     */

    String otExtensionOption = "tinytables.otExtension";
    String otExtensionPortOption = "tinytables.otExtensionPort";
    String tinytablesFileOption = "tinytables.file";

    options.addOption(Option.builder("D").desc(
            "Specify whether we should try to use SCAPIs OT-Extension lib. This requires the SCAPI library to be installed.")
            .longOpt(otExtensionOption).required(false).hasArgs().build());

    options.addOption(Option.builder("D")
            .desc("The port number for the OT-Extension lib. Both players should specify the same here.")
            .longOpt(otExtensionPortOption).required(false).hasArgs().build());

    options.addOption(Option.builder("D").desc("The file where the generated TinyTables should be stored.")
            .longOpt(tinytablesFileOption).required(false).hasArgs().build());

    Properties p = cmd.getOptionProperties("D");

    boolean useOtExtension = Boolean.parseBoolean(p.getProperty(otExtensionOption, "false"));
    configuration.setUseOtExtension(useOtExtension);

    int otExtensionPort = Integer.parseInt(p.getProperty(otExtensionPortOption, "9005"));
    Party sender = sceConf.getParties().get(1);
    configuration.setSenderAddress(InetSocketAddress.createUnresolved(sender.getHostname(), otExtensionPort));
    ;

    String tinyTablesFilePath = p.getProperty(tinytablesFileOption, "tinytables");
    File tinyTablesFile = new File(tinyTablesFilePath);
    configuration.setTinyTablesFile(tinyTablesFile);

    // We are not testing when running from command line
    configuration.setTesting(false);

    return configuration;
}

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

@Test
public void testGetPort() throws Exception {
    //prepare/*from  ww  w.j a  v  a2 s  .com*/
    exchange.setDestinationAddress(InetSocketAddress.createUnresolved("somehost", 12345));

    //act
    int port = subject.getPort();

    //assert
    assertEquals(12345, port);
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAOTest.java

@Test
public void testSaveMessageContent() {
    MutableBufferedRequest request = new MutableBufferedRequest.Impl();
    request.setTarget(InetSocketAddress.createUnresolved("localhost", 80));
    request.setSsl(false);/*from   w ww. j a  v  a2 s .c  o  m*/
    request.setHeader(AsciiString.getBytes("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n"));
    request.setTime(1);
    MutableBufferedResponse response = new MutableBufferedResponse.Impl();
    response.setHeader(AsciiString.getBytes("HTTP/1.0 200 Ok\r\nContent-Type: text\r\n\r\n"));
    byte[] cont = AsciiString.getBytes("Some content");
    response.setContent(cont);
    response.setHeaderTime(2);
    response.setContentTime(4);

    dao.saveRequest(request);
    dao.saveResponse(response);

    int id = dao.saveConversation(request.getId(), response.getId());

    logger.fine("ADDED conversation");
    dump();
    logger.fine("##############################################");

    Conversation c = dao.getConversation(id);

    RequestHeader reqh = dao.loadRequestHeader(c.getRequestId());
    ResponseHeader resph = dao.loadResponseHeader(c.getResponseId());

    assertTrue(Arrays.equals(request.getHeader(), reqh.getHeader()));
    assertEquals(request.getTarget(), reqh.getTarget());
    assertEquals(request.isSsl(), reqh.isSsl());
    assertEquals(request.getTime(), reqh.getTime());

    assertTrue("Response headers differ", Arrays.equals(response.getHeader(), resph.getHeader()));
    assertEquals(response.getHeaderTime(), resph.getHeaderTime());
    assertEquals(response.getContentTime(), resph.getContentTime());
    byte[] content = dao.loadMessageContent(dao.getMessageContentId(c.getRequestId()));

    assertNull(content);

    content = dao.loadMessageContent(dao.getMessageContentId(c.getResponseId()));

    assertTrue(Arrays.equals(cont, content));

    assertTrue("Delete failed", dao.deleteConversation(id));

    dump();
}

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

@Test
public void testGetHostAndPort() throws Exception {
    //prepare//from  www . j  a  v a2  s . c  o m
    exchange.setRequestScheme("http");
    exchange.setDestinationAddress(InetSocketAddress.createUnresolved("somehost", 12345));

    //act
    String hostAndPort = subject.getHostAndPort();

    //assert
    assertEquals("somehost:12345", hostAndPort);
}

From source file:de.elomagic.mag.AbstractTest.java

protected void startHttpServer() throws Exception {
    putServlet = new ServletMock();

    ServletHolder defaultServ = new ServletHolder("default", putServlet);
    defaultServ.setInitParameter("resourceBase", System.getProperty("user.dir"));
    defaultServ.setInitParameter("dirAllowed", "true");

    ServletContextHandler handler = new ServletContextHandler();
    handler.addServlet(defaultServ, "/");
    // Enable BASIC authentication
    handler.setSecurityHandler(createSecurityHandler());

    webserver = new Server(InetSocketAddress.createUnresolved("127.0.0.1", 8180));
    webserver.setHandler(handler);/* w  w w  .  j a va 2s  .co m*/

    // Start Server
    webserver.start();
}

From source file:org.apache.nifi.cluster.coordination.node.LeaderElectionNodeProtocolSender.java

@Override
protected InetSocketAddress getServiceAddress() throws IOException {
    final String address = electionManager.getLeader(ClusterRoles.CLUSTER_COORDINATOR);

    if (StringUtils.isEmpty(address)) {
        throw new NoClusterCoordinatorException(
                "No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
    }// w  w  w  .  j  a v a2s  .c  o m

    final String[] splits = address.split(":");
    if (splits.length != 2) {
        final String message = String.format(
                "Attempted to determine Cluster Coordinator address. Zookeeper indicates "
                        + "that address is %s, but this is not in the expected format of <hostname>:<port>",
                address);
        logger.error(message);
        throw new ProtocolException(message);
    }

    logger.info(
            "Determined that Cluster Coordinator is located at {}; will use this address for sending heartbeat messages",
            address);

    final String hostname = splits[0];
    final int port;
    try {
        port = Integer.parseInt(splits[1]);
        if (port < 1 || port > 65535) {
            throw new NumberFormatException("Port must be in the range of 1 - 65535 but got " + port);
        }
    } catch (final NumberFormatException nfe) {
        final String message = String
                .format("Attempted to determine Cluster Coordinator address. Zookeeper indicates "
                        + "that address is %s, but the port is not a valid port number", address);
        logger.error(message);
        throw new ProtocolException(message);
    }

    final InetSocketAddress socketAddress = InetSocketAddress.createUnresolved(hostname, port);
    return socketAddress;
}