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 java.io.IOException;
22
23 import javax.servlet.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.FilterConfig;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29
30
31 /**
32 * This class will set the chracter encoding of each request that uses the filter. It
33 * will use the encoding specifried in the init parameter, or if that is not present,
34 * fall back to a default value of UTF-8.
35 */
36 public class SetRequestCharacterEncoding implements Filter {
37
38 public static final String DEFAULT_ENCODING = "UTF-8";
39
40 private FilterConfig filterConfig = null;
41 private String encoding = null;
42
43 /**
44 * Set the character encoding in the request.
45 * @param request the current ServletRequest object
46 * @param response the current ServletResponse object
47 * @param filterChain the current FilterChain
48 * @throws IOException if any io error occurs
49 * @throws ServletException any other servlet error occurs
50 */
51 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
52 request.setCharacterEncoding(getEncoding());
53 filterChain.doFilter(request, response);
54 }
55
56 /**
57 * Initialize the filter.
58 * @param filterConfig the current filter configuration
59 */
60 public void init(FilterConfig filterConfig) throws ServletException {
61 this.filterConfig = filterConfig;
62 setEncoding(filterConfig.getInitParameter("encoding"));
63 }
64
65 /**
66 * Returns the encoding of the request.
67 */
68 public String getEncoding() {
69 return (encoding == null ? DEFAULT_ENCODING : encoding);
70 }
71
72 /**
73 * Sets the encoding of the request.
74 */
75 public void setEncoding(String value) {
76 if(value != null) {
77 encoding = value;
78 }
79 }
80
81 /**
82 * Reset the filter settings.
83 */
84 public void destroy() {
85 encoding = null;
86 filterConfig = null;
87 }
88
89 public FilterConfig getFilterConfig() {
90 return filterConfig;
91 }
92
93 public void setFilterConfig(FilterConfig filterConfig) {
94 this.filterConfig = filterConfig;
95 }
96 }
97