Search in sources :

Example 21 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class FriendlyServersTest method testHttpBin.

@Test
@Ignore
public void testHttpBin() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        {
            final HttpResponseStatus status = client.go(new Request(HttpMethod.GET, new URL("https://httpbin.org/get")), StatusResponseHandler.getInstance()).get().getStatus();
            Assert.assertEquals(200, status.getCode());
        }
        {
            final HttpResponseStatus status = client.go(new Request(HttpMethod.POST, new URL("https://httpbin.org/post")).setContent(new byte[] { 'a', 'b', 'c', 1, 2, 3 }), StatusResponseHandler.getInstance()).get().getStatus();
            Assert.assertEquals(200, status.getCode());
        }
    } finally {
        lifecycle.stop();
    }
}
Also used : HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class FriendlyServersTest method testCompressionCodecConfig.

@Test
public void testCompressionCodecConfig() throws Exception {
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    final ServerSocket serverSocket = new ServerSocket(0);
    final AtomicBoolean foundAcceptEncoding = new AtomicBoolean();
    exec.submit(new Runnable() {

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try (Socket clientSocket = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
                    OutputStream out = clientSocket.getOutputStream()) {
                    // Read headers
                    String header;
                    while (!(header = in.readLine()).equals("")) {
                        if ("Accept-Encoding: identity".equals(header)) {
                            foundAcceptEncoding.set(true);
                        }
                    }
                    out.write("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nhello!".getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                // Suppress
                }
            }
        }
    });
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withCompressionCodec(HttpClientConfig.CompressionCodec.IDENTITY).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final StatusResponseHolder response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", serverSocket.getLocalPort()))), StatusResponseHandler.getInstance()).get();
        Assert.assertEquals(200, response.getStatus().getCode());
        Assert.assertEquals("hello!", response.getContent());
        Assert.assertTrue(foundAcceptEncoding.get());
    } finally {
        exec.shutdownNow();
        serverSocket.close();
        lifecycle.stop();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) ServerSocket(java.net.ServerSocket) ChannelException(org.jboss.netty.channel.ChannelException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 23 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class FriendlyServersTest method testFriendlySelfSignedHttpsServer.

@Test
public void testFriendlySelfSignedHttpsServer() throws Exception {
    final Lifecycle lifecycle = new Lifecycle();
    final String keyStorePath = getClass().getClassLoader().getResource("keystore.jks").getFile();
    Server server = new Server();
    HttpConfiguration https = new HttpConfiguration();
    https.addCustomizer(new SecureRequestCustomizer());
    SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
    sslContextFactory.setKeyStorePath(keyStorePath);
    sslContextFactory.setKeyStorePassword("abc123");
    sslContextFactory.setKeyManagerPassword("abc123");
    ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https));
    sslConnector.setPort(0);
    server.setConnectors(new Connector[] { sslConnector });
    server.start();
    try {
        final SSLContext mySsl = HttpClientInit.sslContextWithTrustedKeyStore(keyStorePath, "abc123");
        final HttpClientConfig trustingConfig = HttpClientConfig.builder().withSslContext(mySsl).build();
        final HttpClient trustingClient = HttpClientInit.createClient(trustingConfig, lifecycle);
        final HttpClientConfig skepticalConfig = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
        final HttpClient skepticalClient = HttpClientInit.createClient(skepticalConfig, lifecycle);
        // Correct name ("localhost")
        {
            final HttpResponseStatus status = trustingClient.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", sslConnector.getLocalPort()))), StatusResponseHandler.getInstance()).get().getStatus();
            Assert.assertEquals(404, status.getCode());
        }
        // Incorrect name ("127.0.0.1")
        {
            final ListenableFuture<StatusResponseHolder> response1 = trustingClient.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://127.0.0.1:%d/", sslConnector.getLocalPort()))), StatusResponseHandler.getInstance());
            Throwable ea = null;
            try {
                response1.get();
            } catch (ExecutionException e) {
                ea = e.getCause();
            }
            Assert.assertTrue("ChannelException thrown by 'get'", ea instanceof ChannelException);
            Assert.assertTrue("Expected error message", ea.getCause().getMessage().contains("Failed to handshake"));
        }
        {
            // Untrusting client
            final ListenableFuture<StatusResponseHolder> response2 = skepticalClient.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", sslConnector.getLocalPort()))), StatusResponseHandler.getInstance());
            Throwable eb = null;
            try {
                response2.get();
            } catch (ExecutionException e) {
                eb = e.getCause();
            }
            Assert.assertNotNull("ChannelException thrown by 'get'", eb);
            Assert.assertTrue("Root cause is SSLHandshakeException", eb.getCause().getCause() instanceof SSLHandshakeException);
        }
    } finally {
        lifecycle.stop();
        server.stop();
    }
}
Also used : SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SSLContext(javax.net.ssl.SSLContext) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URL(java.net.URL) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) ChannelException(org.jboss.netty.channel.ChannelException) Test(org.junit.Test)

