Search in sources :

Example 6 with ResponseFilter

use of com.yahoo.jdisc.http.filter.ResponseFilter in project vespa by vespa-engine.

the class FilterTestCase method requireThatResponseFilterChainIsRun.

@Test
public void requireThatResponseFilterChainIsRun() {
    final ResponseFilter responseFilter1 = new ResponseHeaderFilter("foo", "bar");
    final ResponseFilter responseFilter2 = mock(ResponseFilter.class);
    final int statusCode = Response.Status.BAD_GATEWAY;
    final Response response = new Response(statusCode);
    final Request request = null;
    ResponseFilterChain.newInstance(responseFilter1, responseFilter2).filter(response, request);
    final ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(responseFilter2).filter(responseCaptor.capture(), any(Request.class));
    assertThat(responseCaptor.getValue().getStatus(), is(statusCode));
    assertThat(responseCaptor.getValue().headers().getFirst("foo"), is("bar"));
    assertThat(response.getStatus(), is(statusCode));
    assertThat(response.headers().getFirst("foo"), is("bar"));
}
Also used : HttpResponse(com.yahoo.jdisc.http.HttpResponse) Response(com.yahoo.jdisc.Response) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) HttpRequest(com.yahoo.jdisc.http.HttpRequest) Request(com.yahoo.jdisc.Request) ResponseHeaderFilter(com.yahoo.jdisc.http.filter.ResponseHeaderFilter) Test(org.testng.annotations.Test)

Example 7 with ResponseFilter

use of com.yahoo.jdisc.http.filter.ResponseFilter 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 8 with ResponseFilter

use of com.yahoo.jdisc.http.filter.ResponseFilter in project vespa by vespa-engine.

the class FilterBindingsProviderTest method requireThatCorrectlyConfiguredFiltersAreIncluded.

@Test
public void requireThatCorrectlyConfiguredFiltersAreIncluded() {
    final String requestFilter1Id = "requestFilter1";
    final String requestFilter2Id = "requestFilter2";
    final String requestFilter3Id = "requestFilter3";
    final String responseFilter1Id = "responseFilter1";
    final String responseFilter2Id = "responseFilter2";
    final String responseFilter3Id = "responseFilter3";
    // Set up config.
    configBuilder.filter(new ServerConfig.Filter.Builder().id(requestFilter1Id).binding("http://*/a"));
    configBuilder.filter(new ServerConfig.Filter.Builder().id(requestFilter2Id).binding("http://*/b"));
    configBuilder.filter(new ServerConfig.Filter.Builder().id(responseFilter1Id).binding("http://*/c"));
    configBuilder.filter(new ServerConfig.Filter.Builder().id(responseFilter3Id).binding("http://*/d"));
    // Set up registry.
    final ComponentRegistry<RequestFilter> availableRequestFilters = new ComponentRegistry<>();
    final RequestFilter requestFilter1Instance = mock(RequestFilter.class);
    final RequestFilter requestFilter2Instance = mock(RequestFilter.class);
    final RequestFilter requestFilter3Instance = mock(RequestFilter.class);
    availableRequestFilters.register(ComponentId.fromString(requestFilter1Id), requestFilter1Instance);
    availableRequestFilters.register(ComponentId.fromString(requestFilter2Id), requestFilter2Instance);
    availableRequestFilters.register(ComponentId.fromString(requestFilter3Id), requestFilter3Instance);
    final ComponentRegistry<ResponseFilter> availableResponseFilters = new ComponentRegistry<>();
    final ResponseFilter responseFilter1Instance = mock(ResponseFilter.class);
    final ResponseFilter responseFilter2Instance = mock(ResponseFilter.class);
    final ResponseFilter responseFilter3Instance = mock(ResponseFilter.class);
    availableResponseFilters.register(ComponentId.fromString(responseFilter1Id), responseFilter1Instance);
    availableResponseFilters.register(ComponentId.fromString(responseFilter2Id), responseFilter2Instance);
    availableResponseFilters.register(ComponentId.fromString(responseFilter3Id), responseFilter3Instance);
    final FilterChainRepository filterChainRepository = new FilterChainRepository(new ChainsConfig(new ChainsConfig.Builder()), availableRequestFilters, availableResponseFilters, new ComponentRegistry<SecurityRequestFilter>(), new ComponentRegistry<SecurityResponseFilter>());
    // Set up the provider that we aim to test.
    final FilterBindingsProvider provider = new FilterBindingsProvider(new ComponentId("foo"), new ServerConfig(configBuilder), filterChainRepository, new ComponentRegistry<SecurityRequestFilter>());
    // Execute.
    final FilterBindings filterBindings = provider.get();
    // Verify.
    assertThat(filterBindings, is(not(nullValue())));
    assertThat(filterBindings.getRequestFilters(), containsFilters(requestFilter1Instance, requestFilter2Instance));
    assertThat(filterBindings.getRequestFilters(), not(containsFilters(requestFilter3Instance)));
    assertThat(filterBindings.getResponseFilters(), containsFilters(responseFilter1Instance, responseFilter3Instance));
    assertThat(filterBindings.getResponseFilters(), not(containsFilters(responseFilter2Instance)));
}
Also used : SecurityResponseFilter(com.yahoo.jdisc.http.filter.SecurityResponseFilter) ChainsConfig(com.yahoo.container.core.ChainsConfig) FilterBindings(com.yahoo.jdisc.http.server.FilterBindings) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ServerConfig(com.yahoo.jdisc.http.ServerConfig) SecurityRequestFilter(com.yahoo.jdisc.http.filter.SecurityRequestFilter) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) SecurityRequestFilter(com.yahoo.jdisc.http.filter.SecurityRequestFilter) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) SecurityResponseFilter(com.yahoo.jdisc.http.filter.SecurityResponseFilter) ComponentRegistry(com.yahoo.component.provider.ComponentRegistry) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) SecurityResponseFilter(com.yahoo.jdisc.http.filter.SecurityResponseFilter) FilterChainRepository(com.yahoo.container.http.filter.FilterChainRepository) ComponentId(com.yahoo.component.ComponentId) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) SecurityRequestFilter(com.yahoo.jdisc.http.filter.SecurityRequestFilter) Test(org.junit.Test)

