use of org.glassfish.jersey.client.RequestEntityProcessing 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();
}
use of org.glassfish.jersey.client.RequestEntityProcessing in project jersey by jersey.
the class HttpUrlConnector method _apply.
private ClientResponse _apply(final ClientRequest request) throws IOException {
final HttpURLConnection uc;
uc = this.connectionFactory.getConnection(request.getUri().toURL());
uc.setDoInput(true);
final String httpMethod = request.getMethod();
if (request.resolveProperty(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, setMethodWorkaround)) {
setRequestMethodViaJreBugWorkaround(uc, httpMethod);
} else {
uc.setRequestMethod(httpMethod);
}
uc.setInstanceFollowRedirects(request.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
uc.setConnectTimeout(request.resolveProperty(ClientProperties.CONNECT_TIMEOUT, uc.getConnectTimeout()));
uc.setReadTimeout(request.resolveProperty(ClientProperties.READ_TIMEOUT, uc.getReadTimeout()));
secureConnection(request.getClient(), uc);
final Object entity = request.getEntity();
if (entity != null) {
RequestEntityProcessing entityProcessing = request.resolveProperty(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class);
if (entityProcessing == null || entityProcessing != RequestEntityProcessing.BUFFERED) {
final long length = request.getLengthLong();
if (fixLengthStreaming && length > 0) {
// uc.setFixedLengthStreamingMode(long) was introduced in JDK 1.7 and Jersey client supports 1.6+
if ("1.6".equals(Runtime.class.getPackage().getSpecificationVersion())) {
uc.setFixedLengthStreamingMode(request.getLength());
} else {
uc.setFixedLengthStreamingMode(length);
}
} else if (entityProcessing == RequestEntityProcessing.CHUNKED) {
uc.setChunkedStreamingMode(chunkSize);
}
}
uc.setDoOutput(true);
if ("GET".equalsIgnoreCase(httpMethod)) {
final Logger logger = Logger.getLogger(HttpUrlConnector.class.getName());
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO, LocalizationMessages.HTTPURLCONNECTION_REPLACES_GET_WITH_ENTITY());
}
}
request.setStreamProvider(contentLength -> {
setOutboundHeaders(request.getStringHeaders(), uc);
return uc.getOutputStream();
});
request.writeEntity();
} else {
setOutboundHeaders(request.getStringHeaders(), uc);
}
final int code = uc.getResponseCode();
final String reasonPhrase = uc.getResponseMessage();
final Response.StatusType status = reasonPhrase == null ? Statuses.from(code) : Statuses.from(code, reasonPhrase);
final URI resolvedRequestUri;
try {
resolvedRequestUri = uc.getURL().toURI();
} catch (URISyntaxException e) {
throw new ProcessingException(e);
}
ClientResponse responseContext = new ClientResponse(status, request, resolvedRequestUri);
responseContext.headers(uc.getHeaderFields().entrySet().stream().filter(stringListEntry -> stringListEntry.getKey() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
responseContext.setEntityStream(getInputStream(uc));
return responseContext;
}
use of org.glassfish.jersey.client.RequestEntityProcessing in project jersey by jersey.
the class JdkConnector method createHttpRequest.
private HttpRequest createHttpRequest(ClientRequest request) {
Object entity = request.getEntity();
if (entity == null) {
return HttpRequest.createBodyless(request.getMethod(), request.getUri());
}
RequestEntityProcessing entityProcessing = request.resolveProperty(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class);
HttpRequest httpRequest;
if (entityProcessing != null && entityProcessing == RequestEntityProcessing.CHUNKED) {
httpRequest = HttpRequest.createChunked(request.getMethod(), request.getUri(), connectorConfiguration.getChunkSize());
} else {
httpRequest = HttpRequest.createBuffered(request.getMethod(), request.getUri());
}
return httpRequest;
}
Aggregations