KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > SetCharacterEncodingFilter


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  * [Additional notices, if required by prior licensing conditions]
57  *
58  */

59
60
61 package com.sslexplorer.core;
62
63
64 import java.io.IOException JavaDoc;
65
66 import javax.servlet.Filter JavaDoc;
67 import javax.servlet.FilterChain JavaDoc;
68 import javax.servlet.FilterConfig JavaDoc;
69 import javax.servlet.ServletException JavaDoc;
70 import javax.servlet.ServletRequest JavaDoc;
71 import javax.servlet.ServletResponse JavaDoc;
72
73
74 //
75
// Downloaded from http://www.anassina.com/struts/i18n/i18n.html
76
// http://www.anassina.com/struts/i18n/SetCharacterEncodingFilter.java
77
//
78

79 /**
80  * Example filter that unconditionally sets the character encoding to be used
81  * in parsing the incoming request to a value specified by the
82  * <strong>encoding</string> filter initialization parameter in the web app
83  * deployment descriptor (</code>/WEB-INF/web.xml</code>). This filter could
84  * easily be extended to be more intelligent about what character encoding to
85  * set, based on characteristics of the incoming request (such as the values
86  * of the <code>Accept-Language</code> and <code>User-Agent</code> headers,
87  * or a value stashed in the current user's session).
88  *
89  * @author Craig McClanahan
90  */

91
92 public class SetCharacterEncodingFilter implements Filter JavaDoc {
93
94
95     // ----------------------------------------------------- Instance Variables
96

97
98     /**
99      * The default character encoding to set for requests that pass through
100      * this filter.
101      */

102     protected String JavaDoc encoding = null;
103
104
105     /**
106      * The filter configuration object we are associated with. If this value
107      * is null, this filter instance is not currently configured.
108      */

109     protected FilterConfig JavaDoc filterConfig = null;
110
111
112     // --------------------------------------------------------- Public Methods
113

114
115     /**
116      * Take this filter out of service.
117      */

118     public void destroy() {
119
120         this.encoding = null;
121         this.filterConfig = null;
122
123     }
124
125
126     /**
127      * Select and set (if specified) the character encoding to be used to
128      * interpret request parameters for this request.
129      *
130      * @param request The servlet request we are processing
131      * @param result The servlet response we are creating
132      * @param chain The filter chain we are processing
133      *
134      * @exception IOException if an input/output error occurs
135      * @exception ServletException if a servlet error occurs
136      */

137     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
138                          FilterChain JavaDoc chain)
139     throws IOException JavaDoc, ServletException JavaDoc {
140
141         // Select and set (if needed) the character encoding to be used
142
String JavaDoc encoding = selectEncoding(request);
143         if (encoding != null)
144             request.setCharacterEncoding(encoding);
145
146     // Pass control on to the next filter
147
chain.doFilter(request, response);
148
149     }
150
151
152     /**
153      * Place this filter into service.
154      *
155      * @param filterConfig The filter configuration object
156      */

157     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
158
159     this.filterConfig = filterConfig;
160         this.encoding = filterConfig.getInitParameter("encoding");
161
162     }
163
164
165     // ------------------------------------------------------ Protected Methods
166

167
168     /**
169      * Select an appropriate character encoding to be used, based on the
170      * characteristics of the current request and/or filter initialization
171      * parameters. If no character encoding should be set, return
172      * <code>null</code>.
173      * <p>
174      * The default implementation unconditionally returns the value configured
175      * by the <strong>encoding</strong> initialization parameter for this
176      * filter.
177      *
178      * @param request The servlet request we are processing
179      */

180     protected String JavaDoc selectEncoding(ServletRequest JavaDoc request) {
181
182         return (this.encoding);
183
184     }
185
186
187 }
188
189
Popular Tags