You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by kk...@apache.org on 2012/03/21 00:44:36 UTC

svn commit: r1303215 - in /tomcat/tc6.0.x/trunk: STATUS.txt conf/web.xml java/org/apache/catalina/filters/SetCharacterEncodingFilter.java webapps/docs/changelog.xml webapps/docs/config/filter.xml

Author: kkolinko
Date: Tue Mar 20 23:44:35 2012
New Revision: 1303215

URL: http://svn.apache.org/viewvc?rev=1303215&view=rev
Log:
Add SetCharacterEncodingFilter (similar to the one contained in the examples web application)
to the org.apache.catalina.filters package so that it is available for all web applications.

Added:
    tomcat/tc6.0.x/trunk/java/org/apache/catalina/filters/SetCharacterEncodingFilter.java   (with props)
Modified:
    tomcat/tc6.0.x/trunk/STATUS.txt
    tomcat/tc6.0.x/trunk/conf/web.xml
    tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
    tomcat/tc6.0.x/trunk/webapps/docs/config/filter.xml

Modified: tomcat/tc6.0.x/trunk/STATUS.txt
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/STATUS.txt?rev=1303215&r1=1303214&r2=1303215&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/STATUS.txt (original)
+++ tomcat/tc6.0.x/trunk/STATUS.txt Tue Mar 20 23:44:35 2012
@@ -61,16 +61,6 @@ PATCHES PROPOSED TO BACKPORT:
   +1: kkolinko
   -1:
 
-* Backport SetCharacterEncodingFilter
-  1) patch
-   http://people.apache.org/~kkolinko/patches/2011-12-22_tc6_SetCharacterEncodingFilter.patch
-  2)
-   svn propset svn:eol-style native java/org/apache/catalina/filters/SetCharacterEncodingFilter.java
-  3) Add example of configuration to conf/web.xml:
-   http://svn.apache.org/viewvc?rev=1228210&view=rev
-  +1: kkolinko, rjung, kfujino
-  -1:
-
 * Improvements to Windows installer:
   1. When building a Windows installer do not copy whole "res" folder to
      output/dist, but only the files that we need. Make sure that

Modified: tomcat/tc6.0.x/trunk/conf/web.xml
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/conf/web.xml?rev=1303215&r1=1303214&r2=1303215&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/conf/web.xml (original)
+++ tomcat/tc6.0.x/trunk/conf/web.xml Tue Mar 20 23:44:35 2012
@@ -421,6 +421,21 @@
 
   <!-- ================== Built In Filter Definitions ===================== -->
 
+
+  <!-- A filter that sets character encoding that is used to decode -->
+  <!-- parameters in a POST request -->
+<!--
+    <filter>
+        <filter-name>setCharacterEncodingFilter</filter-name>
+        <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
+        <init-param>
+            <param-name>encoding</param-name>
+            <param-value>UTF-8</param-value>
+        </init-param>
+    </filter>
+-->
+
+
   <!-- A filter that triggers request parameters parsing and rejects the    -->
   <!-- request if some parameters were skipped because of parsing errors or -->
   <!-- request size limitations.                                            -->
@@ -494,6 +509,14 @@
 
   <!-- ==================== Built In Filter Mappings ====================== -->
 
