View Javadoc

1   package com.atlassian.johnson.filters;
2   
3   import com.atlassian.johnson.JohnsonEventContainer;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   import com.atlassian.johnson.event.Event;
7   import com.atlassian.johnson.event.EventLevel;
8   
9   import javax.servlet.http.HttpServletResponse;
10  import javax.servlet.http.HttpServletRequest;
11  import java.io.IOException;
12  import java.util.Collection;
13  
14  /**
15   * A handler that returns a no-content temporarily unavailable response suitable for refusing responses when an
16   * application is unable to handle normal requests. This is especially useful for cases where the normal response
17   * is of an unknown, or dynamic content-type and sending actual content may confuse clients.
18   * <p>
19   * Example uses include AJAX requests, generated images, pdf, excel and word docs.
20   */
21  public class Johnson503Filter extends AbstractJohnsonFilter
22  {
23      private static final Logger log = LoggerFactory.getLogger(Johnson503Filter.class);
24  
25      protected void handleError(JohnsonEventContainer appEventContainer, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException
26      {
27          log.info("The application is unavailable, or there are errors.  Returing a temporarily unavailable status.");
28          servletResponse.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
29          if (hasOnlyWarnings(appEventContainer))
30          {
31              servletResponse.setHeader("Retry-After", "30");
32          }
33          // flushing the writer stops the app server from putting its html message into the otherwise empty response
34          servletResponse.getWriter().flush();
35      }
36  
37      protected void handleNotSetup(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException
38      {
39          log.info("The application is not setup.  Returing a temporarily unavailable status.");
40          servletResponse.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
41          // flushing the writer stops the app server from putting its html message into the otherwise empty response
42          servletResponse.getWriter().flush();
43      }
44      
45      private boolean hasOnlyWarnings(final JohnsonEventContainer eventContainer)
46      {
47          if (eventContainer != null && eventContainer.hasEvents())
48          {
49              final Collection events = eventContainer.getEvents();
50              return allEventsAreWarnings(events);
51          }
52          else
53          {
54              return false;
55          }
56      }
57  
58      private boolean allEventsAreWarnings(final Collection events)
59      {
60          for (final Object eventObject : events)
61          {
62              if (eventObject instanceof Event)
63              {
64                  if (hasNonWarningLevel((Event) eventObject))
65                  {
66                      return false;
67                  }
68              }
69              else
70              {
71                  return false;
72              }
73          }
74          return true;
75      }
76  
77      private boolean hasNonWarningLevel(final Event event)
78      {
79          return !EventLevel.WARNING.equals(event.getLevel().getLevel());
80      }
81  }