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

The following examples show how to use javax.servlet.Filter#doFilter() . 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 File: TestHostnameFilter.java    From big-c with Apache License 2.0 7 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 File: MockFilterChain.java    From java-technology-stack with 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 File: TestCheckUploadContentTypeFilter.java    From big-c with Apache License 2.0 6 votes vote down vote up
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 4
Source File: TestHostnameFilter.java    From hadoop with 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 5
Source File: TestCheckUploadContentTypeFilter.java    From hadoop with Apache License 2.0 6 votes vote down vote up
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 File: MockFilterChain.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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 7
Source File: MockFilterChain.java    From java-technology-stack with 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 8
Source File: MockFilterChain.java    From spring-analysis-note with 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 9
Source File: CompositeFilter.java    From java-technology-stack with 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 10
Source File: SimpleFilterChain.java    From Jinx with 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 11
Source File: DelegatingFilter.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
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 12
Source File: CompositeFilter.java    From lams with GNU General Public License v2.0 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 13
Source File: GatewayFilter.java    From knox with 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 14
Source File: DelegatingChainFilter.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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 File: TestHostnameFilter.java    From hadoop with 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
Source File: UserAgentMetricFilterTest.java    From data-highway with Apache License 2.0 5 votes vote down vote up
@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 17
Source File: GatewayFilter.java    From knox with 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
Source File: DelegatingFilterProxy.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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
Source File: DelegatingFilterProxy.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
Source File: DelegatingFilterProxy.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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);
}