+  <!-- The mapping for the Set Character Encoding Filter -->
+<!--
+    <filter-mapping>
+        <filter-name>setCharacterEncodingFilter</filter-name>
+        <url-pattern>/*</url-pattern>
+    </filter-mapping>
+-->
+
   <!-- The mapping for the Failed Request Filter -->
 <!--
     <filter-mapping>

Added: tomcat/tc6.0.x/trunk/java/org/apache/catalina/filters/SetCharacterEncodingFilter.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/catalina/filters/SetCharacterEncodingFilter.java?rev=1303215&view=auto
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/catalina/filters/SetCharacterEncodingFilter.java (added)
+++ tomcat/tc6.0.x/trunk/java/org/apache/catalina/filters/SetCharacterEncodingFilter.java Tue Mar 20 23:44:35 2012
@@ -0,0 +1,134 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.catalina.filters;
+
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+
+
+/**
+ * <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 "false".</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>
+ */
+public class SetCharacterEncodingFilter extends FilterBase {
+
+    private static final Log log =
+        LogFactory.getLog(SetCharacterEncodingFilter.class);
+
+
+    // ----------------------------------------------------- Instance Variables
+
+    /**
+     * The default character encoding to set for requests that pass through
+     * this filter.
+     */
+    private String encoding = null;
+    public void setEncoding(String encoding) { this.encoding = encoding; }
+    public String getEncoding() { return encoding; }
+
+
+    /**
+     * Should a character encoding specified by the client be ignored?
+     */
+    private boolean ignore = false;
+    public void setIgnore(boolean ignore) { this.ignore = ignore; }
+    public boolean isIgnore() { return ignore; }
+
+
+    // --------------------------------------------------------- Public Methods
+
+
+    /**
+     * Select and set (if specified) the character encoding to be used to
+     * interpret request parameters for this request.
+     *
+     * @param request The servlet request we are processing
+     * @param response The servlet response we are creating
+     * @param chain The filter chain we are processing
+     *
+     * @exception IOException if an input/output error occurs
+     * @exception ServletException if a servlet error occurs
+     */
+    public void doFilter(ServletRequest request, ServletResponse response,
+                         FilterChain chain)
+        throws IOException, ServletException {
+
+        // Conditionally select and set the character encoding to be used
+        if (ignore || (request.getCharacterEncoding() == null)) {
+            String characterEncoding = selectEncoding(request);
+            if (characterEncoding != null) {
+                request.setCharacterEncoding(characterEncoding);
+            }
+        }
+
+        // Pass control on to the next filter
+        chain.doFilter(request, response);
+    }
+
+
+    // ------------------------------------------------------ Protected Methods
+
+    @Override
+    protected Log getLogger() {
+        return log;
+    }
+
+
+    /**
+     * Select an appropriate character encoding to be used, based on the
+     * characteristics of the current request and/or filter initialization
+     * parameters.  If no character encoding should be set, return
+     * <code>null</code>.
+     * <p>
+     * The default implementation unconditionally returns the value configured
+     * by the <strong>encoding</strong> initialization parameter for this
+     * filter.
+     *
+     * @param request The servlet request we are processing
+     */
+    protected String selectEncoding(ServletRequest request) {
+        return this.encoding;
+    }
+}

Propchange: tomcat/tc6.0.x/trunk/java/org/apache/catalina/filters/SetCharacterEncodingFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml?rev=1303215&r1=1303214&r2=1303215&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Tue Mar 20 23:44:35 2012
@@ -113,6 +113,12 @@
         It allows to use different HTTP response code when rejecting denied
         request. E.g. 404 instead of 403. (kkolinko)
       </add>
+      <add>
+        Add <code>SetCharacterEncodingFilter</code> (similar to the one
+        contained in the examples web application) to the
+        <code>org.apache.catalina.filters</code> package so that it is
+        available for all web applications. (kkolinko)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">

Modified: tomcat/tc6.0.x/trunk/webapps/docs/config/filter.xml
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/config/filter.xml?rev=1303215&r1=1303214&r2=1303215&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/config/filter.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/config/filter.xml Tue Mar 20 23:44:35 2012
@@ -112,6 +112,58 @@
 </section>
 
 
+<section name="Set Character Encoding Filter">
+
+  <subsection name="Introduction">
+
+    <p>User agents don&apos;t always include character encoding information in
+    requests. Depending on the how the request is processed, usually the
+    default encoding of ISO-8859-1 is used. This is not always
+    desirable. This filter provides options for setting that encoding or
+    forcing it to a particular value. Essentially this filter calls
+    <code>ServletRequest.setCharacterEncoding()</code> method.</p>
+
+    <p>Effectively the value set by this filter is used when parsing parameters
+    in a POST request, if parameter parsing occurs later than this filter. Thus
+    the order of filter mappings is important. Note that the encoding for GET
+    requests is not set here, but on a <strong>Connector</strong>. See
+    CharacterEncoding page in the FAQ for details.</p>
+
+  </subsection>
+
+  <subsection name="Filter Class Name">
+
+    <p>The filter class name for the Set Character Encoding Filter is
+    <strong><code>org.apache.catalina.filters.SetCharacterEncodingFilter</code></strong>.</p>
+
+  </subsection>
+
+  <subsection name="Initialisation parameters">
+
+    <p>The Set Character Encoding Filter supports the following initialization
+    parameters:</p>
+
+    <attributes>
+
+      <attribute name="encoding" required="true">
+        <p>Name of the character encoding which should be set.</p>
+      </attribute>
+
+      <attribute name="ignore" required="false">
+        <p>Determines if any character encoding specified by the user agent is
+        ignored. If this attribute is <code>true</code>, any value provided by
+        the user agent is ignored. If <code>false</code>, the encoding is only
+        set if the user agent did not specify an encoding. The default value
+        is <code>false</code>.</p>
+      </attribute>
+
+    </attributes>
+
+  </subsection>
+
+</section>
+
+
 <section name="Failed Request Filter">
 
   <subsection name="Introduction">



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org