Search in sources :

Example 1 with ResponseFilter

use of org.asynchttpclient.filter.ResponseFilter 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 ResponseFilter

use of org.asynchttpclient.filter.ResponseFilter 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 ResponseFilter

use of org.asynchttpclient.filter.ResponseFilter 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)

Aggregations

FilterContext (org.asynchttpclient.filter.FilterContext)3 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