Revision: 322
Author:   tfenne
Date:     2006-06-18 08:18:58 -0700 (Sun, 18 Jun 2006)
ViewCVS:  http://svn.sourceforge.net/stripes/?rev=322&view=rev

Log Message:
-----------
Implementation of STS-210: Improved locale-based character-encoding handling

Modified Paths:
--------------
    trunk/examples/web/bugzooky/AddEditBug.jsp
    trunk/examples/web/bugzooky/AdministerBugzooky.jsp
    trunk/examples/web/bugzooky/BugList.jsp
    trunk/examples/web/bugzooky/BulkAddEditBugs.jsp
    trunk/examples/web/bugzooky/Exit.jsp
    trunk/examples/web/bugzooky/Login.jsp
    trunk/examples/web/bugzooky/Register.jsp
    trunk/examples/web/bugzooky/Register2.jsp
    trunk/examples/web/bugzooky/layout/standard.jsp
    trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java
    
trunk/stripes/src/net/sourceforge/stripes/localization/DefaultLocalePicker.java
    trunk/stripes/src/net/sourceforge/stripes/localization/LocalePicker.java
Modified: trunk/examples/web/bugzooky/AddEditBug.jsp
===================================================================
--- trunk/examples/web/bugzooky/AddEditBug.jsp  2006-06-18 15:07:05 UTC (rev 
321)
+++ trunk/examples/web/bugzooky/AddEditBug.jsp  2006-06-18 15:18:58 UTC (rev 
322)
@@ -1,5 +1,4 @@
 <%@ page import="net.sourceforge.stripes.examples.bugzooky.biz.Status"%>
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Add/Edit 
Bug">

Modified: trunk/examples/web/bugzooky/AdministerBugzooky.jsp
===================================================================
--- trunk/examples/web/bugzooky/AdministerBugzooky.jsp  2006-06-18 15:07:05 UTC 
(rev 321)
+++ trunk/examples/web/bugzooky/AdministerBugzooky.jsp  2006-06-18 15:18:58 UTC 
(rev 322)
@@ -1,4 +1,3 @@
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Administer 
Bugzooky">

Modified: trunk/examples/web/bugzooky/BugList.jsp
===================================================================
--- trunk/examples/web/bugzooky/BugList.jsp     2006-06-18 15:07:05 UTC (rev 
321)
+++ trunk/examples/web/bugzooky/BugList.jsp     2006-06-18 15:18:58 UTC (rev 
322)
@@ -1,5 +1,3 @@
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Bug List">

Modified: trunk/examples/web/bugzooky/BulkAddEditBugs.jsp
===================================================================
--- trunk/examples/web/bugzooky/BulkAddEditBugs.jsp     2006-06-18 15:07:05 UTC 
(rev 321)
+++ trunk/examples/web/bugzooky/BulkAddEditBugs.jsp     2006-06-18 15:18:58 UTC 
(rev 322)
@@ -1,4 +1,3 @@
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Bulk 
Add/Edit Bugs">

Modified: trunk/examples/web/bugzooky/Exit.jsp
===================================================================
--- trunk/examples/web/bugzooky/Exit.jsp        2006-06-18 15:07:05 UTC (rev 
321)
+++ trunk/examples/web/bugzooky/Exit.jsp        2006-06-18 15:18:58 UTC (rev 
322)
@@ -1,4 +1,3 @@
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Thank You 
For Using Bugzooky">

Modified: trunk/examples/web/bugzooky/Login.jsp
===================================================================
--- trunk/examples/web/bugzooky/Login.jsp       2006-06-18 15:07:05 UTC (rev 
321)
+++ trunk/examples/web/bugzooky/Login.jsp       2006-06-18 15:18:58 UTC (rev 
322)
@@ -1,4 +1,3 @@
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Login">

