Search in sources :

Example 6 with ResponseHandler

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));
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) HttpResponse(com.yahoo.jdisc.http.HttpResponse) Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Example 7 with ResponseHandler

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));
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) HttpResponse(com.yahoo.jdisc.http.HttpResponse) Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Example 8 with ResponseHandler

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();
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Example 9 with ResponseHandler

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;
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) RequestHandler(com.yahoo.jdisc.handler.RequestHandler) AbstractRequestHandler(com.yahoo.jdisc.handler.AbstractRequestHandler) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) BindingNotFoundException(com.yahoo.jdisc.handler.BindingNotFoundException) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter)

Example 10 with ResponseHandler

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();
        }
    });
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) Callable(java.util.concurrent.Callable)

Aggregations

ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)15 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)7 RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)7 Response (com.yahoo.jdisc.Response)6 HttpRequest (com.yahoo.jdisc.http.HttpRequest)6 Test (org.testng.annotations.Test)4 Request (com.yahoo.jdisc.Request)3 Callable (java.util.concurrent.Callable)3 RequestDeniedException (com.yahoo.jdisc.handler.RequestDeniedException)2 HttpResponse (com.yahoo.jdisc.http.HttpResponse)2 ResponseFilter (com.yahoo.jdisc.http.filter.ResponseFilter)2 AbstractModule (com.google.inject.AbstractModule)1 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)1 BufferedContentChannel (com.yahoo.jdisc.handler.BufferedContentChannel)1 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)1 FilterBindings (com.yahoo.jdisc.http.server.FilterBindings)1 FilterInvoker (com.yahoo.jdisc.http.server.jetty.FilterInvoker)1 TestDriver (com.yahoo.jdisc.test.TestDriver)1 IOException (java.io.IOException)1