You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by cr...@apache.org on 2001/10/18 00:52:17 UTC
cvs commit: jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/filters SetCharacterEncodingFilter.java
craigmcc 01/10/17 15:52:17
Modified: webapps/examples/WEB-INF/classes/filters Tag:
tomcat_40_branch SetCharacterEncodingFilter.java
Log:
Make it possible to configure character encoding *only* if not set by the
browser, as well as setting it unconditionally.
Revision Changes Path
No revision
No revision
1.1.2.1 +48 -17 jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java
Index: SetCharacterEncodingFilter.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java,v
retrieving revision 1.1
retrieving revision 1.1.2.1
diff -u -r1.1 -r1.1.2.1
--- SetCharacterEncodingFilter.java 2001/07/24 00:26:55 1.1
+++ SetCharacterEncodingFilter.java 2001/10/17 22:52:17 1.1.2.1
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java,v 1.1 2001/07/24 00:26:55 craigmcc Exp $
- * $Revision: 1.1 $
- * $Date: 2001/07/24 00:26:55 $
+ * $Header: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java,v 1.1.2.1 2001/10/17 22:52:17 craigmcc Exp $
+ * $Revision: 1.1.2.1 $
+ * $Date: 2001/10/17 22:52:17 $
*
* ====================================================================
*
@@ -76,17 +76,32 @@
/**
- * Example filter that unconditionally sets the character encoding to be used
- * in parsing the incoming request to a value specified by the
- * <strong>encoding</string> filter initialization parameter in the web app
- * deployment descriptor (</code>/WEB-INF/web.xml</code>). This filter could
- * easily be extended to be more intelligent about what character encoding to
- * set, based on characteristics of the incoming request (such as the values
- * of the <code>Accept-Language</code> and <code>User-Agent</code> headers,
- * or a value stashed in the current user's session).
+ * <p>Example filter that sets the character encoding to be used in parsing the
+ * incoming request, either unconditionally or only if the client did not
+ * specify a character encoding. Configuration of this filter is based on
+ * the following initialization parameters:</p>
+ * <ul>
+ * <li><strong>encoding</strong> - The character encoding to be configured
+ * for this request, either conditionally or unconditionally based on
+ * the <code>ignore</code> initialization parameter. This parameter
+ * is required, so there is no default.</li>
+ * <li><strong>ignore</strong> - If set to "true", any character encoding
+ * specified by the client is ignored, and the value returned by the
+ * <code>selectEncoding()</code> method is set. If set to "false,
+ * <code>selectEncoding()</code> is called <strong>only</strong> if the
+ * client has not already specified an encoding. By default, this
+ * parameter is set to "true".</li>
+ * </ul>
+ *
+ * <p>Although this filter can be used unchanged, it is also easy to
+ * subclass it and make the <code>selectEncoding()</code> method more
+ * intelligent about what encoding to choose, based on characteristics of
+ * the incoming request (such as the values of the <code>Accept-Language</code>
+ * and <code>User-Agent</code> headers, or a value stashed in the current
+ * user's session.</p>
*
* @author Craig McClanahan
- * @version $Revision: 1.1 $ $Date: 2001/07/24 00:26:55 $
+ * @version $Revision: 1.1.2.1 $ $Date: 2001/10/17 22:52:17 $
*/
public class SetCharacterEncodingFilter implements Filter {
@@ -109,6 +124,12 @@
protected FilterConfig filterConfig = null;
+ /**
+ * Should a character encoding specified by the client be ignored?
+ */
+ protected boolean ignore = true;
+
+
// --------------------------------------------------------- Public Methods
@@ -138,10 +159,12 @@
FilterChain chain)
throws IOException, ServletException {
- // Select and set (if needed) the character encoding to be used
- String encoding = selectEncoding(request);
- if (encoding != null)
- request.setCharacterEncoding(encoding);
+ // Conditionally select and set the character encoding to be used
+ if (ignore || (request.getCharacterEncoding() == null)) {
+ String encoding = selectEncoding(request);
+ if (encoding != null)
+ request.setCharacterEncoding(encoding);
+ }
// Pass control on to the next filter
chain.doFilter(request, response);
@@ -158,6 +181,15 @@
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
+ String value = filterConfig.getInitParameter("ignore");
+ if (value == null)
+ this.ignore = true;
+ else if (value.equalsIgnoreCase("true"))
+ this.ignore = true;
+ else if (value.equalsIgnoreCase("yes"))
+ this.ignore = true;
+ else
+ this.ignore = false;
}
@@ -185,4 +217,3 @@
}
-