KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > filters > CharEncodingFilter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. 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 JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.Locale JavaDoc;
24 import javax.servlet.Filter JavaDoc;
25 import javax.servlet.FilterChain JavaDoc;
26 import javax.servlet.FilterConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.ServletResponse JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32 import javax.servlet.jsp.jstl.core.Config;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.struts.Globals;
37
38 /**
39  * Entry point filter for all requests. This filter ensures that the request encoding is set to UTF-8 before any other
40  * processing forces request parsing using a default encoding. It also syncs up the Struts and JSTL locales. This
41  * 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
47 public class CharEncodingFilter implements Filter JavaDoc
48 {
49     private FilterConfig JavaDoc mFilterConfig = null;
50     private static Log mLogger =
51         LogFactory.getFactory().getInstance(CharEncodingFilter.class);
52
53     /**
54      * init
55      */

56     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
57     {
58         mFilterConfig = filterConfig;
59     }
60
61     /**
62      * destroy
63      */

64     public void destroy()
65     {
66     }
67
68     /**
69      * Set the character encoding and sync up Struts and JSTL locales. This filter should normally be first (and last)
70      * in the chain.
71      */

72     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc res, FilterChain JavaDoc chain)
73         throws IOException JavaDoc, ServletException JavaDoc
74     {
75         if (mLogger.isDebugEnabled()) mLogger.debug("Processing CharEncodingFilter");
76         try
77         {
78             req.setCharacterEncoding("UTF-8");
79             if (mLogger.isDebugEnabled()) mLogger.debug("Set request character encoding to UTF-8");
80         }
81         catch (UnsupportedEncodingException JavaDoc e)
82         {
83             // This should never happen since UTF-8 is a Java-specified required encoding.
84
throw new ServletException JavaDoc("Can't set incoming encoding to UTF-8");
85         }
86
87         // Keep JSTL and Struts Locale's in sync
88
// NOTE: The session here will get created if it is not present. This code was taken from its
89
// earlier incarnation in RequestFilter, which also caused the session to be created.
90
HttpSession JavaDoc session = ((HttpServletRequest JavaDoc) req).getSession();
91         if (mLogger.isDebugEnabled()) mLogger.debug("Synchronizing JSTL and Struts locales");
92         Locale JavaDoc locale = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
93         if (locale == null)
94         {
95             locale = req.getLocale();
96         }
97         if (req.getParameter("locale") != null)
98         {
99             locale = new Locale JavaDoc(req.getParameter("locale"));
100         }
101         session.setAttribute(Globals.LOCALE_KEY, locale);
102         Config.set(session, Config.FMT_LOCALE, locale);
103
104         chain.doFilter(req, res);
105     }
106
107 }
108
Popular Tags