Search in sources :

Example 1 with AbstractClassicServerExchangeHandler

use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicServerExchangeHandler in project httpcomponents-core by apache.

the class Http1IntegrationTest method testSlowResponseProducer.

@Test
public void testSlowResponseProducer() throws Exception {
    server.register("*", () -> new AbstractClassicServerExchangeHandler(2048, Executors.newSingleThreadExecutor()) {

        @Override
        protected void handle(final HttpRequest request, final InputStream requestStream, final HttpResponse response, final OutputStream responseStream, final HttpContext context) throws IOException, HttpException {
            if (!"/hello".equals(request.getPath())) {
                response.setCode(HttpStatus.SC_NOT_FOUND);
                return;
            }
            if (!Method.POST.name().equalsIgnoreCase(request.getMethod())) {
                response.setCode(HttpStatus.SC_NOT_IMPLEMENTED);
                return;
            }
            if (requestStream == null) {
                return;
            }
            final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_TYPE);
            final ContentType contentType = h1 != null ? ContentType.parse(h1.getValue()) : null;
            final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
            response.setCode(HttpStatus.SC_OK);
            response.setHeader(h1);
            try (final BufferedReader reader = new BufferedReader(new InputStreamReader(requestStream, charset));
                final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(responseStream, charset))) {
                try {
                    String l;
                    int count = 0;
                    while ((l = reader.readLine()) != null) {
                        writer.write(l);
                        writer.write("\r\n");
                        count++;
                        if (count % 500 == 0) {
                            Thread.sleep(500);
                        }
                    }
                    writer.flush();
                } catch (final InterruptedException ex) {
                    Thread.currentThread().interrupt();
                    throw new InterruptedIOException(ex.getMessage());
                }
            }
        }
    });
    final InetSocketAddress serverEndpoint = server.start();
    client.start(Http1Config.custom().setBufferSize(256).build());
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcd", 2000)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
    final Message<HttpResponse, String> result1 = future1.get(LONG_TIMEOUT.getDuration(), LONG_TIMEOUT.getTimeUnit());
    Assertions.assertNotNull(result1);
    final HttpResponse response1 = result1.getHead();
    Assertions.assertNotNull(response1);
    Assertions.assertEquals(200, response1.getCode());
    final String s1 = result1.getBody();
    Assertions.assertNotNull(s1);
    final StringTokenizer t1 = new StringTokenizer(s1, "\r\n");
    while (t1.hasMoreTokens()) {
        Assertions.assertEquals("0123456789abcd", t1.nextToken());
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ContentType(org.apache.hc.core5.http.ContentType) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BufferedWriter(java.io.BufferedWriter) HttpException(org.apache.hc.core5.http.HttpException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) Charset(java.nio.charset.Charset) AbstractClassicServerExchangeHandler(org.apache.hc.core5.http.nio.support.classic.AbstractClassicServerExchangeHandler) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) Header(org.apache.hc.core5.http.Header) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Test(org.junit.Test)

Example 2 with AbstractClassicServerExchangeHandler

use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicServerExchangeHandler in project httpcomponents-core by apache.

the class H2IntegrationTest method testSlowResponseProducer.

@Test
public void testSlowResponseProducer() throws Exception {
    server.register("*", () -> new AbstractClassicServerExchangeHandler(2048, Executors.newSingleThreadExecutor()) {

        @Override
        protected void handle(final HttpRequest request, final InputStream requestStream, final HttpResponse response, final OutputStream responseStream, final HttpContext context) throws IOException, HttpException {
            if (!"/hello".equals(request.getPath())) {
                response.setCode(HttpStatus.SC_NOT_FOUND);
                return;
            }
            if (!Method.POST.name().equalsIgnoreCase(request.getMethod())) {
                response.setCode(HttpStatus.SC_NOT_IMPLEMENTED);
                return;
            }
            if (requestStream == null) {
                return;
            }
            final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_TYPE);
            final ContentType contentType = h1 != null ? ContentType.parse(h1.getValue()) : null;
            final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
            response.setCode(HttpStatus.SC_OK);
            response.setHeader(h1);
            try (final BufferedReader reader = new BufferedReader(new InputStreamReader(requestStream, charset));
                final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(responseStream, charset))) {
                try {
                    String l;
                    int count = 0;
                    while ((l = reader.readLine()) != null) {
                        writer.write(l);
                        writer.write("\r\n");
                        count++;
                        if (count % 500 == 0) {
                            Thread.sleep(500);
                        }
                    }
                    writer.flush();
                } catch (final InterruptedException ex) {
                    Thread.currentThread().interrupt();
                    throw new InterruptedIOException(ex.getMessage());
                }
            }
        }
    });
    final InetSocketAddress serverEndpoint = server.start();
    client.start(H2Config.custom().setInitialWindowSize(512).build());
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcd", 2000)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
    final Message<HttpResponse, String> result1 = future1.get(LONG_TIMEOUT.getDuration(), LONG_TIMEOUT.getTimeUnit());
    Assertions.assertNotNull(result1);
    final HttpResponse response1 = result1.getHead();
    Assertions.assertNotNull(response1);
    Assertions.assertEquals(200, response1.getCode());
    final String s1 = result1.getBody();
    Assertions.assertNotNull(s1);
    final StringTokenizer t1 = new StringTokenizer(s1, "\r\n");
    while (t1.hasMoreTokens()) {
        Assertions.assertEquals("0123456789abcd", t1.nextToken());
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ContentType(org.apache.hc.core5.http.ContentType) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BufferedWriter(java.io.BufferedWriter) HttpException(org.apache.hc.core5.http.HttpException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) Charset(java.nio.charset.Charset) AbstractClassicServerExchangeHandler(org.apache.hc.core5.http.nio.support.classic.AbstractClassicServerExchangeHandler) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) Header(org.apache.hc.core5.http.Header) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Test(org.junit.Test)

Aggregations

BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 InterruptedIOException (java.io.InterruptedIOException)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InetSocketAddress (java.net.InetSocketAddress)2 Charset (java.nio.charset.Charset)2 StringTokenizer (java.util.StringTokenizer)2 ContentType (org.apache.hc.core5.http.ContentType)2 Header (org.apache.hc.core5.http.Header)2 HttpException (org.apache.hc.core5.http.HttpException)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 Message (org.apache.hc.core5.http.Message)2 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)2 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)2 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)2