Java Code Examples for javax.servlet.Filter#doFilter()

The following examples show how to use javax.servlet.Filter#doFilter() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: big-c   File: TestHostnameFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMissingHostname() throws Exception {
  ServletRequest request = Mockito.mock(ServletRequest.class);
  Mockito.when(request.getRemoteAddr()).thenReturn(null);

  ServletResponse response = Mockito.mock(ServletResponse.class);

  final AtomicBoolean invoked = new AtomicBoolean();

  FilterChain chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      assertTrue(HostnameFilter.get().contains("???"));
      invoked.set(true);
    }
  };

  Filter filter = new HostnameFilter();
  filter.init(null);
  assertNull(HostnameFilter.get());
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());
  assertNull(HostnameFilter.get());
  filter.destroy();
}
 
Example 2
Source Project: spring-analysis-note   File: MockFilterChain.java    License: MIT License 6 votes vote down vote up
/**
 * Invoke registered {@link Filter Filters} and/or {@link Servlet} also saving the
 * request and response.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
	Assert.notNull(request, "Request must not be null");
	Assert.notNull(response, "Response must not be null");
	Assert.state(this.request == null, "This FilterChain has already been called!");

	if (this.iterator == null) {
		this.iterator = this.filters.iterator();
	}

	if (this.iterator.hasNext()) {
		Filter nextFilter = this.iterator.next();
		nextFilter.doFilter(request, response, this);
	}

	this.request = request;
	this.response = response;
}
 
Example 3
Source Project: java-technology-stack   File: MockFilterChain.java    License: MIT License 6 votes vote down vote up
/**
 * Invoke registered {@link Filter Filters} and/or {@link Servlet} also saving the
 * request and response.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
	Assert.notNull(request, "Request must not be null");
	Assert.notNull(response, "Response must not be null");
	Assert.state(this.request == null, "This FilterChain has already been called!");

	if (this.iterator == null) {
		this.iterator = this.filters.iterator();
	}

	if (this.iterator.hasNext()) {
		Filter nextFilter = this.iterator.next();
		nextFilter.doFilter(request, response, this);
	}

	this.request = request;
	this.response = response;
}
 
Example 4
Source Project: java-technology-stack   File: MockFilterChain.java    License: MIT License 6 votes vote down vote up
/**
 * Invoke registered {@link Filter Filters} and/or {@link Servlet} also saving the
 * request and response.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
	Assert.notNull(request, "Request must not be null");
	Assert.notNull(response, "Response must not be null");
	Assert.state(this.request == null, "This FilterChain has already been called!");

	if (this.iterator == null) {
		this.iterator = this.filters.iterator();
	}

	if (this.iterator.hasNext()) {
		Filter nextFilter = this.iterator.next();
		nextFilter.doFilter(request, response, this);
	}

	this.request = request;
	this.response = response;
}
 
Example 5
private void test(String method, String operation, String contentType,
                  boolean upload, boolean error) throws Exception {
  HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
  HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
  Mockito.reset(request);
  Mockito.when(request.getMethod()).thenReturn(method);
  Mockito.when(request.getParameter(HttpFSFileSystem.OP_PARAM)).thenReturn(operation);
  Mockito.when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).
    thenReturn(Boolean.toString(upload));
  Mockito.when(request.getContentType()).thenReturn(contentType);

  FilterChain chain = Mockito.mock(FilterChain.class);

  Filter filter = new CheckUploadContentTypeFilter();

  filter.doFilter(request, response, chain);

  if (error) {
    Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_BAD_REQUEST),
                                       Mockito.contains("Data upload"));
  }
  else {
    Mockito.verify(chain).doFilter(request, response);
  }
}
 
Example 6
Source Project: hadoop   File: TestHostnameFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMissingHostname() throws Exception {
  ServletRequest request = Mockito.mock(ServletRequest.class);
  Mockito.when(request.getRemoteAddr()).thenReturn(null);

  ServletResponse response = Mockito.mock(ServletResponse.class);

  final AtomicBoolean invoked = new AtomicBoolean();

  FilterChain chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      assertTrue(HostnameFilter.get().contains("???"));
      invoked.set(true);
    }
  };

  Filter filter = new HostnameFilter();
  filter.init(null);
  assertNull(HostnameFilter.get());
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());
  assertNull(HostnameFilter.get());
  filter.destroy();
}
 
Example 7
private void test(String method, String operation, String contentType,
                  boolean upload, boolean error) throws Exception {
  HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
  HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
  Mockito.reset(request);
  Mockito.when(request.getMethod()).thenReturn(method);
  Mockito.when(request.getParameter(HttpFSFileSystem.OP_PARAM)).thenReturn(operation);
  Mockito.when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).
    thenReturn(Boolean.toString(upload));
  Mockito.when(request.getContentType()).thenReturn(contentType);

  FilterChain chain = Mockito.mock(FilterChain.class);

  Filter filter = new CheckUploadContentTypeFilter();

  filter.doFilter(request, response, chain);

  if (error) {
    Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_BAD_REQUEST),
                                       Mockito.contains("Data upload"));
  }
  else {
    Mockito.verify(chain).doFilter(request, response);
  }
}
 
Example 8
/**
 * Invoke registered {@link Filter}s and/or {@link Servlet} also saving the
 * request and response.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
	Assert.notNull(request, "Request must not be null");
	Assert.notNull(response, "Response must not be null");

	if (this.request != null) {
		 throw new IllegalStateException("This FilterChain has already been called!");
	}

	if (this.iterator == null) {
		this.iterator = this.filters.iterator();
	}

	if (this.iterator.hasNext()) {
		Filter nextFilter = this.iterator.next();
		nextFilter.doFilter(request, response, this);
	}

	this.request = request;
	this.response = response;
}
 
Example 9
@Test
public void testName() throws Exception {
  when(request.getRequestURI()).thenReturn("/onramp/v1/roads/road1/messages");
  when(request.getHeader("User-Agent")).thenReturn("product1/1.0 product2/2.0 product3/3.0");
  Set<String> products = new HashSet<>(Arrays.asList("product1", "product2", "product4"));
  Filter underTest = new UserAgentMetricFilter(registry, products);

  underTest.doFilter(request, response, chain);

  assertThat(counter1.count(), is(1.0));
  assertThat(counter2.count(), is(1.0));
  assertThat(counter3.count(), is(0.0));
  assertThat(counter4.count(), is(0.0));
  verify(chain).doFilter(request, response);
}
 
Example 10
Source Project: knox   File: GatewayFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse ) throws IOException, ServletException {
  if( chainList != null && !chainList.isEmpty() ) {
    final Filter filter = chainList.get( 0 );
    final FilterChain chain = subChain();
    filter.doFilter( servletRequest, servletResponse, chain );
  }
}
 
Example 11
Source Project: java-technology-stack   File: CompositeFilter.java    License: MIT License 5 votes vote down vote up
@Override
public void doFilter(final ServletRequest request, final ServletResponse response)
		throws IOException, ServletException {

	if (this.currentPosition == this.additionalFilters.size()) {
		this.originalChain.doFilter(request, response);
	}
	else {
		this.currentPosition++;
		Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1);
		nextFilter.doFilter(request, response, this);
	}
}
 
Example 12
Source Project: Jinx   File: SimpleFilterChain.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if (filterIterator.hasNext()) {
        Filter filter = filterIterator.next();
        filter.doFilter(request, response, this);
    } else {
        servlet.service(request, response);
    }
}
 
Example 13
@Override
public void doFilter(final ServletRequest request, final ServletResponse response)
		throws IOException, ServletException {

	if (this.currentPosition == this.additionalFilters.size()) {
		this.originalChain.doFilter(request, response);
	}
	else {
		this.currentPosition++;
		Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1);
		nextFilter.doFilter(request, response, this);
	}
}
 
Example 14
@Override
public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
	if (filterIterator.hasNext()) {
		Filter filter = filterIterator.next();
		filter.doFilter(req, res, this);
	} else {
		defaultChain.doFilter(req, res);
	}
}
 
Example 15
Source Project: hadoop   File: TestHostnameFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void hostname() throws Exception {
  ServletRequest request = Mockito.mock(ServletRequest.class);
  Mockito.when(request.getRemoteAddr()).thenReturn("localhost");

  ServletResponse response = Mockito.mock(ServletResponse.class);

  final AtomicBoolean invoked = new AtomicBoolean();

  FilterChain chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      // Hostname was set to "localhost", but may get resolved automatically to
      // "127.0.0.1" depending on OS.
      assertTrue(HostnameFilter.get().contains("localhost") ||
        HostnameFilter.get().contains("127.0.0.1"));
      invoked.set(true);
    }
  };

  Filter filter = new HostnameFilter();
  filter.init(null);
  assertNull(HostnameFilter.get());
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());
  assertNull(HostnameFilter.get());
  filter.destroy();
}
 
Example 16
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
    ServletException
{
  Filter filter = delegate;
  if (filter != null) {
    filter.doFilter(request, response, chain);
  }
  else if (response instanceof HttpServletResponse) {
    ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
  }
  else {
    chain.doFilter(request, response);
  }
}
 
Example 17
Source Project: knox   File: GatewayFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain ) throws IOException, ServletException {
  final Filter filter = getInstance();
  filter.doFilter( servletRequest, servletResponse, filterChain );
}
 
Example 18
/**
 * Actually invoke the delegate Filter with the given request and response.
 * @param delegate the delegate Filter
 * @param request the current HTTP request
 * @param response the current HTTP response
 * @param filterChain the current FilterChain
 * @throws ServletException if thrown by the Filter
 * @throws IOException if thrown by the Filter
 */
protected void invokeDelegate(
		Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	delegate.doFilter(request, response, filterChain);
}
 
Example 19
/**
 * Actually invoke the delegate Filter with the given request and response.
 * @param delegate the delegate Filter
 * @param request the current HTTP request
 * @param response the current HTTP response
 * @param filterChain the current FilterChain
 * @throws ServletException if thrown by the Filter
 * @throws IOException if thrown by the Filter
 */
protected void invokeDelegate(
		Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	delegate.doFilter(request, response, filterChain);
}
 
Example 20
/**
 * Actually invoke the delegate Filter with the given request and response.
 * @param delegate the delegate Filter
 * @param request the current HTTP request
 * @param response the current HTTP response
 * @param filterChain the current FilterChain
 * @throws ServletException if thrown by the Filter
 * @throws IOException if thrown by the Filter
 */
protected void invokeDelegate(
		Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	delegate.doFilter(request, response, filterChain);
}