View Javadoc

1   /**
2    * Atlassian Source Code Template.
3    * User: Bobby
4    * Date: Apr 8, 2003
5    * Time: 9:07:18 AM
6    * CVS Revision: $Revision: 1.5 $
7    * Last CVS Commit: $Date: 2006/10/09 01:01:38 $
8    * Author of last CVS Commit: $Author: bkuo $
9    */
10  
11  package com.atlassian.johnson.filters;
12  
13  import com.atlassian.johnson.JohnsonEventContainer;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  import javax.servlet.http.HttpServletRequest;
18  import javax.servlet.http.HttpServletResponse;
19  import java.io.IOException;
20  import java.net.URLEncoder;
21  
22  /**
23   * A filter that handles cases where the application is unable to handle a normal request and redirects to the
24   * configured error path so that a nice error page can be provided.
25   */
26  public class JohnsonFilter extends AbstractJohnsonFilter {
27  
28      private static final Logger LOG = LoggerFactory.getLogger(JohnsonFilter.class);
29  
30      protected void handleError(JohnsonEventContainer appEventContainer, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
31          String servletPath = getServletPath(servletRequest);
32          String contextPath = servletRequest.getContextPath();
33          LOG.info("The application is still starting up, or there are errors. Redirecting request from '{}' to '{}'",
34                  servletPath, config.getErrorPath());
35  
36          String nextUrl = servletRequest.getRequestURI();
37  
38          if (servletRequest.getQueryString() != null && !servletRequest.getQueryString().isEmpty()) {
39              nextUrl += "?" + servletRequest.getQueryString();
40          }
41  
42          String redirectUrl = contextPath + config.getErrorPath()
43                  + "?next=" + URLEncoder.encode(nextUrl, "UTF-8");
44  
45          servletResponse.sendRedirect(redirectUrl);
46      }
47  
48      protected void handleNotSetup(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
49          String servletPath = getServletPath(servletRequest);
50          String contextPath = servletRequest.getContextPath();
51          LOG.info("The application is not yet setup. Redirecting request from '{}' to '{}'",
52                  servletPath, config.getSetupPath());
53          servletResponse.sendRedirect(contextPath + config.getSetupPath());
54      }
55  }