Search in sources :

Example 1 with FilterContext

use of org.asynchttpclient.filter.FilterContext in project async-http-client by AsyncHttpClient.

the class PostRedirectGetTest method doTestNegative.

// --------------------------------------------------------- Private Methods
private void doTestNegative(final int status, boolean strict) throws Exception {
    ResponseFilter responseFilter = new ResponseFilter() {

        @Override
        public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
            // pass on the x-expect-get and remove the x-redirect
            // headers if found in the response
            ctx.getResponseHeaders().getHeaders().get("x-expect-post");
            ctx.getRequest().getHeaders().add("x-expect-post", "true");
            ctx.getRequest().getHeaders().remove("x-redirect");
            return ctx;
        }
    };
    try (AsyncHttpClient p = asyncHttpClient(config().setFollowRedirect(true).setStrict302Handling(strict).addResponseFilter(responseFilter))) {
        Request request = post(getTargetUrl()).addFormParam("q", "a b").addHeader("x-redirect", +status + "@" + "http://localhost:" + port1 + "/foo/bar/baz").addHeader("x-negative", "true").build();
        Future<Integer> responseFuture = p.executeRequest(request, new AsyncCompletionHandler<Integer>() {

            @Override
            public Integer onCompleted(Response response) throws Exception {
                return response.getStatusCode();
            }

            @Override
            public void onThrowable(Throwable t) {
                t.printStackTrace();
                fail("Unexpected exception: " + t.getMessage(), t);
            }
        });
        int statusCode = responseFuture.get();
        assertEquals(statusCode, 200);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FilterException(org.asynchttpclient.filter.FilterException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResponseFilter(org.asynchttpclient.filter.ResponseFilter) FilterContext(org.asynchttpclient.filter.FilterContext)

Example 2 with FilterContext

use of org.asynchttpclient.filter.FilterContext in project async-http-client by AsyncHttpClient.

the class ResponseFiltersInterceptor method exitAfterProcessingFilters.

@SuppressWarnings({ "rawtypes", "unchecked" })
public //
boolean exitAfterProcessingFilters(//
Channel channel, //
NettyResponseFuture<?> future, //
AsyncHandler<?> handler, //
HttpResponseStatus status, HttpResponseHeaders responseHeaders) {
    FilterContext fc = new FilterContext.FilterContextBuilder().asyncHandler(handler).request(future.getCurrentRequest()).responseStatus(status).responseHeaders(responseHeaders).build();
    for (ResponseFilter asyncFilter : config.getResponseFilters()) {
        try {
            fc = asyncFilter.filter(fc);
            // FIXME Is it worth protecting against this?
            assertNotNull("fc", "filterContext");
        } catch (FilterException efe) {
            requestSender.abort(channel, future, efe);
        }
    }
    // The handler may have been wrapped.
    future.setAsyncHandler(fc.getAsyncHandler());
    // The request has changed
    if (fc.replayRequest()) {
        requestSender.replayRequest(future, fc, channel);
        return true;
    }
    return false;
}
Also used : ResponseFilter(org.asynchttpclient.filter.ResponseFilter) FilterException(org.asynchttpclient.filter.FilterException) FilterContext(org.asynchttpclient.filter.FilterContext)

Example 3 with FilterContext

use of org.asynchttpclient.filter.FilterContext in project async-http-client by AsyncHttpClient.

the class PostRedirectGetTest method doTestPositive.

private void doTestPositive(final int status) throws Exception {
    ResponseFilter responseFilter = new ResponseFilter() {

        @Override
        public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
            // pass on the x-expect-get and remove the x-redirect
            // headers if found in the response
            ctx.getResponseHeaders().getHeaders().get("x-expect-get");
            ctx.getRequest().getHeaders().add("x-expect-get", "true");
            ctx.getRequest().getHeaders().remove("x-redirect");
            return ctx;
        }
    };
    try (AsyncHttpClient p = asyncHttpClient(config().setFollowRedirect(true).addResponseFilter(responseFilter))) {
        Request request = post(getTargetUrl()).addFormParam("q", "a b").addHeader("x-redirect", +status + "@" + "http://localhost:" + port1 + "/foo/bar/baz").build();
        Future<Integer> responseFuture = p.executeRequest(request, new AsyncCompletionHandler<Integer>() {

            @Override
            public Integer onCompleted(Response response) throws Exception {
                return response.getStatusCode();
            }

            @Override
            public void onThrowable(Throwable t) {
                t.printStackTrace();
                fail("Unexpected exception: " + t.getMessage(), t);
            }
        });
        int statusCode = responseFuture.get();
        assertEquals(statusCode, 200);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FilterException(org.asynchttpclient.filter.FilterException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResponseFilter(org.asynchttpclient.filter.ResponseFilter) FilterContext(org.asynchttpclient.filter.FilterContext)

Example 4 with FilterContext

use of org.asynchttpclient.filter.FilterContext in project async-http-client by AsyncHttpClient.

the class DefaultAsyncHttpClient method preProcessRequest.

/**
     * Configure and execute the associated {@link RequestFilter}. This class
     * may decorate the {@link Request} and {@link AsyncHandler}
     *
     * @param fc {@link FilterContext}
     * @return {@link FilterContext}
     */
private <T> FilterContext<T> preProcessRequest(FilterContext<T> fc) throws FilterException {
    for (RequestFilter asyncFilter : config.getRequestFilters()) {
        fc = asyncFilter.filter(fc);
        assertNotNull(fc, "filterContext");
    }
    Request request = fc.getRequest();
    if (fc.getAsyncHandler() instanceof ResumableAsyncHandler) {
        request = ResumableAsyncHandler.class.cast(fc.getAsyncHandler()).adjustRequestRange(request);
    }
    if (request.getRangeOffset() != 0) {
        RequestBuilder builder = new RequestBuilder(request);
        builder.setHeader("Range", "bytes=" + request.getRangeOffset() + "-");
        request = builder.build();
    }
    fc = new FilterContext.FilterContextBuilder<>(fc).request(request).build();
    return fc;
}
Also used : ResumableAsyncHandler(org.asynchttpclient.handler.resumable.ResumableAsyncHandler) RequestFilter(org.asynchttpclient.filter.RequestFilter) FilterContext(org.asynchttpclient.filter.FilterContext)

Aggregations

FilterContext (org.asynchttpclient.filter.FilterContext)4 FilterException (org.asynchttpclient.filter.FilterException)3 ResponseFilter (org.asynchttpclient.filter.ResponseFilter)3 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 RequestFilter (org.asynchttpclient.filter.RequestFilter)1 ResumableAsyncHandler (org.asynchttpclient.handler.resumable.ResumableAsyncHandler)1