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