Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
SetCharacterEncodingFilter | 56 | 18 | 0% | 10 | 7 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.container.servlet.filters.internal; | |
21 | ||
22 | import java.io.IOException; | |
23 | ||
24 | import javax.servlet.Filter; | |
25 | import javax.servlet.FilterChain; | |
26 | import javax.servlet.FilterConfig; | |
27 | import javax.servlet.ServletException; | |
28 | import javax.servlet.ServletRequest; | |
29 | import javax.servlet.ServletResponse; | |
30 | ||
31 | /** | |
32 | * <p> | |
33 | * Example filter that sets the character encoding to be used in parsing the incoming request, | |
34 | * either unconditionally or only if the client did not specify a character encoding. Configuration | |
35 | * of this filter is based on the following initialization parameters: | |
36 | * </p> | |
37 | * <ul> | |
38 | * <li><strong>encoding</strong> - The character encoding to be configured for this request, | |
39 | * either conditionally or unconditionally based on the <code>ignore</code> initialization | |
40 | * parameter. This parameter is required, so there is no default.</li> | |
41 | * <li><strong>ignore</strong> - If set to "true", any character encoding specified by the client | |
42 | * is ignored, and the value returned by the <code>selectEncoding()</code> method is set. If set | |
43 | * to "false, <code>selectEncoding()</code> is called <strong>only</strong> if the client has not | |
44 | * already specified an encoding. By default, this parameter is set to "true".</li> | |
45 | * </ul> | |
46 | * <p> | |
47 | * Although this filter can be used unchanged, it is also easy to subclass it and make the | |
48 | * <code>selectEncoding()</code> method more intelligent about what encoding to choose, based on | |
49 | * characteristics of 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 user's session. | |
51 | * </p> | |
52 | * | |
53 | * @author Craig McClanahan | |
54 | * @version $Id: af48f69d9703c895b19b4a53815f53061a81db1e $ | |
55 | */ | |
56 | public class SetCharacterEncodingFilter implements Filter | |
57 | { | |
58 | ||
59 | // ----------------------------------------------------- Instance Variables | |
60 | ||
61 | /** | |
62 | * The default character encoding to set for requests that pass through this filter. | |
63 | */ | |
64 | protected String encoding = null; | |
65 | ||
66 | /** | |
67 | * The filter configuration object we are associated with. If this value is null, this filter | |
68 | * instance is not currently configured. | |
69 | */ | |
70 | protected FilterConfig filterConfig = null; | |
71 | ||
72 | /** | |
73 | * Should a character encoding specified by the client be ignored? | |
74 | */ | |
75 | protected boolean ignore = true; | |
76 | ||
77 | // --------------------------------------------------------- Public Methods | |
78 | ||
79 | /** | |
80 | * Take this filter out of service. | |
81 | */ | |
82 | 32 | ![]() |
83 | public void destroy() | |
84 | { | |
85 | 32 | this.encoding = null; |
86 | 32 | this.filterConfig = null; |
87 | } | |
88 | ||
89 | /** | |
90 | * Select and set (if specified) the character encoding to be used to interpret request | |
91 | * parameters for this request. | |
92 | * | |
93 | * @param request The servlet request we are processing | |
94 | * @param response The servlet response we are creating | |
95 | * @param chain The filter chain we are processing | |
96 | * @exception IOException if an input/output error occurs | |
97 | * @exception ServletException if a servlet error occurs | |
98 | */ | |
99 | 12591 | ![]() |
100 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | |
101 | throws IOException, ServletException | |
102 | { | |
103 | // Conditionally select and set the character encoding to be used | |
104 | 12552 | if (ignore || (request.getCharacterEncoding() == null)) { |
105 | 11737 | String encoding = selectEncoding(request); |
106 | 11757 | if (encoding != null) { |
107 | 11747 | request.setCharacterEncoding(encoding); |
108 | } | |
109 | } | |
110 | // Pass control on to the next filter | |
111 | 12548 | chain.doFilter(request, response); |
112 | } | |
113 | ||
114 | /** | |
115 | * Place this filter into service. | |
116 | * | |
117 | * @param filterConfig The filter configuration object | |
118 | */ | |
119 | 32 | ![]() |
120 | public void init(FilterConfig filterConfig) throws ServletException | |
121 | { | |
122 | 32 | this.filterConfig = filterConfig; |
123 | 32 | this.encoding = filterConfig.getInitParameter("encoding"); |
124 | 32 | String value = filterConfig.getInitParameter("ignore"); |
125 | 32 | if (value == null) { |
126 | 0 | this.ignore = true; |
127 | 32 | } else if (value.equalsIgnoreCase("true")) { |
128 | 0 | this.ignore = true; |
129 | 32 | } else if (value.equalsIgnoreCase("yes")) { |
130 | 0 | this.ignore = true; |
131 | } else { | |
132 | 32 | this.ignore = false; |
133 | } | |
134 | ||
135 | } | |
136 | ||
137 | // ------------------------------------------------------ Protected Methods | |
138 | ||
139 | /** | |
140 | * Select an appropriate character encoding to be used, based on the characteristics of the | |
141 | * current request and/or filter initialization parameters. If no character encoding should be | |
142 | * set, return <code>null</code>. | |
143 | * <p> | |
144 | * The default implementation unconditionally returns the value configured by the | |
145 | * <strong>encoding</strong> initialization parameter for this filter. | |
146 | * | |
147 | * @param request The servlet request we are processing | |
148 | */ | |
149 | 11759 | ![]() |
150 | { | |
151 | 11766 | return (this.encoding); |
152 | } | |
153 | } |