Search in sources :

Example 1 with HttpClientConfig

use of io.airlift.http.client.HttpClientConfig in project presto by prestodb.

the class QueryRunner method getHttpClientConfig.

private static HttpClientConfig getHttpClientConfig(Optional<HostAndPort> socksProxy, Optional<String> keystorePath, Optional<String> keystorePassword, Optional<String> truststorePath, Optional<String> truststorePassword, Optional<String> kerberosPrincipal, Optional<String> kerberosRemoteServiceName, boolean authenticationEnabled) {
    HttpClientConfig httpClientConfig = new HttpClientConfig().setConnectTimeout(new Duration(5, TimeUnit.SECONDS)).setRequestTimeout(new Duration(5, TimeUnit.SECONDS));
    socksProxy.ifPresent(httpClientConfig::setSocksProxy);
    httpClientConfig.setAuthenticationEnabled(authenticationEnabled);
    keystorePath.ifPresent(httpClientConfig::setKeyStorePath);
    keystorePassword.ifPresent(httpClientConfig::setKeyStorePassword);
    truststorePath.ifPresent(httpClientConfig::setTrustStorePath);
    truststorePassword.ifPresent(httpClientConfig::setTrustStorePassword);
    kerberosPrincipal.ifPresent(httpClientConfig::setKerberosPrincipal);
    kerberosRemoteServiceName.ifPresent(httpClientConfig::setKerberosRemoteServiceName);
    return httpClientConfig;
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) Duration(io.airlift.units.Duration)

Example 2 with HttpClientConfig

use of io.airlift.http.client.HttpClientConfig in project airlift by airlift.

the class TestHttpServerProvider method testClientCertificate.

@Test
public void testClientCertificate() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
            if ((certs == null) || (certs.length == 0)) {
                throw new RuntimeException("No client certificate");
            }
            if (certs.length > 1) {
                throw new RuntimeException("Received multiple client certificates");
            }
            X509Certificate cert = certs[0];
            response.getWriter().write(cert.getSubjectX500Principal().getName());
            response.setStatus(HttpServletResponse.SC_OK);
        }
    };
    config.setHttpEnabled(false).setAdminEnabled(false).setHttpsEnabled(true).setHttpsPort(0).setKeystorePath(getResource("clientcert/server.keystore").toString()).setKeystorePassword("airlift");
    createAndStartServer(servlet);
    HttpClientConfig clientConfig = new HttpClientConfig().setKeyStorePath(getResource("clientcert/client.keystore").toString()).setKeyStorePassword("airlift").setTrustStorePath(getResource("clientcert/client.truststore").toString()).setTrustStorePassword("airlift");
    System.out.println(httpServerInfo.getHttpsUri());
    try (JettyHttpClient httpClient = new JettyHttpClient(clientConfig)) {
        StringResponse response = httpClient.execute(prepareGet().setUri(httpServerInfo.getHttpsUri()).build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(response.getBody(), "CN=testing,OU=Client,O=Airlift,L=Palo Alto,ST=CA,C=US");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpClientConfig(io.airlift.http.client.HttpClientConfig) HttpServlet(javax.servlet.http.HttpServlet) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) X509Certificate(java.security.cert.X509Certificate) Test(org.testng.annotations.Test)

Example 3 with HttpClientConfig

use of io.airlift.http.client.HttpClientConfig in project airlift by airlift.

the class TestTestingHttpServer method testFilteredRequest.

@Test
public void testFilteredRequest() throws Exception {
    DummyServlet servlet = new DummyServlet();
    DummyFilter filter = new DummyFilter();
    TestingHttpServer server = createTestingHttpServerWithFilter(servlet, ImmutableMap.of(), filter);
    try {
        server.start();
        try (HttpClient client = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(1, SECONDS)))) {
            StatusResponse response = client.execute(prepareGet().setUri(server.getBaseUrl()).build(), createStatusResponseHandler());
            assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
            assertEquals(servlet.getCallCount(), 1);
            assertEquals(filter.getCallCount(), 1);
        }
    } finally {
        server.stop();
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Duration(io.airlift.units.Duration) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Test(org.testng.annotations.Test)

Example 4 with HttpClientConfig

use of io.airlift.http.client.HttpClientConfig in project airlift by airlift.

the class TestTestingHttpServer method testRequest.

@Test
public void testRequest() throws Exception {
    DummyServlet servlet = new DummyServlet();
    TestingHttpServer server = createTestingHttpServer(servlet, ImmutableMap.of());
    try {
        server.start();
        try (HttpClient client = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(1, SECONDS)))) {
            StatusResponse response = client.execute(prepareGet().setUri(server.getBaseUrl()).build(), createStatusResponseHandler());
            assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
            assertEquals(servlet.getCallCount(), 1);
        }
    } finally {
        server.stop();
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Duration(io.airlift.units.Duration) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Test(org.testng.annotations.Test)

Example 5 with HttpClientConfig

use of io.airlift.http.client.HttpClientConfig in project airlift by airlift.

the class TestHttpEventClient method setup.

@BeforeMethod
public void setup() throws Exception {
    httpClient = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(10, SECONDS)));
    servlet = new DummyServlet();
    server = createServer(servlet);
    server.start();
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Duration(io.airlift.units.Duration) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

HttpClientConfig (io.airlift.http.client.HttpClientConfig)10 JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)9 Test (org.testng.annotations.Test)8 Duration (io.airlift.units.Duration)7 StatusResponse (io.airlift.http.client.StatusResponseHandler.StatusResponse)6 HttpClient (io.airlift.http.client.HttpClient)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 Injector (com.google.inject.Injector)3 Bootstrap (io.airlift.bootstrap.Bootstrap)3 LifeCycleManager (io.airlift.bootstrap.LifeCycleManager)3 TheServlet (io.airlift.http.server.TheServlet)3 TestingNodeModule (io.airlift.node.testing.TestingNodeModule)3 Map (java.util.Map)3 StringResponse (io.airlift.http.client.StringResponseHandler.StringResponse)1 URI (java.net.URI)1 X509Certificate (java.security.cert.X509Certificate)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BeforeMethod (org.testng.annotations.BeforeMethod)1