Modified: trunk/examples/web/bugzooky/Register.jsp
===================================================================
--- trunk/examples/web/bugzooky/Register.jsp    2006-06-18 15:07:05 UTC (rev 
321)
+++ trunk/examples/web/bugzooky/Register.jsp    2006-06-18 15:18:58 UTC (rev 
322)
@@ -1,5 +1,4 @@
 <%@ page import="net.sourceforge.stripes.examples.bugzooky.biz.Status"%>
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Register">

Modified: trunk/examples/web/bugzooky/Register2.jsp
===================================================================
--- trunk/examples/web/bugzooky/Register2.jsp   2006-06-18 15:07:05 UTC (rev 
321)
+++ trunk/examples/web/bugzooky/Register2.jsp   2006-06-18 15:18:58 UTC (rev 
322)
@@ -1,5 +1,4 @@
 <%@ page import="net.sourceforge.stripes.examples.bugzooky.biz.Status"%>
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-render name="/bugzooky/layout/standard.jsp" title="Register">

Modified: trunk/examples/web/bugzooky/layout/standard.jsp
===================================================================
--- trunk/examples/web/bugzooky/layout/standard.jsp     2006-06-18 15:07:05 UTC 
(rev 321)
+++ trunk/examples/web/bugzooky/layout/standard.jsp     2006-06-18 15:18:58 UTC 
(rev 322)
@@ -1,4 +1,3 @@
-<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ include file="/bugzooky/taglibs.jsp" %>
 
 <stripes:layout-definition>

Modified: 
trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java     
2006-06-18 15:07:05 UTC (rev 321)
+++ trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java     
2006-06-18 15:18:58 UTC (rev 322)
@@ -204,11 +204,29 @@
             // Pop the configuration into thread local
             StripesFilter.configurationStash.set(this.configuration);
 
-            // Figure out the locale to use, and then wrap the request
+            // Figure out the locale and character encoding to use. The 
ordering of things here
+            // is very important!! We pick the locale first since picking the 
encoding is
+            // locale dependent, but the encoding *must* be set on the request 
before any
+            // parameters or parts are accessed, and wrapping the request 
accesses stuff.
             Locale locale = 
this.configuration.getLocalePicker().pickLocale(httpRequest);
+            log.debug("LocalePicker selected locale: ", locale);
+
+            String encoding = 
this.configuration.getLocalePicker().pickCharacterEncoding(httpRequest, locale);
+            if (encoding != null) {
+                httpRequest.setCharacterEncoding(encoding);
+                log.debug("LocalePicker selected character encoding: ", 
encoding);
+            }
+            else {
+                log.debug("LocalePicker did not pick a character encoding, 
using default: ",
+                          httpRequest.getCharacterEncoding());
+            }
+
             StripesRequestWrapper request = wrapRequest(httpRequest);
             request.setLocale(locale);
-            log.debug("LocalePicker selected locale: ", locale);
+            httpResponse.setLocale(locale);
+            if (encoding != null) {
+                httpResponse.setCharacterEncoding(encoding);
+            }
 
             // Execute the rest of the chain
             flashInbound(request);

Modified: 
trunk/stripes/src/net/sourceforge/stripes/localization/DefaultLocalePicker.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/localization/DefaultLocalePicker.java 
    2006-06-18 15:07:05 UTC (rev 321)
+++ 
trunk/stripes/src/net/sourceforge/stripes/localization/DefaultLocalePicker.java 
    2006-06-18 15:18:58 UTC (rev 322)
@@ -22,6 +22,9 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Collections;
+import java.util.Map;
+import java.util.HashMap;
+import java.nio.charset.Charset;
 
 /**
  * <p>Default locale picker that uses a comma separated list of locales in the 
servlet init
@@ -56,6 +59,9 @@
     /** Stores the configured set of Locales that the system supports, looked 
up at init time. */
     protected List<Locale> locales = new ArrayList<Locale>();
 
