Author: lukaszlenart
Date: Fri Dec 14 08:53:49 2012
New Revision: 1421740

URL: http://svn.apache.org/viewvc?rev=1421740&view=rev
Log:
WW-3863 Allows to define encoding per result

Modified:
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
    
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java

Modified: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java?rev=1421740&r1=1421739&r2=1421740&view=diff
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
 (original)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
 Fri Dec 14 08:53:49 2012
@@ -65,6 +65,7 @@ public class JSONResult implements Resul
 
     private static final Logger LOG = 
LoggerFactory.getLogger(JSONResult.class);
 
+    private String encoding;
     private String defaultEncoding = "ISO-8859-1";
     private List<Pattern> includeProperties;
     private List<Pattern> excludeProperties;
@@ -217,10 +218,14 @@ public class JSONResult implements Resul
      * Retrieve the encoding <p/>
      *
      * @return The encoding associated with this template (defaults to the 
value
-     *         of 'struts.i18n.encoding' property)
+     *         of param 'encoding', if empty default to 'struts.i18n.encoding' 
property)
      */
     protected String getEncoding() {
-        String encoding = this.defaultEncoding;
+        String encoding = this.encoding;
+
+        if (encoding == null) {
+            encoding = this.defaultEncoding;
+        }
 
         if (encoding == null) {
             encoding = System.getProperty("file.encoding");
@@ -421,4 +426,16 @@ public class JSONResult implements Resul
     public void setWrapSuffix(String wrapSuffix) {
         this.wrapSuffix = wrapSuffix;
     }
+
+    /**
+     * If defined will be used instead of {@link #defaultEncoding}, you can 
define it with result
+     * &lt;result name=&quot;success&quot; type=&quot;json&quot;&gt;
+     *     &lt;param name=&quot;encoding&quot;&gt;UTF-8&lt;/param&gt;
+     * &lt;/result&gt;
+     *
+     * @param encoding valid encoding string
+     */
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
 }

Modified: 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java?rev=1421740&r1=1421739&r2=1421740&view=diff
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java
 (original)
+++ 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java
 Fri Dec 14 08:53:49 2012
@@ -20,6 +20,16 @@
  */
 package org.apache.struts2.json;
 
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.mock.MockActionInvocation;
+import com.opensymphony.xwork2.util.ValueStack;
+import org.apache.struts2.StrutsStatics;
+import org.apache.struts2.StrutsTestCase;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockServletContext;
+
+import javax.servlet.http.HttpServletResponse;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.ArrayList;
@@ -34,18 +44,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.regex.Pattern;
 
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.struts2.StrutsStatics;
-import org.apache.struts2.StrutsTestCase;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.springframework.mock.web.MockHttpServletResponse;
-import org.springframework.mock.web.MockServletContext;
-
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.mock.MockActionInvocation;
-import com.opensymphony.xwork2.util.ValueStack;
-
 /**
  * JSONResultTest
  */
@@ -573,6 +571,31 @@ public class JSONResultTest extends Stru
         assertEquals("application/json;charset=ISO-8859-1", 
response.getContentType());
     }
 
+    public void testDefaultEncoding() throws Exception {
+        // given
+        JSONResult json = new JSONResult();
+        json.setDefaultEncoding("UTF-16");
+
+        // when
+        String encoding = json.getEncoding();
+
+        // thn
+        assertEquals("UTF-16", encoding);
+    }
+
+    public void testEncoding() throws Exception {
+        // given
+        JSONResult json = new JSONResult();
+        json.setEncoding("UTF-8");
+        json.setDefaultEncoding("UTF-8");
+
+        // when
+        String encoding = json.getEncoding();
+
+        // thn
+        assertEquals("UTF-8", encoding);
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();


Reply via email to