Example 9 with ResponseFilter

use of com.yahoo.jdisc.http.filter.ResponseFilter in project vespa by vespa-engine.

the class FilterTestCase method requireThatRequestFilterChangesAreSeenByRequestHandler.

@Test
public void requireThatRequestFilterChangesAreSeenByRequestHandler() throws Exception {
    final RequestFilter filter = new HeaderRequestFilter("foo", "bar");
    final BindingRepository<RequestFilter> requestFilters = new BindingRepository<>();
    requestFilters.bind("http://*/*", filter);
    final BindingRepository<ResponseFilter> responseFilters = null;
    final MyRequestHandler requestHandler = new MyRequestHandler();
    final TestDriver testDriver = newDriver(requestHandler, requestFilters, responseFilters);
    testDriver.client().get("status.html");
    assertThat(requestHandler.awaitInvocation(), is(true));
    assertThat(requestHandler.getHeaderMap().get("foo").get(0), is("bar"));
    assertThat(testDriver.close(), is(true));
}
Also used : BindingRepository(com.yahoo.jdisc.application.BindingRepository) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Example 10 with ResponseFilter

use of com.yahoo.jdisc.http.filter.ResponseFilter in project vespa by vespa-engine.

the class FilterTestCase method requireThatResponseFilterIsNotRunOnUnboundPath.

@Test
public void requireThatResponseFilterIsNotRunOnUnboundPath() throws Exception {
    final ResponseFilter filter = mock(ResponseFilterMockBase.class);
    final BindingRepository<RequestFilter> requestFilters = null;
    final BindingRepository<ResponseFilter> responseFilters = new BindingRepository<>();
    responseFilters.bind("http://*/filtered/*", filter);
    final MyRequestHandler requestHandler = new MyRequestHandler();
    final TestDriver testDriver = newDriver(requestHandler, requestFilters, responseFilters);
    testDriver.client().get("/status.html");
    assertThat(requestHandler.awaitInvocation(), is(true));
    verify(filter, never()).filter(any(Response.class), any(Request.class));
    assertThat(testDriver.close(), is(true));
}
Also used : HttpResponse(com.yahoo.jdisc.http.HttpResponse) Response(com.yahoo.jdisc.Response) BindingRepository(com.yahoo.jdisc.application.BindingRepository) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) HttpRequest(com.yahoo.jdisc.http.HttpRequest) Request(com.yahoo.jdisc.Request) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Aggregations

ResponseFilter (com.yahoo.jdisc.http.filter.ResponseFilter)11 RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)9 Test (org.testng.annotations.Test)8 HttpRequest (com.yahoo.jdisc.http.HttpRequest)7 BindingRepository (com.yahoo.jdisc.application.BindingRepository)6 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)5 Request (com.yahoo.jdisc.Request)4 Response (com.yahoo.jdisc.Response)4 HttpResponse (com.yahoo.jdisc.http.HttpResponse)4 FilterBindings (com.yahoo.jdisc.http.server.FilterBindings)2 AbstractModule (com.google.inject.AbstractModule)1 ComponentId (com.yahoo.component.ComponentId)1 ComponentRegistry (com.yahoo.component.provider.ComponentRegistry)1 ChainsConfig (com.yahoo.container.core.ChainsConfig)1 FilterChainRepository (com.yahoo.container.http.filter.FilterChainRepository)1 ResourceReference (com.yahoo.jdisc.ResourceReference)1 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)1 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)1 RequestDeniedException (com.yahoo.jdisc.handler.RequestDeniedException)1