Author: lukaszlenart
Date: Wed Apr 11 17:41:06 2012
New Revision: 1324888

URL: http://svn.apache.org/viewvc?rev=1324888&view=rev
Log:
WW-3796 Changes logic to set encoding on HttpServletRequest if differs from 
configured

Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java?rev=1324888&r1=1324887&r2=1324888&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
 Wed Apr 11 17:41:06 2012
@@ -690,11 +690,7 @@ public class Dispatcher {
         }
 
         if (encoding != null) {
-            try {
-                request.setCharacterEncoding(encoding);
-            } catch (Exception e) {
-                LOG.error("Error setting character encoding to '" + encoding + 
"' - ignoring.", e);
-            }
+            applyEncoding(request, encoding);
         }
 
         if (locale != null) {
@@ -706,6 +702,18 @@ public class Dispatcher {
         }
     }
 
+    private void applyEncoding(HttpServletRequest request, String encoding) {
+        try {
+            if (!encoding.equals(request.getCharacterEncoding())) {
+                // if the encoding is already correctly set and the parameters 
have been already read
+                // do not try to set encoding because it is useless and will 
cause an error
+                request.setCharacterEncoding(encoding);
+            }
+        } catch (Exception e) {
+            LOG.error("Error setting character encoding to '" + encoding + "' 
- ignoring.", e);
+        }
+    }
+
     /**
      * Wrap and return the given request or return the original request object.
      * </p>

Modified: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java?rev=1324888&r1=1324887&r2=1324888&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
 (original)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
 Wed Apr 11 17:41:06 2012
@@ -70,7 +70,7 @@ public class DispatcherTest extends Stru
         HttpServletRequest req = new MockHttpServletRequest();
         HttpServletResponse res = new MockHttpServletResponse();
 
-        Dispatcher du = initDispatcher(new HashMap() {{
+        Dispatcher du = initDispatcher(new HashMap<String, String>() {{
             put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
         }});
         du.prepare(req, res);
@@ -82,9 +82,10 @@ public class DispatcherTest extends Stru
         // given
         MockHttpServletRequest req = new MockHttpServletRequest();
         req.addHeader("X-Requested-With", "XMLHttpRequest");
+        req.setCharacterEncoding("utf-8");
         HttpServletResponse res = new MockHttpServletResponse();
 
-        Dispatcher du = initDispatcher(new HashMap() {{
+        Dispatcher du = initDispatcher(new HashMap<String, String>() {{
             put(StrutsConstants.STRUTS_I18N_ENCODING, "latin-2");
         }});
 
@@ -95,12 +96,36 @@ public class DispatcherTest extends Stru
         assertEquals(req.getCharacterEncoding(), "utf-8");
     }
 
+    public void testSetEncodingIfDiffer() throws Exception {
+        // given
+        Mock mock = new Mock(HttpServletRequest.class);
+        mock.expectAndReturn("getCharacterEncoding", "utf-8");
+        mock.expectAndReturn("getHeader", "X-Requested-With", "");
+        mock.expectAndReturn("getLocale", Locale.getDefault());
+        mock.expectAndReturn("getCharacterEncoding", "utf-8");
+        HttpServletRequest req = (HttpServletRequest) mock.proxy();
+        HttpServletResponse res = new MockHttpServletResponse();
+
+        Dispatcher du = initDispatcher(new HashMap<String, String>() {{
+            put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
+        }});
+
+
+        // when
+        du.prepare(req, res);
+
+        // then
+
+        assertEquals(req.getCharacterEncoding(), "utf-8");
+        mock.verify();
+    }
+
     public void testPrepareSetEncodingPropertyWithMultipartRequest() throws 
Exception {
         MockHttpServletRequest req = new MockHttpServletRequest();
         MockHttpServletResponse res = new MockHttpServletResponse();
 
         req.setContentType("multipart/form-data");
-        Dispatcher du = initDispatcher(new HashMap() {{
+        Dispatcher du = initDispatcher(new HashMap<String, String>() {{
             put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
         }});
         du.prepare(req, res);
@@ -136,7 +161,7 @@ public class DispatcherTest extends Stru
     
     
     public void testConfigurationManager() {
-       Dispatcher du = null;
+       Dispatcher du;
        InternalConfigurationManager configurationManager = new 
InternalConfigurationManager();
        try {
                du = new Dispatcher(new MockServletContext(), new 
HashMap<String, String>());
@@ -154,7 +179,7 @@ public class DispatcherTest extends Stru
             
        }
        finally {
-               du.setInstance(null);
+               Dispatcher.setInstance(null);
        }
     }
     


Reply via email to