Search in sources :

Example 11 with NettyHttpService

use of io.cdap.http.NettyHttpService in project cdap by caskdata.

the class HttpHandlerGeneratorTest method testContentConsumer.

@Test
public void testContentConsumer() throws Exception {
    HttpHandlerFactory factory = new HttpHandlerFactory("/content", TransactionControl.IMPLICIT);
    // Create the file upload handler and starts a netty server with it
    final File outputDir = TEMP_FOLDER.newFolder();
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {

        @Override
        protected FileHandler createHandler() {
            return new FileHandler(outputDir);
        }
    }, new NoopMetricsContext());
    // Creates a Netty http server with 1K request buffer
    NettyHttpService service = NettyHttpService.builder("test-content-consumer").setHttpHandlers(httpHandler).setHttpChunkLimit(1024).build();
    service.start();
    try {
        InetSocketAddress bindAddress = service.getBindAddress();
        testUpload(outputDir, bindAddress, "");
        testUpload(outputDir, bindAddress, "-no-tx");
    } finally {
        service.stop();
    }
}
Also used : HttpHandler(io.cdap.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) NettyHttpService(io.cdap.http.NettyHttpService) NoopMetricsContext(io.cdap.cdap.api.metrics.NoopMetricsContext) File(java.io.File) Test(org.junit.Test)

Example 12 with NettyHttpService

use of io.cdap.http.NettyHttpService in project cdap by caskdata.

the class HttpHandlerGeneratorTest method testContentProducer.

@Test
public void testContentProducer() throws Exception {
    HttpHandlerFactory factory = new HttpHandlerFactory("/content", TransactionControl.IMPLICIT);
    // Create the file upload handler and starts a netty server with it
    final File outputDir = TEMP_FOLDER.newFolder();
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {

        @Override
        protected FileHandler createHandler() {
            return new FileHandler(outputDir);
        }
    }, new NoopMetricsContext());
    NettyHttpService service = NettyHttpService.builder("test-content-producer").setHttpHandlers(httpHandler).build();
    service.start();
    try {
        // Generate a 100K file
        File file = TEMP_FOLDER.newFile();
        Files.write(Strings.repeat("0123456789", 10240).getBytes(Charsets.UTF_8), file);
        InetSocketAddress bindAddress = service.getBindAddress();
        // Upload the generated file
        URL uploadURL = new URL(String.format("http://%s:%d/content/upload/test.txt", bindAddress.getHostName(), bindAddress.getPort()));
        HttpURLConnection urlConn = (HttpURLConnection) uploadURL.openConnection();
        try {
            urlConn.setDoOutput(true);
            urlConn.setRequestMethod("PUT");
            Files.copy(file, urlConn.getOutputStream());
            Assert.assertEquals(200, urlConn.getResponseCode());
        } finally {
            urlConn.disconnect();
        }
        // Download the file
        File downloadFile = TEMP_FOLDER.newFile();
        urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        try {
            ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
        } finally {
            urlConn.disconnect();
        }
        // Compare if the file content are the same
        Assert.assertTrue(Files.equal(file, downloadFile));
        // Download a file that doesn't exist
        urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test2.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        try {
            Assert.assertEquals(500, urlConn.getResponseCode());
        } finally {
            urlConn.disconnect();
        }
        // Upload the file to the POST endpoint. The endpoint should response with the same file content
        downloadFile = TEMP_FOLDER.newFile();
        urlConn = (HttpURLConnection) uploadURL.openConnection();
        try {
            urlConn.setDoOutput(true);
            urlConn.setRequestMethod("POST");
            Files.copy(file, urlConn.getOutputStream());
            ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
            Assert.assertEquals(200, urlConn.getResponseCode());
            Assert.assertTrue(Files.equal(file, downloadFile));
        } finally {
            urlConn.disconnect();
        }
    } finally {
        service.stop();
    }
}
Also used : HttpHandler(io.cdap.http.HttpHandler) HttpURLConnection(java.net.HttpURLConnection) InetSocketAddress(java.net.InetSocketAddress) NettyHttpService(io.cdap.http.NettyHttpService) NoopMetricsContext(io.cdap.cdap.api.metrics.NoopMetricsContext) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Aggregations

NettyHttpService (io.cdap.http.NettyHttpService)12 InetSocketAddress (java.net.InetSocketAddress)9 URL (java.net.URL)8 Test (org.junit.Test)8 NoopMetricsContext (io.cdap.cdap.api.metrics.NoopMetricsContext)4 HttpHandler (io.cdap.http.HttpHandler)4 File (java.io.File)3 HttpURLConnection (java.net.HttpURLConnection)3 KeyStore (java.security.KeyStore)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)2 HttpResponse (io.cdap.common.http.HttpResponse)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ImmutableList (com.google.common.collect.ImmutableList)1 AccessException (io.cdap.cdap.api.security.AccessException)1 AuthorizationClient (io.cdap.cdap.client.AuthorizationClient)1 FeatureDisabledException (io.cdap.cdap.common.FeatureDisabledException)1 HttpExceptionHandler (io.cdap.cdap.common.HttpExceptionHandler)1 SConfiguration (io.cdap.cdap.common.conf.SConfiguration)1