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

The following examples show how to use javax.servlet.Filter#destroy() . 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
/**
 * 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 2
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 3
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 4
Source Project: dagger-servlet   File: FilterDefinition.java    License: 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 5
Source Project: spring-analysis-note   File: CompositeFilter.java    License: 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 Project: java-technology-stack   File: CompositeFilter.java    License: 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 7
/**
 * 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 8
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 9
/**
 * 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 10
Source Project: big-c   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 11
public static synchronized void unset(Filter filter) {
  if (delegate == filter) {
    delegate = null;
  }
  if (cachedConfig != null) {
    filter.destroy();
  }
}
 
Example 12
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 13
public void destroy() {
	for (Filter filter : filters.values()) {
		try {
			filter.destroy();
		} catch (Exception e) {
			LOG.error("Error destroying filter: " + filter, e);
		}
	}
}
 
Example 14
Source Project: hadoop   File: TestMDCFilter.java    License: 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 Project: big-c   File: TestMDCFilter.java    License: 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 Project: lucene-solr   File: JettySolrRunner.java    License: 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
/**
 * 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
/**
 * 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
/**
 * 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
/**
 * 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();
	}
}