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();
}
Aggregations