View Javadoc

1   package com.atlassian.johnson.spring.lifecycle;
2   
3   import com.atlassian.johnson.spring.web.filter.BypassableDelegatingFilterProxy;
4   
5   import javax.annotation.Nonnull;
6   import javax.servlet.FilterChain;
7   import javax.servlet.ServletException;
8   import javax.servlet.ServletRequest;
9   import javax.servlet.ServletResponse;
10  import java.io.IOException;
11  
12  /**
13   * A specialized {@link BypassableDelegatingFilterProxy} for use with the {@link LifecycleDispatcherServlet}. This
14   * proxy automatically bypasses the filter when requests are received during application startup.
15   *
16   * @since 3.0
17   */
18  public class LifecycleDelegatingFilterProxy extends BypassableDelegatingFilterProxy {
19  
20      private volatile boolean starting = true;
21  
22      @Override
23      public void doFilter(@Nonnull ServletRequest request, @Nonnull ServletResponse response,
24                           @Nonnull FilterChain filterChain) throws ServletException, IOException {
25          if (starting) {
26              if (LifecycleUtils.isStarting(getServletContext())) {
27                  //Otherwise, if the application is still starting, bypass the filter and invoke the chain
28                  filterChain.doFilter(request, response);
29  
30                  return;
31              } else {
32                  //The first time we see a status other than CREATED or STARTING, note that the application
33                  //is no longer starting and apply the filter normally
34                  starting = false;
35              }
36          }
37  
38          super.doFilter(request, response, filterChain);
39      }
40  }