View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.filters;
20  
21  import javax.servlet.*;
22  import java.io.IOException;
23  
24  
25  /**
26   * This class will set the chracter encoding of each request that uses the filter.  It
27   * will use the encoding specifried in the init parameter, or if that is not present,
28   * fall back to a default value of UTF-8.
29   */
30  public class SetRequestCharacterEncoding implements Filter {
31  
32      public static final String DEFAULT_ENCODING = "UTF-8";
33  
34      private FilterConfig filterConfig = null;
35      private String encoding = null;
36  
37      /**
38       * Set the character encoding in the request.
39       *
40       * @param request     the current ServletRequest object
41       * @param response    the current ServletResponse object
42       * @param filterChain the current FilterChain
43       * @throws IOException      if any io error occurs
44       * @throws ServletException any other servlet error occurs
45       */
46      public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
47          request.setCharacterEncoding(getEncoding());
48          filterChain.doFilter(request, response);
49      }
50  
51      /**
52       * Initialize the filter.
53       *
54       * @param filterConfig the current filter configuration
55       */
56      public void init(FilterConfig filterConfig) throws ServletException {
57          this.filterConfig = filterConfig;
58          setEncoding(filterConfig.getInitParameter("encoding"));
59      }
60  
61      /**
62       * Returns the encoding of the request.
63       */
64      public String getEncoding() {
65          return (encoding == null ? DEFAULT_ENCODING : encoding);
66      }
67  
68      /**
69       * Sets the encoding of the request.
70       */
71      public void setEncoding(String value) {
72          if (value != null) {
73              encoding = value;
74          }
75      }
76  
77      /**
78       * Reset the filter settings.
79       */
80      public void destroy() {
81          encoding = null;
82          filterConfig = null;
83      }
84  
85      public FilterConfig getFilterConfig() {
86          return filterConfig;
87      }
88  
89      public void setFilterConfig(FilterConfig filterConfig) {
90          this.filterConfig = filterConfig;
91      }
92  }
93