use of com.yahoo.jdisc.http.filter.RequestFilter 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.http.filter.RequestFilter in project vespa by vespa-engine.
the class FilterTestCase method requireThatRequestFilterAndResponseFilterCanBindToSamePath.
@Test
public void requireThatRequestFilterAndResponseFilterCanBindToSamePath() throws Exception {
final String uriPattern = "http://*/*";
final BindingRepository<RequestFilter> requestFilters = new BindingRepository<>();
final RequestFilter requestFilter = mock(RequestFilterMockBase.class);
requestFilters.bind(uriPattern, requestFilter);
final BindingRepository<ResponseFilter> responseFilters = new BindingRepository<>();
final ResponseFilter responseFilter = mock(ResponseFilterMockBase.class);
responseFilters.bind(uriPattern, responseFilter);
final MyRequestHandler requestHandler = new MyRequestHandler();
final TestDriver testDriver = newDriver(requestHandler, requestFilters, responseFilters);
testDriver.client().get("/status.html");
assertThat(requestHandler.awaitInvocation(), is(true));
verify(requestFilter, times(1)).filter(any(HttpRequest.class), any(ResponseHandler.class));
verify(responseFilter, times(1)).filter(any(Response.class), any(Request.class));
assertThat(testDriver.close(), is(true));
}
use of com.yahoo.jdisc.http.filter.RequestFilter 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.http.filter.RequestFilter 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)));
}
use of com.yahoo.jdisc.http.filter.RequestFilter in project vespa by vespa-engine.
the class FilterBindingsProviderTest method requireThatInstanceCanNotBeBothRequestAndResponseFilter.
@Test
public void requireThatInstanceCanNotBeBothRequestAndResponseFilter() {
final String filterId = "filter";
// Set up config.
configBuilder.filter(new ServerConfig.Filter.Builder().id(filterId).binding("http://*/*"));
// Set up registry.
final DualRoleFilter filterInstance = mock(DualRoleFilter.class);
final ComponentRegistry<RequestFilter> availableRequestFilters = new ComponentRegistry<>();
availableRequestFilters.register(ComponentId.fromString(filterId), filterInstance);
final FilterChainRepository filterChainRepository = new FilterChainRepository(new ChainsConfig(new ChainsConfig.Builder()), availableRequestFilters, new ComponentRegistry<>(), new ComponentRegistry<>(), new ComponentRegistry<>());
try {
new FilterBindingsProvider(new ComponentId("foo"), new ServerConfig(configBuilder), filterChainRepository, new ComponentRegistry<>());
fail("Dual-role filter should not be accepted");
} catch (RuntimeException e) {
assertThat(e.getMessage(), containsString("Invalid config"));
}
}
Aggregations