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

The following examples show how to use javax.servlet.Filter#destroy() . 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: FilterDefinition.java    From dagger-servlet with Apache License 2.0 6 votes vote down vote up
public void destroy(Set<Filter> destroyedSoFar) {
    // filters are always singletons
    Filter reference = filter.get();

    // Do nothing if this Filter was invalid (usually due to not being scoped
    // properly), or was already destroyed. According to Servlet Spec: it is
    // "out of service", and does not need to be destroyed.
    // Also prevent duplicate destroys to the same singleton that may appear
    // more than once on the filter chain.
    if (null == reference || destroyedSoFar.contains(reference)) {
        return;
    }

    try {
        reference.destroy();
    } finally {
        destroyedSoFar.add(reference);
    }
}
 
Example 3
Source File: HttpServletProtocol.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
/**
 * Destruction filter
 */
protected void destroyFilter(){
    Map<String, ServletFilterRegistration> servletRegistrationMap = servletContext.getFilterRegistrations();
    for(ServletFilterRegistration registration : servletRegistrationMap.values()){
        Filter filter = registration.getFilter();
        if(filter == null) {
            continue;
        }
        if(registration.isInitFilter()){
            try {
                filter.destroy();
            }catch (Exception e){
                logger.error("destroyFilter error={},filter={}",e.toString(),filter,e);
            }
        }
    }
}
 
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: CompositeFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Clean up all the filters supplied, calling each one's destroy method in turn, but in reverse order.
 * @see Filter#init(FilterConfig)
 */
@Override
public void destroy() {
	for (int i = this.filters.size(); i-- > 0;) {
		Filter filter = this.filters.get(i);
		filter.destroy();
	}
}
 
Example 6
Source File: BootstrapFilter.java    From rice with Educational Community License v2.0 5 votes vote down vote up
public void destroy() {
	for (Filter filter : filters.values()) {
		try {
			filter.destroy();
		} catch (Exception e) {
			LOG.error("Error destroying filter: " + filter, e);
		}
	}
}
 
Example 7
Source File: GatewayServlet.java    From hadoop-mini-clusters with Apache License 2.0 5 votes vote down vote up
public synchronized void setFilter( GatewayFilter filter ) throws ServletException {
    Filter prev = filter;
    if( filterConfig != null ) {
        filter.init( filterConfig );
    }
    this.filter = filter;
    if( prev != null && filterConfig != null ) {
        prev.destroy();
    }
}
 
Example 8
Source File: DelegatingFilter.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public static synchronized void unset(Filter filter) {
  if (delegate == filter) {
    delegate = null;
  }
  if (cachedConfig != null) {
    filter.destroy();
  }
}
 
Example 9
Source File: TestHostnameFilter.java    From big-c 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 10
Source File: CompositeFilter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Clean up all the filters supplied, calling each one's destroy method in turn, but in reverse order.
 * @see Filter#init(FilterConfig)
 */
@Override
public void destroy() {
	for (int i = this.filters.size(); i-- > 0;) {
		Filter filter = this.filters.get(i);
		filter.destroy();
	}
}
 
Example 11
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 12
Source File: CompositeFilter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Clean up all the filters supplied, calling each one's destroy method in turn, but in reverse order.
 * @see Filter#init(FilterConfig)
 */
@Override
public void destroy() {
	for (int i = this.filters.size(); i-- > 0;) {
		Filter filter = this.filters.get(i);
		filter.destroy();
	}
}
 
Example 13
Source File: CompositeFilter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Clean up all the filters supplied, calling each one's destroy method in turn, but in reverse order.
 * @see Filter#init(FilterConfig)
 */
@Override
public void destroy() {
	for (int i = this.filters.size(); i-- > 0;) {
		Filter filter = this.filters.get(i);
		filter.destroy();
	}
}
 
Example 14
Source File: TestMDCFilter.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Test
public void mdc() throws Exception {
  HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
  Mockito.when(request.getUserPrincipal()).thenReturn(null);
  Mockito.when(request.getMethod()).thenReturn("METHOD");
  Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");

  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 {
      assertEquals(MDC.get("hostname"), null);
      assertEquals(MDC.get("user"), null);
      assertEquals(MDC.get("method"), "METHOD");
      assertEquals(MDC.get("path"), "/pathinfo");
      invoked.set(true);
    }
  };

  MDC.clear();
  Filter filter = new MDCFilter();
  filter.init(null);

  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());
  assertNull(MDC.get("hostname"));
  assertNull(MDC.get("user"));
  assertNull(MDC.get("method"));
  assertNull(MDC.get("path"));

  Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {
    @Override
    public String getName() {
      return "name";
    }
  });

  invoked.set(false);
  chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      assertEquals(MDC.get("hostname"), null);
      assertEquals(MDC.get("user"), "name");
      assertEquals(MDC.get("method"), "METHOD");
      assertEquals(MDC.get("path"), "/pathinfo");
      invoked.set(true);
    }
  };
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());

  HostnameFilter.HOSTNAME_TL.set("HOST");

  invoked.set(false);
  chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      assertEquals(MDC.get("hostname"), "HOST");
      assertEquals(MDC.get("user"), "name");
      assertEquals(MDC.get("method"), "METHOD");
      assertEquals(MDC.get("path"), "/pathinfo");
      invoked.set(true);
    }
  };
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());

  HostnameFilter.HOSTNAME_TL.remove();

  filter.destroy();
}
 