Example 24 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class EmitterTest method sizeBasedEmitterGeneralizedCreation.

private HttpPostEmitter sizeBasedEmitterGeneralizedCreation(int size) {
    Properties props = new Properties();
    props.setProperty("org.apache.druid.java.util.emitter.type", "http");
    props.setProperty("org.apache.druid.java.util.emitter.recipientBaseUrl", TARGET_URL);
    props.setProperty("org.apache.druid.java.util.emitter.flushMillis", String.valueOf(Long.MAX_VALUE));
    props.setProperty("org.apache.druid.java.util.emitter.flushCount", String.valueOf(size));
    props.setProperty("org.apache.druid.java.util.emitter.flushTimeOut", String.valueOf(BaseHttpEmittingConfig.TEST_FLUSH_TIMEOUT_MILLIS));
    Lifecycle lifecycle = new Lifecycle();
    Emitter emitter = Emitters.create(props, httpClient, JSON_MAPPER, lifecycle);
    Assert.assertTrue(StringUtils.format("HttpPostEmitter emitter should be created, but found %s", emitter.getClass().getName()), emitter instanceof HttpPostEmitter);
    emitter.start();
    return (HttpPostEmitter) emitter;
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) Properties(java.util.Properties)

Example 25 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JankyServersTest method testHttpSilentServerWithGlobalTimeout.

@Test
public void testHttpSilentServerWithGlobalTimeout() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withReadTimeout(new Duration(100)).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final ListenableFuture<StatusResponseHolder> future = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", silentServerSocket.getLocalPort()))), StatusResponseHandler.getInstance());
        Throwable e = null;
        try {
            future.get();
        } catch (ExecutionException e1) {
            e = e1.getCause();
        }
        Assert.assertTrue("ReadTimeoutException thrown by 'get'", e instanceof ReadTimeoutException);
    } finally {
        lifecycle.stop();
    }
}
Also used : ReadTimeoutException(org.jboss.netty.handler.timeout.ReadTimeoutException) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Duration(org.joda.time.Duration) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Lifecycle (org.apache.druid.java.util.common.lifecycle.Lifecycle)46 Test (org.junit.Test)21 Injector (com.google.inject.Injector)12 URL (java.net.URL)12 ExecutionException (java.util.concurrent.ExecutionException)12 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)10 Before (org.junit.Before)10 ExecutorService (java.util.concurrent.ExecutorService)6 Binder (com.google.inject.Binder)5 Module (com.google.inject.Module)5 OutputStream (java.io.OutputStream)5 Properties (java.util.Properties)5 ManageLifecycle (org.apache.druid.guice.ManageLifecycle)5 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)5 ChannelException (org.jboss.netty.channel.ChannelException)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 IOException (java.io.IOException)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Provides (com.google.inject.Provides)3