Search in sources :

Example 11 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project kylo by Teradata.

the class CustomApacheConnector method getHttpEntity.

private HttpEntity getHttpEntity(final ClientRequest clientRequest, final boolean bufferingEnabled) {
    final Object entity = clientRequest.getEntity();
    if (entity == null) {
        return null;
    }
    final AbstractHttpEntity httpEntity = new AbstractHttpEntity() {

        @Override
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public long getContentLength() {
            return -1;
        }

        @Override
        public InputStream getContent() throws IOException, IllegalStateException {
            if (bufferingEnabled) {
                final ByteArrayOutputStream buffer = new ByteArrayOutputStream(512);
                writeTo(buffer);
                return new ByteArrayInputStream(buffer.toByteArray());
            } else {
                return null;
            }
        }

        @Override
        public void writeTo(final OutputStream outputStream) throws IOException {
            clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {

                @Override
                public OutputStream getOutputStream(final int contentLength) throws IOException {
                    return outputStream;
                }
            });
            clientRequest.writeEntity();
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };
    if (bufferingEnabled) {
        try {
            return new BufferedHttpEntity(httpEntity);
        } catch (final IOException e) {
            throw new ProcessingException(LocalizationMessages.ERROR_BUFFERING_ENTITY(), e);
        }
    } else {
        return httpEntity;
    }
}
Also used : BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ChunkedOutputStream(org.apache.http.impl.io.ChunkedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) ProcessingException(javax.ws.rs.ProcessingException)

Example 12 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project platformlayer by platformlayer.

the class ApacheCommonsHttpRequest method setRequestContent.

