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 org.pentaho.ui.servlet;
018:
019: import java.io.IOException;
020:
021: import javax.servlet.Filter;
022: import javax.servlet.FilterChain;
023: import javax.servlet.FilterConfig;
024: import javax.servlet.ServletException;
025: import javax.servlet.ServletRequest;
026: import javax.servlet.ServletResponse;
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: 267129 $ $Date: 2004-03-18 11:40:35 -0500 (Thu, 18 Mar 2004) $
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 localEncoding = selectEncoding(request);
108: if (localEncoding != null)
109: request.setCharacterEncoding(localEncoding);
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 localFilterConfig The filter configuration object
121: */
122: public void init(FilterConfig localFilterConfig)
123: throws ServletException {
124:
125: this .filterConfig = localFilterConfig;
126: this .encoding = filterConfig.getInitParameter("encoding"); //$NON-NLS-1$
127: String value = filterConfig.getInitParameter("ignore"); //$NON-NLS-1$
128: if (value == null)
129: this .ignore = true;
130: else if (value.equalsIgnoreCase("true")) //$NON-NLS-1$
131: this .ignore = true;
132: else if (value.equalsIgnoreCase("yes")) //$NON-NLS-1$
133: this .ignore = true;
134: else
135: this .ignore = false;
136:
137: }
138:
139: // ------------------------------------------------------ Protected Methods
140:
141: /**
142: * Select an appropriate character encoding to be used, based on the
143: * characteristics of the current request and/or filter initialization
144: * parameters. If no character encoding should be set, return
145: * <code>null</code>.
146: * <p>
147: * The default implementation unconditionally returns the value configured
148: * by the <strong>encoding</strong> initialization parameter for this
149: * filter.
150: *
151: * @param request The servlet request we are processing
152: */
153: protected String selectEncoding(ServletRequest request) {
154:
155: return (this.encoding);
156:
157: }
158:
159: }
|