Search in sources :

Example 1 with FeedableBodyGenerator

use of com.ning.http.client.providers.grizzly.FeedableBodyGenerator in project jersey by jersey.

the class GrizzlyConnector method translate.

private com.ning.http.client.Request translate(final ClientRequest requestContext) {
    final String strMethod = requestContext.getMethod();
    final URI uri = requestContext.getUri();
    RequestBuilder builder = new RequestBuilder(strMethod).setUrl(uri.toString());
    builder.setFollowRedirects(requestContext.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
    if (requestContext.hasEntity()) {
        final RequestEntityProcessing entityProcessing = requestContext.resolveProperty(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class);
        if (entityProcessing == RequestEntityProcessing.BUFFERED) {
            byte[] entityBytes = bufferEntity(requestContext);
            builder = builder.setBody(entityBytes);
        } else {
            final FeedableBodyGenerator bodyGenerator = new FeedableBodyGenerator();
            final Integer chunkSize = requestContext.resolveProperty(ClientProperties.CHUNKED_ENCODING_SIZE, ClientProperties.DEFAULT_CHUNK_SIZE);
            bodyGenerator.setMaxPendingBytes(chunkSize);
            final FeedableBodyGenerator.Feeder feeder = new FeedableBodyGenerator.SimpleFeeder(bodyGenerator) {

                @Override
                public void flush() throws IOException {
                    requestContext.writeEntity();
                }
            };
            requestContext.setStreamProvider(new OutboundMessageContext.StreamProvider() {

                @Override
                public OutputStream getOutputStream(int contentLength) throws IOException {
                    return new FeederAdapter(feeder);
                }
            });
            bodyGenerator.setFeeder(feeder);
            builder.setBody(bodyGenerator);
        }
    }
    final GrizzlyConnectorProvider.RequestCustomizer requestCustomizer = requestContext.resolveProperty(GrizzlyConnectorProvider.REQUEST_CUSTOMIZER, GrizzlyConnectorProvider.RequestCustomizer.class);
    if (requestCustomizer != null) {
        builder = requestCustomizer.customize(requestContext, builder);
    }
    return builder.build();
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestEntityProcessing(org.glassfish.jersey.client.RequestEntityProcessing) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URI(java.net.URI) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) FeedableBodyGenerator(com.ning.http.client.providers.grizzly.FeedableBodyGenerator)

Aggregations

RequestBuilder (com.ning.http.client.RequestBuilder)1 FeedableBodyGenerator (com.ning.http.client.providers.grizzly.FeedableBodyGenerator)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 RequestEntityProcessing (org.glassfish.jersey.client.RequestEntityProcessing)1 OutboundMessageContext (org.glassfish.jersey.message.internal.OutboundMessageContext)1