001: /*
002: * Copyright 2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package filters;
018:
019: import java.io.IOException;
020: import javax.servlet.Filter;
021: import javax.servlet.FilterChain;
022: import javax.servlet.FilterConfig;
023: import javax.servlet.ServletException;
024: import javax.servlet.ServletRequest;
025: import javax.servlet.ServletResponse;
026: import javax.servlet.UnavailableException;
027:
028: /**
029: * <p>Example filter that sets the character encoding to be used in parsing the
030: * incoming request, either unconditionally or only if the client did not
031: * specify a character encoding. Configuration of this filter is based on
032: * the following initialization parameters:</p>
033: * <ul>
034: * <li><strong>encoding</strong> - The character encoding to be configured
035: * for this request, either conditionally or unconditionally based on
036: * the <code>ignore</code> initialization parameter. This parameter
037: * is required, so there is no default.</li>
038: * <li><strong>ignore</strong> - If set to "true", any character encoding
039: * specified by the client is ignored, and the value returned by the
040: * <code>selectEncoding()</code> method is set. If set to "false,
041: * <code>selectEncoding()</code> is called <strong>only</strong> if the
042: * client has not already specified an encoding. By default, this
043: * parameter is set to "true".</li>
044: * </ul>
045: *
046: * <p>Although this filter can be used unchanged, it is also easy to
047: * subclass it and make the <code>selectEncoding()</code> method more
048: * intelligent about what encoding to choose, based on characteristics of
049: * the incoming request (such as the values of the <code>Accept-Language</code>
050: * and <code>User-Agent</code> headers, or a value stashed in the current
051: * user's session.</p>
052: *
053: * @author Craig McClanahan
054: * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $
055: */
056:
057: public class SetCharacterEncodingFilter implements Filter {
058:
059: // ----------------------------------------------------- Instance Variables
060:
061: /**
062: * The default character encoding to set for requests that pass through
063: * this filter.
064: */
065: protected String encoding = null;
066:
067: /**
068: * The filter configuration object we are associated with. If this value
069: * is null, this filter instance is not currently configured.
070: */
071: protected FilterConfig filterConfig = null;
072:
073: /**
074: * Should a character encoding specified by the client be ignored?
075: */
076: protected boolean ignore = true;
077:
078: // --------------------------------------------------------- Public Methods
079:
080: /**
081: * Take this filter out of service.
082: */
083: public void destroy() {
084:
085: this .encoding = null;
086: this .filterConfig = null;
087:
088: }
089:
090: /**
091: * Select and set (if specified) the character encoding to be used to
092: * interpret request parameters for this request.
093: *
094: * @param request The servlet request we are processing
095: * @param result The servlet response we are creating
096: * @param chain The filter chain we are processing
097: *
098: * @exception IOException if an input/output error occurs
099: * @exception ServletException if a servlet error occurs
100: */
101: public void doFilter(ServletRequest request,
102: ServletResponse response, FilterChain chain)
103: throws IOException, ServletException {
104:
105: // Conditionally select and set the character encoding to be used
106: if (ignore || (request.getCharacterEncoding() == null)) {
107: String encoding = selectEncoding(request);
108: if (encoding != null)
109: request.setCharacterEncoding(encoding);
110: }
111:
112: // Pass control on to the next filter
113: chain.doFilter(request, response);
114:
115: }
116:
117: /**
118: * Place this filter into service.
119: *
120: * @param filterConfig The filter configuration object
121: */
122: public void init(FilterConfig filterConfig) throws ServletException {
123:
124: this .filterConfig = filterConfig;
125: this .encoding = filterConfig.getInitParameter("encoding");
126: String value = filterConfig.getInitParameter("ignore");
127: if (value == null)
128: this .ignore = true;
129: else if (value.equalsIgnoreCase("true"))
130: this .ignore = true;
131: else if (value.equalsIgnoreCase("yes"))
132: this .ignore = true;
133: else
134: this .ignore = false;
135:
136: }
137:
138: // ------------------------------------------------------ Protected Methods
139:
140: /**
141: * Select an appropriate character encoding to be used, based on the
142: * characteristics of the current request and/or filter initialization
143: * parameters. If no character encoding should be set, return
144: * <code>null</code>.
145: * <p>
146: * The default implementation unconditionally returns the value configured
147: * by the <strong>encoding</strong> initialization parameter for this
148: * filter.
149: *
150: * @param request The servlet request we are processing
151: */
152: protected String selectEncoding(ServletRequest request) {
153:
154: return (this.encoding);
155:
156: }
157:
158: }
|