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...@locus.apache.org on 2000/09/27 04:13:07 UTC

cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util CharsetMapper.java LocaleToCharsetMap.java

craigmcc    00/09/26 19:13:07

  Modified:    catalina/src/share/org/apache/catalina Context.java
               catalina/src/share/org/apache/catalina/connector
                        ResponseBase.java
               catalina/src/share/org/apache/catalina/core
                        StandardContext.java WrappedRequest.java
               catalina/src/share/org/apache/catalina/startup
                        LocalStrings.properties
  Added:       catalina/src/share/org/apache/catalina/util
                        CharsetMapper.java
  Removed:     catalina/src/share/org/apache/catalina/util
                        LocaleToCharsetMap.java
  Log:
  Implement some I18N improvements for Tomcat 4.0:
  
  * Replace the previous hard-coded mapping between Locales and character
    sets with a class that loads its mapping information from a properties
    resource that can be replaced if needed.
  
  * Make the CharsetMapper pluggable on a per-Context (i.e. per-webapp) basis
    rather than being global to the entire server.  You can choose the
    implementation class of the mapper to be used (if you want to change the
    algorithm, not just the mapping data) by setting the
    "charsetMapperClass" attribute in the <Context> element in server.xml.
    The default class is org.apache.catalina.util.CharsetMapper, and your
    custom class (if any) must subclass this and override getCharset().
  
  * Fix an I18N bug in WrappedRequest -- it was ignoring setCharacterEncoding().
    NOTE:  There are other FIXME issues with this class, which is used when
    executing RequestDispatcher.include() and RequestDispatcher.forward() calls,
    which will be fixed soon.
  
  Submitted by: Eugen Kuleshov <eu...@hotmail.com>
  
  Revision  Changes    Path
  1.7       +19 -4     jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/Context.java
  
  Index: Context.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/Context.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Context.java	2000/09/24 02:52:58	1.6
  +++ Context.java	2000/09/27 02:12:59	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/Context.java,v 1.6 2000/09/24 02:52:58 craigmcc Exp $
  - * $Revision: 1.6 $
  - * $Date: 2000/09/24 02:52:58 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/Context.java,v 1.7 2000/09/27 02:12:59 craigmcc Exp $
  + * $Revision: 1.7 $
  + * $Date: 2000/09/27 02:12:59 $
    *
    * ====================================================================
    *
  @@ -75,6 +75,7 @@
   import org.apache.catalina.deploy.FilterMap;
   import org.apache.catalina.deploy.LoginConfig;
   import org.apache.catalina.deploy.SecurityConstraint;
  +import org.apache.catalina.util.CharsetMapper;
   
   
   /**
  @@ -94,7 +95,7 @@
    * <p>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.6 $ $Date: 2000/09/24 02:52:58 $
  + * @version $Revision: 1.7 $ $Date: 2000/09/27 02:12:59 $
    */
   
   public interface Context extends Container {
  @@ -122,6 +123,20 @@
        * @param listeners The set of instantiated listener objects.
        */
       public void setApplicationListeners(Object listeners[]);
  +
  +
  +    /**
  +     * Return the Locale to character set mapper for this Context.
  +     */
  +    public CharsetMapper getCharsetMapper();
  +
  +
  +    /**
  +     * Set the Locale to character set mapper for this Context.
  +     *
  +     * @param mapper The new mapper
  +     */
  +    public void setCharsetMapper(CharsetMapper mapper);
   
   
       /**
  
  
  
  1.3       +9 -8      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseBase.java
  
  Index: ResponseBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseBase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResponseBase.java	2000/08/19 20:19:26	1.2
  +++ ResponseBase.java	2000/09/27 02:13:00	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseBase.java,v 1.2 2000/08/19 20:19:26 craigmcc Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/08/19 20:19:26 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseBase.java,v 1.3 2000/09/27 02:13:00 craigmcc Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/09/27 02:13:00 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
   import org.apache.catalina.Context;
   import org.apache.catalina.Request;
   import org.apache.catalina.Response;
  -import org.apache.catalina.util.LocaleToCharsetMap;
  +import org.apache.catalina.util.CharsetMapper;
   import org.apache.catalina.util.RequestUtil;
   import org.apache.catalina.util.StringManager;
   
  @@ -88,7 +88,7 @@
    * the connector-specific methods need to be implemented.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.2 $ $Date: 2000/08/19 20:19:26 $
  + * @version $Revision: 1.3 $ $Date: 2000/09/27 02:13:00 $
    */
   
   public abstract class ResponseBase
  @@ -751,9 +751,10 @@
   	    return;	// Ignore any call from an included servlet
   
   	this.locale = locale;
  -	String encoding = LocaleToCharsetMap.getCharset(locale);
  -	if (encoding != null)
  -	    this.encoding = encoding;
  +	if ((this.encoding == null) && (this.context != null)) {
  +	    CharsetMapper mapper = context.getCharsetMapper();
  +	    this.encoding = mapper.getCharset(locale);
  +	}
   
       }
   
  
  
  
  1.15      +80 -4     jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java
  
  Index: StandardContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- StandardContext.java	2000/09/24 02:52:59	1.14
  +++ StandardContext.java	2000/09/27 02:13:01	1.15
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java,v 1.14 2000/09/24 02:52:59 craigmcc Exp $
  - * $Revision: 1.14 $
  - * $Date: 2000/09/24 02:52:59 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java,v 1.15 2000/09/27 02:13:01 craigmcc Exp $
  + * $Revision: 1.15 $
  + * $Date: 2000/09/27 02:13:01 $
    *
    * ====================================================================
    *
  @@ -97,6 +97,7 @@
   import org.apache.catalina.deploy.FilterMap;
   import org.apache.catalina.deploy.LoginConfig;
   import org.apache.catalina.deploy.SecurityConstraint;
  +import org.apache.catalina.util.CharsetMapper;
   
   
   /**
  @@ -105,7 +106,7 @@
    * requests directed to a particular servlet.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.14 $ $Date: 2000/09/24 02:52:59 $
  + * @version $Revision: 1.15 $ $Date: 2000/09/27 02:13:01 $
    */
   
   public final class StandardContext
  @@ -152,6 +153,19 @@
   
   
       /**
  +     * The Locale to character set mapper for this application.
  +     */
  +    private CharsetMapper charsetMapper = null;
  +
  +
  +    /**
  +     * The Java class name of the CharsetMapper class to be created.
  +     */
  +    private String charsetMapperClass =
  +      "org.apache.catalina.util.CharsetMapper";
  +
  +
  +    /**
        * The security constraints for this web application.
        */
       private SecurityConstraint constraints[] = new SecurityConstraint[0];
  @@ -385,6 +399,68 @@
       public void setApplicationListeners(Object listeners[]) {
   
           applicationListenersObjects = listeners;
  +
  +    }
  +
  +
  +    /**
  +     * Return the Locale to character set mapper for this Context.
  +     */
  +    public CharsetMapper getCharsetMapper() {
  +
  +        // Create a mapper the first time it is requested
  +        if (this.charsetMapper == null) {
  +            try {
  +	        Class clazz = Class.forName(charsetMapperClass);
  +		this.charsetMapper =
  +		  (CharsetMapper) clazz.newInstance();
  +	    } catch (Throwable t) {
  +	        this.charsetMapper = new CharsetMapper();
  +	    }
  +	}
  +
  +        return (this.charsetMapper);
  +
  +    }
  +
  +
  +    /**
  +     * Set the Locale to character set mapper for this Context.
  +     *
  +     * @param mapper The new mapper
  +     */
  +    public void setCharsetMapper(CharsetMapper mapper) {
  +
  +        CharsetMapper oldCharsetMapper = this.charsetMapper;
  +	this.charsetMapper = mapper;
  +	support.firePropertyChange("charsetMapper", oldCharsetMapper,
  +				   this.charsetMapper);
  +
  +    }
  +
  +
  +    /**
  +     * Return the Locale to character set mapper class for this Context.
  +     */
  +    public String getCharsetMapperClass() {
  +
  +        return (this.charsetMapperClass);
  +
  +    }
  +
  +
  +    /**
  +     * Set the Locale to character set mapper class for this Context.
  +     *
  +     * @param mapper The new mapper class
  +     */
  +    public void setCharsetMapperClass(String mapper) {
  +
  +        String oldCharsetMapperClass = this.charsetMapperClass;
  +	this.charsetMapperClass = mapper;
  +	support.firePropertyChange("charsetMapperClass",
  +				   oldCharsetMapperClass,
  +				   this.charsetMapperClass);
   
       }
   
  
  
  
  1.6       +5 -5      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/WrappedRequest.java
  
  Index: WrappedRequest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/WrappedRequest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- WrappedRequest.java	2000/09/25 21:46:33	1.5
  +++ WrappedRequest.java	2000/09/27 02:13:01	1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/WrappedRequest.java,v 1.5 2000/09/25 21:46:33 craigmcc Exp $
  - * $Revision: 1.5 $
  - * $Date: 2000/09/25 21:46:33 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/WrappedRequest.java,v 1.6 2000/09/27 02:13:01 craigmcc Exp $
  + * $Revision: 1.6 $
  + * $Date: 2000/09/27 02:13:01 $
    *
    * ====================================================================
    *
  @@ -113,7 +113,7 @@
    * <code>parameters</code> instance variables.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.5 $ $Date: 2000/09/25 21:46:33 $
  + * @version $Revision: 1.6 $ $Date: 2000/09/27 02:13:01 $
    */
   
   final class WrappedRequest
  @@ -1233,7 +1233,7 @@
        * Set the character encoding to be used for this request.
        */
       public void setCharacterEncoding(String enc) {
  -        ; // FIXME - setCharacterEncoding()
  +        request.getRequest().setCharacterEncoding(enc);
       }
   
   
  
  
  
  1.5       +2 -0      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/LocalStrings.properties,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- LocalStrings.properties	2000/09/22 23:52:53	1.4
  +++ LocalStrings.properties	2000/09/27 02:13:04	1.5
  @@ -13,6 +13,8 @@
   contextConfig.cce=Lifecycle event data object {0} is not a Context
   contextConfig.certificatesConfig.added=Added certificates -> request attribute Valve
   contextConfig.certificatesConfig.error=Exception adding CertificatesValve:
  +contextConfig.charsetMapper=Configuring Locale to character set mapper
  +contextConfig.charsetMapperError=Error configuring Locale to character set mapper
   contextConfig.defaultClose=Error closing default web.xml
   contextConfig.defaultConfig=Configuration error in default web.xml
   contextConfig.defaultDeploy=Processing defaults deployment descriptor
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/CharsetMapper.java
  
  Index: CharsetMapper.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  /*
   * This class is based on a class originally written by Jason Hunter
   * <jh...@acm.org> as part of the book "Java Servlet Programming"
   * (O'Reilly).  See http://www.servlets.com/book for more information.
   * Used by Sun Microsystems with permission.
   */
  
  package org.apache.catalina.util;
  
  
  import java.io.InputStream;
  import java.util.Locale;
  import java.util.Properties;
  
  
  
  /**
   * Utility class that attempts to map from a Locale to the corresponding
   * character set to be used for interpreting input text (or generating
   * output text) when the Content-Type header does not include one.  You
   * can customize the behavior of this class by modifying the mapping data
   * it loads, or by subclassing it (to change the algorithm) and then using
   * your own version for a particular web application.
   *
   * @author Craig R. McClanahan
   * @revision $Date: 2000/09/27 02:13:06 $ $Version$
   */
  
  public class CharsetMapper {
  
  
      // ---------------------------------------------------- Manifest Constants
  
  
      /**
       * Default properties resource name.
       */
      public static final String DEFAULT_RESOURCE =
        "/org/apache/catalina/util/CharsetMapperDefault.properties";
  
  
      // ---------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new CharsetMapper using the default properties resource.
       */
      public CharsetMapper() {
  
          this(DEFAULT_RESOURCE);
  
      }
  
  
      /**
       * Construct a new CharsetMapper using the specified properties resource.
       *
       * @param name Name of a properties resource to be loaded
       *
       * @exception IllegalArgumentException if the specified properties
       *  resource could not be loaded for any reason.
       */
      public CharsetMapper(String name) {
  
          try {
  	    InputStream stream =
  	      this.getClass().getResourceAsStream(name);
  	    map.load(stream);
  	    stream.close();
  	} catch (Throwable t) {
  	    throw new IllegalArgumentException(t.toString());
  	}
  
  
      }
  
  
      // ---------------------------------------------------- Instance Variables
  
  
      /**
       * The mapping properties that have been initialized from the specified or
       * default properties resource.
       */
      private Properties map = new Properties();
  
  
  
  
      // ------------------------------------------------------- Public Methods
  
  
      /**
       * Calculate the name of a character set to be assumed, given the specified
       * Locale and the absence of a character set specified as part of the
       * content type header.
       *
       * @param locale The locale for which to calculate a character set
       */
      public String getCharset(Locale locale) {
  
          String charset = null;
  
          // First, try a full name match (language and country)
          charset = map.getProperty(locale.toString());
  	if (charset != null)
  	    return (charset);
  
  	// Second, try to match just the language
  	charset = map.getProperty(locale.getLanguage());
  	return (charset);
  
      }
  
  
  }