Source code: com/xpn/xwiki/web/SetCharacterEncodingFilter.java
1 /**
2 * ===================================================================
3 *
4 * Copyright (c) 2003,2004 Ludovic Dubost, All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details, published at
15 * http://www.gnu.org/copyleft/lesser.html or in lesser.txt in the
16 * root folder of this distribution.
17
18 * Created by
19 * User: Ludovic Dubost
20 * Date: 20 févr. 2005
21 * Time: 02:53:53
22 */
23 package com.xpn.xwiki.web;
24
25 import java.io.IOException;
26 import javax.servlet.Filter;
27 import javax.servlet.FilterChain;
28 import javax.servlet.FilterConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.UnavailableException;
33
34
35 /**
36 * <p>Example filter that sets the character encoding to be used in parsing the
37 * incoming request, either unconditionally or only if the client did not
38 * specify a character encoding. Configuration of this filter is based on
39 * the following initialization parameters:</p>
40 * <ul>
41 * <li><strong>encoding</strong> - The character encoding to be configured
42 * for this request, either conditionally or unconditionally based on
43 * the <code>ignore</code> initialization parameter. This parameter
44 * is required, so there is no default.</li>
45 * <li><strong>ignore</strong> - If set to "true", any character encoding
46 * specified by the client is ignored, and the value returned by the
47 * <code>selectEncoding()</code> method is set. If set to "false,
48 * <code>selectEncoding()</code> is called <strong>only</strong> if the
49 * client has not already specified an encoding. By default, this
50 * parameter is set to "true".</li>
51 * </ul>
52 *
53 * <p>Although this filter can be used unchanged, it is also easy to
54 * subclass it and make the <code>selectEncoding()</code> method more
55 * intelligent about what encoding to choose, based on characteristics of
56 * the incoming request (such as the values of the <code>Accept-Language</code>
57 * and <code>User-Agent</code> headers, or a value stashed in the current
58 * user's session.</p>
59 *
60 * @author Craig McClanahan
61 * @version $Revision: 452 $ $Date: 2005-03-25 13:58:30 +0100 (Fri, 25 Mar 2005) $
62 */
63
64 public class SetCharacterEncodingFilter implements Filter {
65
66
67 // ----------------------------------------------------- Instance Variables
68
69
70 /**
71 * The default character encoding to set for requests that pass through
72 * this filter.
73 */
74 protected String encoding = null;
75
76
77 /**
78 * The filter configuration object we are associated with. If this value
79 * is null, this filter instance is not currently configured.
80 */
81 protected FilterConfig filterConfig = null;
82
83
84 /**
85 * Should a character encoding specified by the client be ignored?
86 */
87 protected boolean ignore = true;
88
89
90 // --------------------------------------------------------- Public Methods
91
92
93 /**
94 * Take this filter out of service.
95 */
96 public void destroy() {
97
98 this.encoding = null;
99 this.filterConfig = null;
100
101 }
102
103
104 /**
105 * Select and set (if specified) the character encoding to be used to
106 * interpret request parameters for this request.
107 *
108 * @param request The servlet request we are processing
109 * @param result The servlet response we are creating
110 * @param chain The filter chain we are processing
111 *
112 * @exception IOException if an input/output error occurs
113 * @exception ServletException if a servlet error occurs
114 */
115 public void doFilter(ServletRequest request, ServletResponse response,
116 FilterChain chain)
117 throws IOException, ServletException {
118
119 // Conditionally select and set the character encoding to be used
120 if (ignore || (request.getCharacterEncoding() == null)) {
121 String encoding = selectEncoding(request);
122 if (encoding != null)
123 request.setCharacterEncoding(encoding);
124 }
125
126 // Pass control on to the next filter
127 chain.doFilter(request, response);
128
129 }
130
131
132 /**
133 * Place this filter into service.
134 *
135 * @param filterConfig The filter configuration object
136 */
137 public void init(FilterConfig filterConfig) throws ServletException {
138
139 this.filterConfig = filterConfig;
140 this.encoding = filterConfig.getInitParameter("encoding");
141 String value = filterConfig.getInitParameter("ignore");
142 if (value == null)
143 this.ignore = true;
144 else if (value.equalsIgnoreCase("true"))
145 this.ignore = true;
146 else if (value.equalsIgnoreCase("yes"))
147 this.ignore = true;
148 else
149 this.ignore = false;
150
151 }
152
153
154 // ------------------------------------------------------ Protected Methods
155
156
157 /**
158 * Select an appropriate character encoding to be used, based on the
159 * characteristics of the current request and/or filter initialization
160 * parameters. If no character encoding should be set, return
161 * <code>null</code>.
162 * <p>
163 * The default implementation unconditionally returns the value configured
164 * by the <strong>encoding</strong> initialization parameter for this
165 * filter.
166 *
167 * @param request The servlet request we are processing
168 */
169 protected String selectEncoding(ServletRequest request) {
170
171 return (this.encoding);
172
173 }
174
175
176 }