KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > filter > CharacterEncodingFilter


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.filter;
25
26 import java.io.IOException JavaDoc;
27
28 import javax.servlet.FilterChain JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
33
34 import org.springframework.web.filter.OncePerRequestFilter;
35
36 /**
37  * Servlet filter that allows to specify a default character encoding for
38  * requests and responses.
39  *
40  * If don't want to modify the response encoding use the {@link
41  * org.springframework.web.filter.CharacterEncodingFilter
42  * CharacterEncodingFilter} provided by Spring.
43  */

44 public class CharacterEncodingFilter extends OncePerRequestFilter {
45
46     private String JavaDoc encoding;
47
48     private boolean forceRequestEncoding;
49     
50     private boolean forceResponseEncoding;
51
52     /**
53      * Set the encoding to use for requests. This encoding will be passed into a
54      * ServletRequest.setCharacterEncoding call.
55      * <p>
56      * Whether this encoding will override existing request encodings depends on
57      * the "forceEncoding" flag.
58      *
59      * @see javax.servlet.ServletRequest#setCharacterEncoding
60      */

61     public void setEncoding(String JavaDoc encoding) {
62         this.encoding = encoding;
63     }
64
65     /**
66      * Set whether the encoding of this filter should override existing request
67      * encodings. Default is false, i.e. do not modify encoding if
68      * ServletRequest.getCharacterEncoding returns a non-null value.
69      *
70      * @see #setEncoding
71      * @see javax.servlet.ServletRequest#getCharacterEncoding
72      */

73     public void setForceRequestEncoding(boolean forceEncoding) {
74         this.forceRequestEncoding = forceEncoding;
75     }
76     
77     /**
78      * Set whether the encoding of this filter should override existing response
79      * encodings.
80      *
81      * @see #setEncoding
82      * @see javax.servlet.ServletRequest#getCharacterEncoding
83      */

84     public void setForceResponseEncoding(boolean forceEncoding) {
85         this.forceResponseEncoding = forceEncoding;
86     }
87
88     protected void doFilterInternal(HttpServletRequest JavaDoc request,
89             HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
90             throws ServletException JavaDoc, IOException JavaDoc {
91
92         if (this.forceRequestEncoding || request.getCharacterEncoding() == null) {
93             request.setCharacterEncoding(this.encoding);
94         }
95         filterChain.doFilter(request, new EncodingResponseWrapper(response));
96     }
97
98     private class EncodingResponseWrapper extends
99             HttpServletResponseWrapper JavaDoc {
100
101         private boolean encodingSpecified;
102
103         public EncodingResponseWrapper(HttpServletResponse JavaDoc response) {
104             super(response);
105         }
106
107         public void setCharacterEncoding(String JavaDoc encoding) {
108             if (forceResponseEncoding) {
109                 super.setCharacterEncoding(encoding);
110             }
111             else {
112                 super.setCharacterEncoding(encoding);
113             }
114             encodingSpecified = true;
115         }
116         
117         public void setContentType(String JavaDoc type) {
118             String JavaDoc mimeType = null;
119             String JavaDoc charset = null;
120             int i = type.indexOf(';');
121             if (i != -1) {
122                 mimeType = type.substring(0, i).trim().toLowerCase();
123                 i = type.indexOf('=', i);
124                 charset = type.substring(i + 1).trim();
125             }
126             else {
127                 mimeType = type.trim().toLowerCase();
128             }
129             
130             if (mimeType.startsWith("text/") &&
131                     (forceResponseEncoding || (charset == null && !encodingSpecified))) {
132                 
133                 charset = encoding;
134             }
135             
136             String JavaDoc explicitType = mimeType;
137             if (charset != null) {
138                 explicitType += "; charset=" + charset;
139                 encodingSpecified = true;
140             }
141             super.setContentType(explicitType);
142         }
143
144     }
145 }
146
Popular Tags