View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.httpstatus;
18  
19  import java.io.IOException;
20  import java.util.Enumeration;
21  
22  import javax.servlet.Filter;
23  import javax.servlet.FilterChain;
24  import javax.servlet.FilterConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpServletResponseWrapper;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /**
36   * <p>
37   * A response header filter sets the customer status headers from ContentServer.
38   * </p>
39   * 
40   * 
41   * @since 16 June 2009
42   * @author Daniel Iversen, Ram Sabnavis, Dolf Dijkstra
43   * 
44   */
45  public class HttpResponseStatusFilter implements Filter {
46  
47      private static Log log = LogFactory.getLog(HttpResponseStatusFilter.class.getPackage().getName());
48      private boolean sendError = false;
49  
50      /**
51       * This method is called by application server at the shutdown and destroys
52       * filterConf object
53       */
54  
55      public void destroy() {
56  
57      }
58  
59      /**
60       * This method performs the filtering process on the response headers to set
61       * the custom headers in the response object and initiates the subsequent
62       * filter chain
63       * 
64       * @param request Request object
65       * @param response Response Object
66       * @param filterChain FilterChain Object
67       * @throws IOException , ServletException
68       */
69  
70      public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain)
71              throws IOException, ServletException {
72  
73          final HttpServletResponse httpResponse = (HttpServletResponse) response;
74  
75          if (log.isDebugEnabled()) {
76              printRequestHeaders(request);
77          }
78  
79          final HttpServletResponseWrapper wrapper = new StatusFilterHttpResponseWrapper(httpResponse, sendError);
80  
81          filterChain.doFilter(request, wrapper);
82  
83      }
84  
85      private void printRequestHeaders(final ServletRequest request) {
86          final HttpServletRequest httpRequest = (HttpServletRequest) request;
87  
88          for (Enumeration<?> en = httpRequest.getHeaderNames(); en.hasMoreElements();) {
89              String headername = (String) en.nextElement();
90              log.debug(headername + ": " + httpRequest.getHeader(headername));
91          }
92      }
93  
94      /**
95       * This method is called by application server at the startup and
96       * initializes the FilterConfig object
97       * 
98       * @param filterConf FilterConfig object
99       * @throws ServletException
100      */
101 
102     public void init(final FilterConfig filterConf) throws ServletException {
103         sendError = "true".equalsIgnoreCase(filterConf.getInitParameter("sendError"));
104     }
105 }