001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.filter;
025:
026: import java.io.IOException;
027:
028: import javax.servlet.FilterChain;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.http.HttpServletResponseWrapper;
033:
034: import org.springframework.web.filter.OncePerRequestFilter;
035:
036: /**
037: * Servlet filter that allows to specify a default character encoding for
038: * requests and responses.
039: *
040: * If don't want to modify the response encoding use the {@link
041: * org.springframework.web.filter.CharacterEncodingFilter
042: * CharacterEncodingFilter} provided by Spring.
043: */
044: public class CharacterEncodingFilter extends OncePerRequestFilter {
045:
046: private String encoding;
047:
048: private boolean forceRequestEncoding;
049:
050: private boolean forceResponseEncoding;
051:
052: /**
053: * Set the encoding to use for requests. This encoding will be passed into a
054: * ServletRequest.setCharacterEncoding call.
055: * <p>
056: * Whether this encoding will override existing request encodings depends on
057: * the "forceEncoding" flag.
058: *
059: * @see javax.servlet.ServletRequest#setCharacterEncoding
060: */
061: public void setEncoding(String encoding) {
062: this .encoding = encoding;
063: }
064:
065: /**
066: * Set whether the encoding of this filter should override existing request
067: * encodings. Default is false, i.e. do not modify encoding if
068: * ServletRequest.getCharacterEncoding returns a non-null value.
069: *
070: * @see #setEncoding
071: * @see javax.servlet.ServletRequest#getCharacterEncoding
072: */
073: public void setForceRequestEncoding(boolean forceEncoding) {
074: this .forceRequestEncoding = forceEncoding;
075: }
076:
077: /**
078: * Set whether the encoding of this filter should override existing response
079: * encodings.
080: *
081: * @see #setEncoding
082: * @see javax.servlet.ServletRequest#getCharacterEncoding
083: */
084: public void setForceResponseEncoding(boolean forceEncoding) {
085: this .forceResponseEncoding = forceEncoding;
086: }
087:
088: protected void doFilterInternal(HttpServletRequest request,
089: HttpServletResponse response, FilterChain filterChain)
090: throws ServletException, IOException {
091:
092: if (this .forceRequestEncoding
093: || request.getCharacterEncoding() == null) {
094: request.setCharacterEncoding(this .encoding);
095: }
096: filterChain.doFilter(request, new EncodingResponseWrapper(
097: response));
098: }
099:
100: private class EncodingResponseWrapper extends
101: HttpServletResponseWrapper {
102:
103: private boolean encodingSpecified;
104:
105: public EncodingResponseWrapper(HttpServletResponse response) {
106: super (response);
107: }
108:
109: public void setCharacterEncoding(String encoding) {
110: if (forceResponseEncoding) {
111: super .setCharacterEncoding(encoding);
112: } else {
113: super .setCharacterEncoding(encoding);
114: }
115: encodingSpecified = true;
116: }
117:
118: public void setContentType(String type) {
119: String mimeType = null;
120: String charset = null;
121: int i = type.indexOf(';');
122: if (i != -1) {
123: mimeType = type.substring(0, i).trim().toLowerCase();
124: i = type.indexOf('=', i);
125: charset = type.substring(i + 1).trim();
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 explicitType = mimeType;
137: if (charset != null) {
138: explicitType += "; charset=" + charset;
139: encodingSpecified = true;
140: }
141: super.setContentType(explicitType);
142: }
143:
144: }
145: }
|