Search in sources :

Example 1 with FilterException

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

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

the class RateLimitedThrottleRequestFilter method filter.

/**
     * {@inheritDoc}
     */
@Override
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Current Throttling Status {}", available.availablePermits());
        }
        long startOfWait = System.currentTimeMillis();
        attemptConcurrencyPermitAcquistion(ctx);
        attemptRateLimitedPermitAcquistion(ctx, startOfWait);
    } catch (InterruptedException e) {
        throw new FilterException(String.format("Interrupted Request %s with AsyncHandler %s", ctx.getRequest(), ctx.getAsyncHandler()));
    }
    return new FilterContext.FilterContextBuilder<>(ctx).asyncHandler(ReleasePermitOnComplete.wrap(ctx.getAsyncHandler(), available)).build();
}
Also used : FilterException(org.asynchttpclient.filter.FilterException)

Example 3 with FilterException

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

the class NettyRequestSender method applyIoExceptionFiltersAndReplayRequest.

public boolean applyIoExceptionFiltersAndReplayRequest(NettyResponseFuture<?> future, IOException e, Channel channel) {
    boolean replayed = false;
    @SuppressWarnings({ "unchecked", "rawtypes" }) FilterContext<?> fc = new FilterContext.FilterContextBuilder().asyncHandler(future.getAsyncHandler()).request(future.getCurrentRequest()).ioException(e).build();
    for (IOExceptionFilter asyncFilter : config.getIoExceptionFilters()) {
        try {
            fc = asyncFilter.filter(fc);
            assertNotNull(fc, "filterContext");
        } catch (FilterException efe) {
            abort(channel, future, efe);
        }
    }
    if (fc.replayRequest() && future.incrementRetryAndCheck() && future.isReplayPossible()) {
        replayRequest(future, fc, channel);
        replayed = true;
    }
    return replayed;
}
Also used : IOExceptionFilter(org.asynchttpclient.filter.IOExceptionFilter) FilterException(org.asynchttpclient.filter.FilterException)

Example 4 with FilterException

use of org.asynchttpclient.filter.FilterException 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 5 with FilterException

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

FilterException (org.asynchttpclient.filter.FilterException)5 FilterContext (org.asynchttpclient.filter.FilterContext)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 IOExceptionFilter (org.asynchttpclient.filter.IOExceptionFilter)1