Search in sources :

Example 1 with RequestEntityProcessing

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

Example 2 with RequestEntityProcessing

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;
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) RequestEntityProcessing(org.glassfish.jersey.client.RequestEntityProcessing) URISyntaxException(java.net.URISyntaxException) Logger(java.util.logging.Logger) URI(java.net.URI) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) HttpURLConnection(java.net.HttpURLConnection) ProcessingException(javax.ws.rs.ProcessingException)

Example 3 with RequestEntityProcessing

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;
}
Also used : RequestEntityProcessing(org.glassfish.jersey.client.RequestEntityProcessing)

Aggregations

RequestEntityProcessing (org.glassfish.jersey.client.RequestEntityProcessing)3 URI (java.net.URI)2 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 HttpURLConnection (java.net.HttpURLConnection)1 URISyntaxException (java.net.URISyntaxException)1 Logger (java.util.logging.Logger)1 ProcessingException (javax.ws.rs.ProcessingException)1 Response (javax.ws.rs.core.Response)1 ClientResponse (org.glassfish.jersey.client.ClientResponse)1 OutboundMessageContext (org.glassfish.jersey.message.internal.OutboundMessageContext)1