Search in sources :

Example 1 with RequestSpec

use of ratpack.http.client.RequestSpec in project ratpack by ratpack.

the class RequestActionSupport method addCommonResponseHandlers.

private void addCommonResponseHandlers(ChannelPipeline p, Downstream<? super T> downstream) throws Exception {
    if (channelKey.ssl && p.get(SSL_HANDLER_NAME) == null) {
        //this is added once because netty is not able to properly replace this handler on
        //pooled channels from request to request. Because a pool is unique to a uri,
        //doing this works, as subsequent requests would be passing in the same certs.
        p.addLast(SSL_HANDLER_NAME, createSslHandler());
    }
    p.addLast(CLIENT_CODEC_HANDLER_NAME, new HttpClientCodec(4096, 8192, 8192, false));
    p.addLast(READ_TIMEOUT_HANDLER_NAME, new ReadTimeoutHandler(requestConfig.readTimeout.toNanos(), TimeUnit.NANOSECONDS));
    p.addLast(REDIRECT_HANDLER_NAME, new SimpleChannelInboundHandler<HttpObject>(false) {

        boolean redirected;

        HttpResponse response;

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            forceDispose(ctx.pipeline());
            if (cause instanceof ReadTimeoutException) {
                cause = new HttpClientReadTimeoutException("Read timeout (" + requestConfig.readTimeout + ") waiting on HTTP server at " + requestConfig.uri);
            }
            error(downstream, cause);
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            Exception e = new PrematureChannelClosureException("Server " + requestConfig.uri + " closed the connection prematurely");
            error(downstream, e);
        }

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
            if (msg instanceof HttpResponse) {
                this.response = (HttpResponse) msg;
                int maxRedirects = requestConfig.maxRedirects;
                int status = response.status().code();
                String locationValue = response.headers().getAsString(HttpHeaderConstants.LOCATION);
                Action<? super RequestSpec> redirectConfigurer = RequestActionSupport.this.requestConfigurer;
                if (isRedirect(status) && redirectCount < maxRedirects && locationValue != null) {
                    final Function<? super ReceivedResponse, Action<? super RequestSpec>> onRedirect = requestConfig.onRedirect;
                    if (onRedirect != null) {
                        final Action<? super RequestSpec> onRedirectResult = onRedirect.apply(toReceivedResponse(response));
                        if (onRedirectResult == null) {
                            redirectConfigurer = null;
                        } else {
                            redirectConfigurer = redirectConfigurer.append(onRedirectResult);
                        }
                    }
                    if (redirectConfigurer != null) {
                        Action<? super RequestSpec> redirectRequestConfig = s -> {
                            if (status == 301 || status == 302) {
                                s.get();
                            }
                        };
                        redirectRequestConfig = redirectRequestConfig.append(redirectConfigurer);
                        URI locationUrl;
                        if (ABSOLUTE_PATTERN.matcher(locationValue).matches()) {
                            locationUrl = new URI(locationValue);
                        } else {
                            locationUrl = new URI(channelKey.ssl ? "https" : "http", null, channelKey.host, channelKey.port, locationValue, null, null);
                        }
                        onRedirect(locationUrl, redirectCount + 1, redirectRequestConfig).connect(downstream);
                        redirected = true;
                        dispose(ctx.pipeline(), response);
                    }
                }
            }
            if (!redirected) {
                ctx.fireChannelRead(msg);
            }
        }
    });
    if (requestConfig.decompressResponse) {
        p.addLast(DECOMPRESS_HANDLER_NAME, new HttpContentDecompressor());
    }
    addResponseHandlers(p, downstream);
}
Also used : Action(ratpack.func.Action) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) HttpClientReadTimeoutException(ratpack.http.client.HttpClientReadTimeoutException) HttpClientReadTimeoutException(ratpack.http.client.HttpClientReadTimeoutException) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) ReceivedResponse(ratpack.http.client.ReceivedResponse) URI(java.net.URI) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) HttpClientReadTimeoutException(ratpack.http.client.HttpClientReadTimeoutException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Function(ratpack.func.Function) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) RequestSpec(ratpack.http.client.RequestSpec)

Example 2 with RequestSpec

use of ratpack.http.client.RequestSpec in project ratpack by ratpack.

the class RequestConfig method of.

static RequestConfig of(URI uri, HttpClient httpClient, Action<? super RequestSpec> action) throws Exception {
    Spec spec = new Spec(uri, httpClient.getByteBufAllocator());
    spec.readTimeout = httpClient.getReadTimeout();
    spec.maxContentLength = httpClient.getMaxContentLength();
    try {
        action.execute(spec);
    } catch (Exception any) {
        if (spec.bodyByteBuf != null) {
            spec.bodyByteBuf.release();
        }
        throw any;
    }
    return new RequestConfig(spec.uri, spec.method, spec.headers, spec.bodyByteBuf, spec.maxContentLength, spec.connectTimeout, spec.readTimeout, spec.decompressResponse, spec.maxRedirects, spec.sslContext, spec.onRedirect);
}
Also used : RequestSpec(ratpack.http.client.RequestSpec)

Aggregations

RequestSpec (ratpack.http.client.RequestSpec)2 PrematureChannelClosureException (io.netty.handler.codec.PrematureChannelClosureException)1 ReadTimeoutException (io.netty.handler.timeout.ReadTimeoutException)1 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)1 URI (java.net.URI)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Action (ratpack.func.Action)1 Function (ratpack.func.Function)1 HttpClientReadTimeoutException (ratpack.http.client.HttpClientReadTimeoutException)1 ReceivedResponse (ratpack.http.client.ReceivedResponse)1