Example 15
Source File: TestMDCFilter.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Test
public void mdc() throws Exception {
  HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
  Mockito.when(request.getUserPrincipal()).thenReturn(null);
  Mockito.when(request.getMethod()).thenReturn("METHOD");
  Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");

  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 {
      assertEquals(MDC.get("hostname"), null);
      assertEquals(MDC.get("user"), null);
      assertEquals(MDC.get("method"), "METHOD");
      assertEquals(MDC.get("path"), "/pathinfo");
      invoked.set(true);
    }
  };

  MDC.clear();
  Filter filter = new MDCFilter();
  filter.init(null);

  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());
  assertNull(MDC.get("hostname"));
  assertNull(MDC.get("user"));
  assertNull(MDC.get("method"));
  assertNull(MDC.get("path"));

  Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {
    @Override
    public String getName() {
      return "name";
    }
  });

  invoked.set(false);
  chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      assertEquals(MDC.get("hostname"), null);
      assertEquals(MDC.get("user"), "name");
      assertEquals(MDC.get("method"), "METHOD");
      assertEquals(MDC.get("path"), "/pathinfo");
      invoked.set(true);
    }
  };
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());

  HostnameFilter.HOSTNAME_TL.set("HOST");

  invoked.set(false);
  chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      assertEquals(MDC.get("hostname"), "HOST");
      assertEquals(MDC.get("user"), "name");
      assertEquals(MDC.get("method"), "METHOD");
      assertEquals(MDC.get("path"), "/pathinfo");
      invoked.set(true);
    }
  };
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());

  HostnameFilter.HOSTNAME_TL.remove();

  filter.destroy();
}
 
Example 16
Source File: JettySolrRunner.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
   * Stop the Jetty server
   *
   * @throws Exception if an error occurs on shutdown
   */
  public void stop() throws Exception {
    // Do not let Jetty/Solr pollute the MDC for this thread
    Map<String,String> prevContext = MDC.getCopyOfContextMap();
    MDC.clear();
    try {
      Filter filter = dispatchFilter.getFilter();

      // we want to shutdown outside of jetty cutting us off
      SolrDispatchFilter sdf = getSolrDispatchFilter();
      ExecutorService customThreadPool = null;
      if (sdf != null) {
        customThreadPool = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrNamedThreadFactory("jettyShutDown"));

        sdf.closeOnDestroy(false);
//        customThreadPool.submit(() -> {
//          try {
//            sdf.close();
//          } catch (Throwable t) {
//            log.error("Error shutting down Solr", t);
//          }
//        });
        try {
          sdf.close();
        } catch (Throwable t) {
          log.error("Error shutting down Solr", t);
        }
      }

      QueuedThreadPool qtp = (QueuedThreadPool) server.getThreadPool();
      ReservedThreadExecutor rte = qtp.getBean(ReservedThreadExecutor.class);

      server.stop();

      if (server.getState().equals(Server.FAILED)) {
        filter.destroy();
        if (extraFilters != null) {
          for (FilterHolder f : extraFilters) {
            f.getFilter().destroy();
          }
        }
      }

      // stop timeout is 0, so we will interrupt right away
      while(!qtp.isStopped()) {
        qtp.stop();
        if (qtp.isStopped()) {
          Thread.sleep(50);
        }
      }

      // we tried to kill everything, now we wait for executor to stop
      qtp.setStopTimeout(Integer.MAX_VALUE);
      qtp.stop();
      qtp.join();

      if (rte != null) {
        // we try and wait for the reserved thread executor, but it doesn't always seem to work
        // so we actually set 0 reserved threads at creation

        rte.stop();

        TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);
        timeout.waitFor("Timeout waiting for reserved executor to stop.", ()
            -> rte.isStopped());
      }

      if (customThreadPool != null) {
        ExecutorUtil.shutdownAndAwaitTermination(customThreadPool);
      }

      do {
        try {
          server.join();
        } catch (InterruptedException e) {
          // ignore
        }
      } while (!server.isStopped());

    } finally {
      if (enableProxy) {
        proxy.close();
      }

      if (prevContext != null) {
        MDC.setContextMap(prevContext);
      } else {
        MDC.clear();
      }
    }
  }
 
Example 17
Source File: DelegatingFilterProxy.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Destroy the Filter delegate.
 * Default implementation simply calls {@code Filter.destroy} on it.
 * @param delegate the Filter delegate (never {@code null})
 * @see #isTargetFilterLifecycle()
 * @see javax.servlet.Filter#destroy()
 */
protected void destroyDelegate(Filter delegate) {
	if (isTargetFilterLifecycle()) {
		delegate.destroy();
	}
}
 
Example 18
Source File: DelegatingFilterProxy.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Destroy the Filter delegate.
 * Default implementation simply calls {@code Filter.destroy} on it.
 * @param delegate the Filter delegate (never {@code null})
 * @see #isTargetFilterLifecycle()
 * @see javax.servlet.Filter#destroy()
 */
protected void destroyDelegate(Filter delegate) {
	if (isTargetFilterLifecycle()) {
		delegate.destroy();
	}
}
 
Example 19
Source File: DelegatingFilterProxy.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Destroy the Filter delegate.
 * Default implementation simply calls {@code Filter.destroy} on it.
 * @param delegate the Filter delegate (never {@code null})
 * @see #isTargetFilterLifecycle()
 * @see javax.servlet.Filter#destroy()
 */
protected void destroyDelegate(Filter delegate) {
	if (isTargetFilterLifecycle()) {
		delegate.destroy();
	}
}
 
Example 20
Source File: DelegatingFilterProxy.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Destroy the Filter delegate.
 * Default implementation simply calls {@code Filter.destroy} on it.
 * @param delegate the Filter delegate (never {@code null})
 * @see #isTargetFilterLifecycle()
 * @see javax.servlet.Filter#destroy()
 */
protected void destroyDelegate(Filter delegate) {
	if (isTargetFilterLifecycle()) {
		delegate.destroy();
	}
}