01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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. For additional information regarding
15: * copyright in this work, please see the NOTICE file in the top level
16: * directory of this distribution.
17: */
18:
19: package org.apache.roller.ui.core.filters;
20:
21: import java.io.IOException;
22: import java.io.UnsupportedEncodingException;
23: import java.util.Locale;
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: import javax.servlet.http.HttpServletRequest;
31: import javax.servlet.http.HttpSession;
32:
33: import org.apache.commons.logging.Log;
34: import org.apache.commons.logging.LogFactory;
35: import org.apache.struts.Globals;
36:
37: /**
38: * Entry point filter for all requests. This filter ensures that the request
39: * encoding is set to UTF-8 before any other processing forces request parsing
40: * using a default encoding. It also syncs up the Struts and JSTL locales.
41: * This filter should normally be first and last in the chain.
42: *
43: * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
44: * @web.filter name="CharEncodingFilter"
45: */
46: public class CharEncodingFilter implements Filter {
47: private FilterConfig mFilterConfig = null;
48: private static Log mLogger = LogFactory.getFactory().getInstance(
49: CharEncodingFilter.class);
50:
51: /**
52: * init
53: */
54: public void init(FilterConfig filterConfig) throws ServletException {
55: mFilterConfig = filterConfig;
56: }
57:
58: /**
59: * destroy
60: */
61: public void destroy() {
62: }
63:
64: /**
65: * Set the character encoding and sync up Struts and JSTL locales. This filter should normally be first (and last)
66: * in the chain.
67: */
68: public void doFilter(ServletRequest req, ServletResponse res,
69: FilterChain chain) throws IOException, ServletException {
70: if (mLogger.isDebugEnabled())
71: mLogger.debug("Processing CharEncodingFilter");
72: try {
73:
74: req.setCharacterEncoding("UTF-8");
75: if (mLogger.isDebugEnabled())
76: mLogger
77: .debug("Set request character encoding to UTF-8");
78:
79: } catch (UnsupportedEncodingException e) {
80: // This should never happen since UTF-8 is a Java-specified required encoding.
81: throw new ServletException(
82: "Can't set incoming encoding to UTF-8");
83: }
84:
85: chain.doFilter(req, res);
86: }
87:
88: }
|