Author: jogep
Date: Wed Apr 27 16:36:54 2011
New Revision: 1097172
URL: http://svn.apache.org/viewvc?rev=1097172&view=rev
Log:
WW-3614: Incorrect Content-Type in REST Plugin JSON Content Handler
WW-3536: Specify UTF-8 content type in JsonLibHandler
Modified:
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
Modified:
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java?rev=1097172&r1=1097171&r2=1097172&view=diff
==============================================================================
---
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java
(original)
+++
struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java
Wed Apr 27 16:36:54 2011
@@ -21,20 +21,27 @@
package org.apache.struts2.rest.handler;
-import net.sf.json.JSONArray;
-import net.sf.json.JSONObject;
-import net.sf.json.JsonConfig;
-
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
+import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
+import net.sf.json.JsonConfig;
+
+import org.apache.struts2.StrutsConstants;
+
+import com.opensymphony.xwork2.inject.Inject;
+
/**
* Handles JSON content using json-lib
*/
public class JsonLibHandler implements ContentTypeHandler {
+ private static final String DEFAULT_CONTENT_TYPE = "application/json";
+ private String defaultEncoding = "ISO-8859-1";
+
public void toObject(Reader in, Object target) throws IOException {
StringBuilder sb = new StringBuilder();
char[] buffer = new char[1024];
@@ -76,10 +83,15 @@ public class JsonLibHandler implements C
}
public String getContentType() {
- return "text/javascript";
+ return DEFAULT_CONTENT_TYPE+";charset=" + this.defaultEncoding;
}
public String getExtension() {
return "json";
}
+
+ @Inject(StrutsConstants.STRUTS_I18N_ENCODING)
+ public void setDefaultEncoding(String val) {
+ this.defaultEncoding = val;
+ }
}
Modified:
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java?rev=1097172&r1=1097171&r2=1097172&view=diff
==============================================================================
---
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
(original)
+++
struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
Wed Apr 27 16:36:54 2011
@@ -21,13 +21,13 @@
package org.apache.struts2.rest.handler;
-import junit.framework.TestCase;
-
-import java.io.StringWriter;
import java.io.IOException;
import java.io.StringReader;
+import java.io.StringWriter;
import java.util.Arrays;
+import junit.framework.TestCase;
+
public class JsonLibHandlerTest extends TestCase {
public void testFromObject() throws IOException {
@@ -67,4 +67,15 @@ public class JsonLibHandlerTest extends
assertEquals(contact, target);
}
+
+ public void testContentType() throws IOException {
+ JsonLibHandler handler = new JsonLibHandler();
+ assertEquals(handler.getContentType(),
"application/json;charset=ISO-8859-1");
+ }
+
+ public void testDefaultEncoding() throws IOException {
+ JsonLibHandler handler = new JsonLibHandler();
+ handler.setDefaultEncoding("UTF-8");
+ assertEquals(handler.getContentType(),
"application/json;charset=UTF-8");
+ }
}