Commits

Anonymous committed 8610d9e

WW-698 : webwork.i18n.encoding property in webwork.properties does what it did in WebWork 1.x:
request.setCharacterEncoding(encoding);

WW-490 : webwork.locale property setting was being ignored. Now it sets the specified Locale into the response and into the ActionContext.

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@639573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (6)

src/java/com/opensymphony/webwork/dispatcher/ServletDispatcher.java

 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Locale;
 
 
 /*
      */
     protected static final Log log = LogFactory.getLog(ServletDispatcher.class);
 
+    // set from the webwork.i18n.encoding property in webwork.properties
+    protected static String encoding = null;
+
+    // set from webwork.locale property in webwork.properties
+    protected static Locale locale = null;
+
     //~ Instance fields ////////////////////////////////////////////////////////
 
     boolean paramsWorkaroundEnabled = false;
         extraContext.put(ActionContext.PARAMETERS, parameterMap);
         extraContext.put(ActionContext.SESSION, sessionMap);
         extraContext.put(ActionContext.APPLICATION, applicationMap);
-        extraContext.put(ActionContext.LOCALE, request.getLocale());
+        extraContext.put(ActionContext.LOCALE, (locale == null) ? request.getLocale() : locale);
 
         extraContext.put(HTTP_REQUEST, request);
         extraContext.put(HTTP_RESPONSE, response);
         return extraContext;
     }
 
+    public static String getEncoding() {
+        return encoding;
+    }
+
+    public static Locale getLocale() {
+        return locale;
+    }
+
+    public boolean isParamsWorkaroundEnabled() {
+        return paramsWorkaroundEnabled;
+    }
+
     /**
      * Initalizes the servlet. Please read the {@link ServletDispatcher class documentation} for more
      * detail. <p>
             FileManager.setReloadingConfigs(true);
         }
 
+        if (Configuration.isSet("webwork.i18n.encoding")) {
+            encoding = Configuration.getString("webwork.i18n.encoding");
+        }
+
+        if (Configuration.isSet("webwork.locale")) {
+            locale = localeFromString(Configuration.getString("webwork.locale"));
+        }
+
         // store a reference to ourself into the SessionContext so that we can generate a PageContext
         servletConfig.getServletContext().setAttribute("webwork.servlet", this);
 
      */
     public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
         try {
+            if (encoding != null) {
+                try {
+                    request.setCharacterEncoding(encoding);
+                } catch (Exception e) {
+                }
+            }
+            if (locale != null) {
+                response.setLocale(locale);
+            }
             if (paramsWorkaroundEnabled) {
                 request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request
             }
 
         return maxSize.intValue();
     }
+
+    /**
+     * Builds a {@link java.util.Locale} from a String of the form en_US_foo into a Locale
+     * with language "en", country "US" and variant "foo". This will parse the output of
+     * {@link java.util.Locale#toString()}.
+     * todo move this to LocalizedTextUtil in xwork 1.0.6
+     */
+    public static Locale localeFromString(String localeStr) {
+        if ((localeStr == null) || (localeStr.trim().length() == 0) || (localeStr.equals("_"))) {
+            return null;
+        }
+        int index = localeStr.indexOf('_');
+        if (index < 0) {
+            return new Locale(localeStr);
+        }
+        String language = localeStr.substring(0,index);
+        if (index == localeStr.length()) {
+            return new Locale(language);
+        }
+        localeStr = localeStr.substring(index +1);
+        index = localeStr.indexOf('_');
+        if (index < 0) {
+            return new Locale(language,localeStr);
+        }
+        String country = localeStr.substring(0,index);
+        if (index == localeStr.length()) {
+            return new Locale(language,country);
+        }
+        localeStr = localeStr.substring(index +1);
+        return new Locale(language,country,localeStr);
+    }
 }

src/test/com/opensymphony/webwork/dispatcher/AbstractServletDispatcherTestCase.java

 import com.mockobjects.servlet.MockServletConfig;
 import com.mockobjects.servlet.MockServletOutputStream;
 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest;
+import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletResponse;
 import com.opensymphony.xwork.config.ConfigurationManager;
 import com.opensymphony.xwork.config.providers.XmlConfigurationProvider;
 import junit.framework.TestCase;
         request.setupGetServletPath(getServletPath());
         request.setupGetPathInfo(getServletPath());
 
-        MockHttpServletResponse response = new MockHttpServletResponse();
+        MockHttpServletResponse response = new WebWorkMockHttpServletResponse();
         response.setupOutputStream(new MockServletOutputStream());
 
         Mock servletContextDMock = new Mock(ServletContext.class);

src/test/com/opensymphony/webwork/dispatcher/ServletDispatcherTest.java

  */
 package com.opensymphony.webwork.dispatcher;
 
+import javax.servlet.ServletException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Locale;
+import java.io.IOException;
 
 
 /**
         return "/Test.action";
     }
 
+    public void testEncodingAndLocaleSetFromWebWorkProperties() throws IOException, ServletException {
+        testServletDispatcher();
+        assertEquals("ISO-8859-1",ServletDispatcher.getEncoding());
+        assertEquals(Locale.GERMANY,ServletDispatcher.getLocale());
+    }
+
     public void testGetActionName() {
         ServletDispatcher dispatcher = new ServletDispatcher();
         String actionName = null;

src/test/com/opensymphony/webwork/views/jsp/WebWorkMockHttpServletRequest.java

     private String scheme;
     private String serverName;
     private int serverPort;
+    private String encoding;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
         return locale;
     }
 
+    public void setCharacterEncoding(String s) {
+        this.encoding = s;
+    }
+
+    public String getCharacterEncoding() {
+        return encoding;
+    }
+
     public void setParameterMap(Map parameterMap) {
         this.parameterMap = parameterMap;
     }

src/test/com/opensymphony/webwork/views/jsp/WebWorkMockHttpServletResponse.java

 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Locale;
 
 
 /**
  *         Created May 21, 2003 10:46:53 AM
  */
 public class WebWorkMockHttpServletResponse extends MockHttpServletResponse {
+    private Locale locale;
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    public Locale getLocale() {
+        return locale;
+    }
+
+    public void setLocale(Locale locale) {
+        this.locale = locale;
+    }
+
     public PrintWriter getWriter() throws IOException {
         return new PrintWriter(new ByteArrayOutputStream());
     }

src/test/webwork.properties

+webwork.i18n.encoding=ISO-8859-1
 webwork.locale=de_DE
 webwork.multipart.saveDir=\temp
 webwork.multipart.maxSize=12345
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.