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