use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.
the class FilterTestCase method requireThatRequestFilterCanTerminateChain.
@Test
public void requireThatRequestFilterCanTerminateChain() throws Exception {
final RequestFilter requestFilter1 = new RespondForbiddenFilter();
final RequestFilter requestFilter2 = mock(RequestFilter.class);
final RequestFilter requestFilterChain = RequestFilterChain.newInstance(requestFilter1, requestFilter2);
final HttpRequest request = null;
final ResponseHandler responseHandler = mock(ResponseHandler.class);
when(responseHandler.handleResponse(any(Response.class))).thenReturn(mock(ContentChannel.class));
requestFilterChain.filter(request, responseHandler);
verify(requestFilter2, never()).filter(any(HttpRequest.class), any(ResponseHandler.class));
final ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
verify(responseHandler).handleResponse(responseCaptor.capture());
assertThat(responseCaptor.getValue().getStatus(), is(Response.Status.FORBIDDEN));
}
use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.
the class FilterTestCase method requireThatRequestFilterChainCallsFilterWithOriginalResponseHandler.
@Test
public void requireThatRequestFilterChainCallsFilterWithOriginalResponseHandler() throws Exception {
final RequestFilter requestFilter = mock(RequestFilter.class);
final RequestFilter requestFilterChain = RequestFilterChain.newInstance(requestFilter);
final HttpRequest request = null;
final ResponseHandler responseHandler = mock(ResponseHandler.class);
requestFilterChain.filter(request, responseHandler);
// Check that the filter is called with the same response handler argument as the chain was,
// in a manner that allows the handler object to be wrapped.
final ArgumentCaptor<ResponseHandler> responseHandlerCaptor = ArgumentCaptor.forClass(ResponseHandler.class);
verify(requestFilter).filter(any(HttpRequest.class), responseHandlerCaptor.capture());
verify(responseHandler, never()).handleResponse(any(Response.class));
responseHandlerCaptor.getValue().handleResponse(mock(Response.class));
verify(responseHandler, times(1)).handleResponse(any(Response.class));
}
use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.
the class FilterTestCase method requireThatRequestFilterChainCallsFilterWithOriginalRequest.
@Test
public void requireThatRequestFilterChainCallsFilterWithOriginalRequest() throws Exception {
final RequestFilter requestFilter = mock(RequestFilter.class);
final RequestFilter requestFilterChain = RequestFilterChain.newInstance(requestFilter);
final HttpRequest request = mock(HttpRequest.class);
final ResponseHandler responseHandler = null;
requestFilterChain.filter(request, responseHandler);
// Check that the filter is called with the same request argument as the chain was,
// in a manner that allows the request object to be wrapped.
final ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
verify(requestFilter).filter(requestCaptor.capture(), any(ResponseHandler.class));
verify(request, never()).getUri();
requestCaptor.getValue().getUri();
verify(request, times(1)).getUri();
}
use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.
the class FilteringRequestHandler method handleRequest.
@Override
public ContentChannel handleRequest(Request request, ResponseHandler originalResponseHandler) {
Preconditions.checkArgument(request instanceof HttpRequest, "Expected HttpRequest, got " + request);
Objects.requireNonNull(originalResponseHandler, "responseHandler");
RequestFilter requestFilter = requestFilters.resolve(request.getUri());
ResponseFilter responseFilter = responseFilters.resolve(request.getUri());
// Not using request.connect() here - it adds logic for error handling that we'd rather leave to the framework.
RequestHandler resolvedRequestHandler = request.container().resolveHandler(request);
if (resolvedRequestHandler == null) {
throw new BindingNotFoundException(request.getUri());
}
RequestHandler requestHandler = new ReferenceCountingRequestHandler(resolvedRequestHandler);
ResponseHandler responseHandler;
if (responseFilter != null) {
responseHandler = new FilteringResponseHandler(originalResponseHandler, responseFilter, request);
} else {
responseHandler = originalResponseHandler;
}
if (requestFilter != null) {
InterceptingResponseHandler interceptingResponseHandler = new InterceptingResponseHandler(responseHandler);
requestFilter.filter(HttpRequest.class.cast(request), interceptingResponseHandler);
if (interceptingResponseHandler.hasProducedResponse()) {
return COMPLETING_CONTENT_CHANNEL;
}
}
ContentChannel contentChannel = requestHandler.handleRequest(request, responseHandler);
if (contentChannel == null) {
throw new RequestDeniedException(request);
}
return contentChannel;
}
use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.
the class ServerProviderConformanceTest method testRequestNondeterministicExceptionWithAsyncHandleResponse.
private <T extends ServerProvider, U, V> void testRequestNondeterministicExceptionWithAsyncHandleResponse(final Adapter<T, U, V> adapter, final Module... config) throws Throwable {
runTest(adapter, Modules.combine(config), RequestType.WITHOUT_CONTENT, new TestRequestHandler() {
@Override
public ContentChannel handle(final Request request, final ResponseHandler handler) {
callInOtherThread(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
final ContentChannel out = handler.handleResponse(new Response(Response.Status.OK));
closeResponse(out);
} catch (Throwable ignored) {
}
return null;
}
});
throw new ConformanceException();
}
});
}
Aggregations