KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > SetCharacterEncodingFilter


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util;
25
26 import java.io.IOException JavaDoc;
27
28 import javax.servlet.Filter JavaDoc;
29 import javax.servlet.FilterChain JavaDoc;
30 import javax.servlet.FilterConfig JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.ServletRequest JavaDoc;
33 import javax.servlet.ServletResponse JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35
36 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
37 import org.infoglue.cms.entities.management.LanguageVO;
38
39
40 public class SetCharacterEncodingFilter implements Filter JavaDoc
41 {
42
43
44     /**
45      * The default character encoding to set for requests that pass through
46      * this filter.
47      */

48     protected String JavaDoc encoding = null;
49
50
51     /**
52      * The filter configuration object we are associated with. If this value
53      * is null, this filter instance is not currently configured.
54      */

55     protected FilterConfig JavaDoc filterConfig = null;
56
57
58     /**
59      * Should a character encoding specified by the client be ignored?
60      */

61     protected boolean ignore = true;
62
63
64
65     /**
66      * Take this filter out of service.
67      */

68     public void destroy() {
69
70         this.encoding = null;
71         this.filterConfig = null;
72
73     }
74
75
76     /**
77      * Select and set (if specified) the character encoding to be used to
78      * interpret request parameters for this request.
79      *
80      * @param request The servlet request we are processing
81      * @param result The servlet response we are creating
82      * @param chain The filter chain we are processing
83      *
84      * @exception IOException if an input/output error occurs
85      * @exception ServletException if a servlet error occurs
86      */

87     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc
88     {
89         // Conditionally select and set the character encoding to be used
90
String JavaDoc referer = ((HttpServletRequest JavaDoc)request).getHeader("referer");
91         if(referer != null && referer.length() > 0 && referer.indexOf("ViewPage!renderDecoratedPage.action") > -1)
92         {
93             try
94             {
95                 int startIndex = referer.indexOf("&languageId=");
96                 if(startIndex > -1)
97                 {
98                     int endIndex = referer.indexOf("&", startIndex + 12);
99                     String JavaDoc languageId = referer.substring(startIndex + 12, endIndex);
100                     //System.out.println("languageId:" + languageId);
101
LanguageVO languageVO = LanguageController.getController().getLanguageVOWithId(new Integer JavaDoc(languageId));
102                     request.setCharacterEncoding(languageVO.getCharset());
103                 }
104             }
105             catch(Exception JavaDoc e)
106             {
107                 e.printStackTrace();
108             }
109         }
110         else if (ignore || (request.getCharacterEncoding() == null))
111         {
112             String JavaDoc encoding = selectEncoding(request);
113             if (encoding != null)
114                 request.setCharacterEncoding(encoding);
115         }
116
117         // Pass control on to the next filter
118
chain.doFilter(request, response);
119
120     }
121
122
123     /**
124      * Place this filter into service.
125      *
126      * @param filterConfig The filter configuration object
127      */

128     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
129     {
130
131         this.filterConfig = filterConfig;
132         this.encoding = filterConfig.getInitParameter("encoding");
133         String JavaDoc value = filterConfig.getInitParameter("ignore");
134         if (value == null)
135             this.ignore = true;
136         else if (value.equalsIgnoreCase("true"))
137             this.ignore = true;
138         else if (value.equalsIgnoreCase("yes"))
139             this.ignore = true;
140         else
141             this.ignore = false;
142
143     }
144
145
146     /**
147      * Select an appropriate character encoding to be used, based on the
148      * characteristics of the current request and/or filter initialization
149      * parameters. If no character encoding should be set, return
150      * <code>null</code>.
151      * <p>
152      * The default implementation unconditionally returns the value configured
153      * by the <strong>encoding</strong> initialization parameter for this
154      * filter.
155      *
156      * @param request The servlet request we are processing
157      */

158     protected String JavaDoc selectEncoding(ServletRequest JavaDoc request)
159     {
160         String JavaDoc inputCharacterEncoding = CmsPropertyHandler.getInputCharacterEncoding(this.encoding);
161         
162         if(inputCharacterEncoding != null && !inputCharacterEncoding.equals("") && !inputCharacterEncoding.equalsIgnoreCase("@inputCharacterEncoding@"))
163             return inputCharacterEncoding;
164         else
165             return (this.encoding);
166     }
167
168
169 }
170
Popular Tags