+    /** Contains a map of Locale to preferred character encoding. */
+    protected Map<Locale,String> encodings = new HashMap<Locale,String>();
+
     /**
      * Attempts to read the
      * @param configuration
@@ -75,26 +81,49 @@
             // Split apart the Locales on commas, and then parse the local 
strings into their bits
             String[] localeStrings = configuredLocales.split(",");
             for (String localeString : localeStrings) {
-                String[] parts = localeString.split("[-_]");
+                // Each locale string can be made up of two parts, 
locale:encoding
+                // and the locale can be made up of up to three segment, e.g. 
en_US_PC
+
+                String[] halves = localeString.split(":");
+                String[] parts = halves[0].split("[-_]");
+                Locale locale = null;
+
                 if (parts.length == 1) {
-                    this.locales.add( new 
Locale(parts[0].trim().toLowerCase()));
+                    locale = new Locale(parts[0].trim().toLowerCase());
                 }
                 else if (parts.length == 2) {
-                    this.locales.add( new Locale(parts[0].trim().toLowerCase(),
-                                                 
parts[1].trim().toUpperCase()));
+                    locale = new Locale(parts[0].trim().toLowerCase(),
+                                        parts[1].trim().toUpperCase());
                 }
                 else if (parts.length == 3) {
-                    this.locales.add( new Locale(parts[0].trim().toLowerCase(),
-                                                 parts[1].trim().toUpperCase(),
-                                                 parts[2].trim()));
+                    locale =  new Locale(parts[0].trim().toLowerCase(),
+                                         parts[1].trim().toUpperCase(),
+                                         parts[2].trim());
                 }
                 else {
                     log.error("Configuration property ", LOCALE_LIST, " 
contained a locale value ",
                               "that split into more than three parts! The 
parts were: ", parts);
                 }
+
+                this.locales.add(locale);
+
+                // Now check to see if a character encoding was specified, and 
if so is it valid
+                if (halves.length == 2) {
+                    String encoding = halves[1];
+
+                    if (Charset.isSupported(encoding)) {
+                        this.encodings.put(locale, halves[1]);
+                    }
+                    else {
+                        log.error("Configuration property ", LOCALE_LIST, " 
contained a locale value ",
+                                  "with an unsupported character encoding. The 
offending entry is: ",
+                                  localeString);
+                    }
+                }
             }
 
-            log.debug("Configured DefaultLocalPicker with locales: ", 
this.locales);
+            log.debug("Configured DefaultLocalePicker with locales: ", 
this.locales);
+            log.debug("Configured DefaultLocalePicker with encodings: ", 
this.encodings);
         }
     }
 
@@ -155,4 +184,15 @@
         }
     }
 
+    /**
+     * Returns the character encoding to use for the request and locale if one 
has been
+     * specified in the configuration.  If no value has been specified, 
returns null.
+     *
+     * @param request the current request
+     * @param locale the locale picked for the request
+     * @return a valid character encoding or null
+     */
+    public String pickCharacterEncoding(HttpServletRequest request, Locale 
locale) {
+        return this.encodings.get(locale);
+    }
 }

Modified: 
trunk/stripes/src/net/sourceforge/stripes/localization/LocalePicker.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/localization/LocalePicker.java    
2006-06-18 15:07:05 UTC (rev 321)
+++ trunk/stripes/src/net/sourceforge/stripes/localization/LocalePicker.java    
2006-06-18 15:18:58 UTC (rev 322)
@@ -43,4 +43,16 @@
      *         localized output
      */
     Locale pickLocale(HttpServletRequest request);
+
+    /**
+     * Picks the character encoding to use for the current request using the 
specified
+     * Locale. The character encoding will be set on both the request and the 
response. If the
+     * LocalePicker does not wish to change or specify a character encoding 
then this
+     * method should return null.
+     *
+     * @param request the current HttpServletRequest
+     * @param locale the Locale picked by the LocalePicker for this request
+     * @return the name of the character encoding to use, or null to use the 
default
+     */
+    String pickCharacterEncoding(HttpServletRequest request, Locale locale);
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to