1 /*
2 * Copyright 2004 The Apache Software Foundation
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 org.apache.servlet;
18
19 import java.io.IOException;
20
21 import javax.servlet.Filter;
22 import javax.servlet.FilterChain;
23 import javax.servlet.FilterConfig;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27
28 /**
29 * <p>Example filter that sets the character encoding to be used in parsing the
30 * incoming request, either unconditionally or only if the client did not
31 * specify a character encoding. Configuration of this filter is based on
32 * the following initialization parameters:</p>
33 * <ul>
34 * <li><strong>encoding</strong> - The character encoding to be configured
35 * for this request, either conditionally or unconditionally based on
36 * the <code>ignore</code> initialization parameter. This parameter
37 * is required, so there is no default.</li>
38 * <li><strong>ignore</strong> - If set to "true", any character encoding
39 * specified by the client is ignored, and the value returned by the
40 * <code>selectEncoding()</code> method is set. If set to "false,
41 * <code>selectEncoding()</code> is called <strong>only</strong> if the
42 * client has not already specified an encoding. By default, this
43 * parameter is set to "true".</li>
44 * </ul>
45 *
46 * <p>Although this filter can be used unchanged, it is also easy to
47 * subclass it and make the <code>selectEncoding()</code> method more
48 * intelligent about what encoding to choose, based on characteristics of
49 * the incoming request (such as the values of the <code>Accept-Language</code>
50 * and <code>User-Agent</code> headers, or a value stashed in the current
51 * user's session.</p>
52 *
53 * @author Craig McClanahan
54 * @version $Revision: 267129 $ $Date: 2004-03-18 11:40:35 -0500 (Thu, 18 Mar 2004) $
55 */
56
57 public class SetCharacterEncodingFilter implements Filter {
58
59 // ----------------------------------------------------- Instance Variables
60
61 /**
62 * The default character encoding to set for requests that pass through
63 * this filter.
64 */
65 protected String encoding = null;
66
67 /**
68 * The filter configuration object we are associated with. If this value
69 * is null, this filter instance is not currently configured.
70 */
71 protected FilterConfig filterConfig = null;
72
73 /**
74 * Should a character encoding specified by the client be ignored?
75 */
76 protected boolean ignore = true;
77
78 // --------------------------------------------------------- Public Methods
79
80 /**
81 * Take this filter out of service.
82 */
83 public void destroy() {
84
85 this.encoding = null;
86 this.filterConfig = null;
87
88 }
89
90 /**
91 * Select and set (if specified) the character encoding to be used to
92 * interpret request parameters for this request.
93 *
94 * @param request The servlet request we are processing
95 * @param result The servlet response we are creating
96 * @param chain The filter chain we are processing
97 *
98 * @exception IOException if an input/output error occurs
99 * @exception ServletException if a servlet error occurs
100 */
101 public void doFilter(ServletRequest request, ServletResponse response,
102 FilterChain chain)
103 throws IOException, ServletException {
104
105 // Conditionally select and set the character encoding to be used
106 if (ignore || (request.getCharacterEncoding() == null)) {
107 String encoding = selectEncoding(request);
108 if (encoding != null)
109 request.setCharacterEncoding(encoding);
110 }
111
112 // Pass control on to the next filter
113 chain.doFilter(request, response);
114
115 }
116
117 /**
118 * Place this filter into service.
119 *
120 * @param filterConfig The filter configuration object
121 */
122 public void init(FilterConfig filterConfig) throws ServletException {
123
124 this.filterConfig = filterConfig;
125 this.encoding = filterConfig.getInitParameter("encoding");
126 String value = filterConfig.getInitParameter("ignore");
127 if (value == null)
128 this.ignore = true;
129 else if (value.equalsIgnoreCase("true"))
130 this.ignore = true;
131 else if (value.equalsIgnoreCase("yes"))
132 this.ignore = true;
133 else
134 this.ignore = false;
135
136 }
137
138 // ------------------------------------------------------ Protected Methods
139
140 /**
141 * Select an appropriate character encoding to be used, based on the
142 * characteristics of the current request and/or filter initialization
143 * parameters. If no character encoding should be set, return
144 * <code>null</code>.
145 * <p>
146 * The default implementation unconditionally returns the value configured
147 * by the <strong>encoding</strong> initialization parameter for this
148 * filter.
149 *
150 * @param request The servlet request we are processing
151 */
152 protected String selectEncoding(ServletRequest request) {
153
154 return (this.encoding);
155
156 }
157 }