@Override
public void setRequestContent(final ByteSource data) throws IOException {
    HttpEntityEnclosingRequestBase post = (HttpEntityEnclosingRequestBase) request;
    post.setEntity(new AbstractHttpEntity() {

        @Override
        public boolean isRepeatable() {
            return true;
        }

        @Override
        public long getContentLength() {
            try {
                return data.getContentLength();
            } catch (IOException e) {
                throw new IllegalStateException("Error getting content length", e);
            }
        }

        @Override
        public InputStream getContent() throws IOException, IllegalStateException {
            return data.open();
        }

        @Override
        public void writeTo(OutputStream os) throws IOException {
            InputStream is = data.open();
            try {
                IoUtils.copyToOutputStream(is, os);
            } finally {
                is.close();
            }
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    });
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Example 13 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project undertow by undertow-io.

the class ReadTimeoutTestCase method testReadTimeout.

@Test
public void testReadTimeout() throws InterruptedException, IOException {
    final CountDownLatch errorLatch = new CountDownLatch(1);
    DefaultServer.setRootHandler((final HttpServerExchange exchange) -> {
        final StreamSinkChannel response = exchange.getResponseChannel();
        final StreamSourceChannel request = exchange.getRequestChannel();
        request.getReadSetter().set(ChannelListeners.drainListener(Long.MAX_VALUE, (final Channel channel) -> {
            new StringWriteChannelListener("COMPLETED") {

                @Override
                protected void writeDone(final StreamSinkChannel channel) {
                    exchange.endExchange();
                }
            }.setup(response);
        }, (final StreamSourceChannel channel, final IOException e) -> {
            e.printStackTrace();
            exchange.endExchange();
            exception = e;
            errorLatch.countDown();
        }));
        request.wakeupReads();
    });
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL());
        post.setEntity(new AbstractHttpEntity() {

            @Override
            public InputStream getContent() throws IllegalStateException {
                return null;
            }

            @Override
            public void writeTo(final OutputStream outstream) throws IOException {
                for (int i = 0; i < 5; ++i) {
                    outstream.write('*');
                    outstream.flush();
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            @Override
            public boolean isStreaming() {
                return true;
            }

            @Override
            public boolean isRepeatable() {
                return false;
            }

            @Override
            public long getContentLength() {
                return 5;
            }
        });
        post.addHeader(Headers.CONNECTION_STRING, "close");
        boolean socketFailure = false;
        try {
            client.execute(post);
        } catch (SocketException e) {
            Assert.assertTrue(e.getMessage(), e.getMessage().contains("Broken pipe") || e.getMessage().contains("connection abort"));
            socketFailure = true;
        }
        Assert.assertTrue("Test sent request without any exception", socketFailure);
        if (errorLatch.await(5, TimeUnit.SECONDS)) {
            Assert.assertTrue(getExceptionDescription(exception), exception instanceof ReadTimeoutException || (DefaultServer.isProxy() && exception instanceof IOException));
            if (exception.getSuppressed() != null && exception.getSuppressed().length > 0) {
                for (Throwable supressed : exception.getSuppressed()) {
                    Assert.assertEquals(getExceptionDescription(supressed), ReadTimeoutException.class, exception.getClass());
                }
            }
        } else if (!DefaultServer.isProxy()) {
            // ignore if proxy, because when we're on proxy, we might not be able to see the exception
            Assert.fail("Did not get ReadTimeoutException");
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) HttpPost(org.apache.http.client.methods.HttpPost) SocketException(java.net.SocketException) InputStream(java.io.InputStream) ReadTimeoutException(org.xnio.channels.ReadTimeoutException) StreamSourceChannel(org.xnio.channels.StreamSourceChannel) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) OutputStream(java.io.OutputStream) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TestHttpClient(io.undertow.testutils.TestHttpClient) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) Test(org.junit.Test)

Example 14 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project undertow by undertow-io.

the class BlockingReadTimeoutHandlerTestCase method testReadTimeout.

@Test
public void testReadTimeout() throws InterruptedException {
    DefaultServer.setRootHandler(BlockingReadTimeoutHandler.builder().nextHandler(new BlockingHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            try {
                IOUtils.copyLarge(exchange.getInputStream(), STUB_OUTPUT_STREAM);
                exchange.getOutputStream().write("COMPLETED".getBytes(StandardCharsets.UTF_8));
            } catch (IOException e) {
                exception = e;
                errorLatch.countDown();
            }
        }
    })).readTimeout(Duration.ofMillis(1)).build());
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL());
        post.setEntity(new AbstractHttpEntity() {

            @Override
            public InputStream getContent() throws IOException, IllegalStateException {
                return null;
            }

            @Override
            public void writeTo(final OutputStream outstream) throws IOException {
                for (int i = 0; i < 5; ++i) {
                    outstream.write('*');
                    outstream.flush();
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            @Override
            public boolean isStreaming() {
                return true;
            }

            @Override
            public boolean isRepeatable() {
                return false;
            }

            @Override
            public long getContentLength() {
                return 5;
            }
        });
        post.addHeader(Headers.CONNECTION_STRING, "close");
        try {
            client.execute(post);
        } catch (IOException e) {
        }
        if (errorLatch.await(5, TimeUnit.SECONDS)) {
            Assert.assertEquals(ReadTimeoutException.class, exception.getClass());
        } else {
            Assert.fail("Read did not time out");
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) HttpPost(org.apache.http.client.methods.HttpPost) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) IOException(java.io.IOException) ReadTimeoutException(org.xnio.channels.ReadTimeoutException) TestHttpClient(io.undertow.testutils.TestHttpClient) HttpServerExchange(io.undertow.server.HttpServerExchange) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) Test(org.junit.Test)

Example 15 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project platform_external_apache-http by android.

the class AndroidHttpClient method getCompressedEntity.

/**
 * Compress data to send to server.
 * Creates a Http Entity holding the gzipped data.
 * The data will not be compressed if it is too short.
 * @param data The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Aggregations

AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)16 OutputStream (java.io.OutputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GZIPOutputStream (java.util.zip.GZIPOutputStream)4 HttpPost (org.apache.http.client.methods.HttpPost)4 BasicHeader (org.apache.http.message.BasicHeader)3 TestHttpClient (io.undertow.testutils.TestHttpClient)2 ProcessingException (javax.ws.rs.ProcessingException)2 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)2 InputStreamEntity (org.apache.http.entity.InputStreamEntity)2 ChunkedOutputStream (org.apache.http.impl.io.ChunkedOutputStream)2 OutboundMessageContext (org.glassfish.jersey.message.internal.OutboundMessageContext)2 Test (org.junit.Test)2 ReadTimeoutException (org.xnio.channels.ReadTimeoutException)2 HttpContent (com.google.api.client.http.HttpContent)1 StreamingContent (com.google.api.client.util.StreamingContent)1