Author: mrdon
Date: Sat Nov  3 04:32:28 2007
New Revision: 591601

URL: http://svn.apache.org/viewvc?rev=591601&view=rev
Log:
Changing json handler to use json-lib over xstream for cleaner output

Added:
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java
      - copied, changed from r589075, 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamJsonHandler.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/
    
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/Contact.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
Removed:
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamJsonHandler.java
Modified:
    struts/sandbox/trunk/struts2-rest-plugin/pom.xml
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/ContentTypeHandler.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/resources/struts-plugin.xml
    
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java

Modified: struts/sandbox/trunk/struts2-rest-plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/pom.xml?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/pom.xml (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/pom.xml Sat Nov  3 04:32:28 2007
@@ -24,14 +24,10 @@
             <version>1.2.2</version>
         </dependency>
         <dependency>
-            <groupId>org.codehaus.jettison</groupId>
-            <artifactId>jettison</artifactId>
-            <version>1.0-RC1</version>
-        </dependency>   
-        <dependency>
-            <groupId>stax</groupId>
-            <artifactId>stax-api</artifactId>
-            <version>1.0.1</version>
+            <groupId>net.sf.json-lib</groupId>
+            <artifactId>json-lib</artifactId>
+            <classifier>jdk15</classifier>
+            <version>2.1</version>
         </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
@@ -56,6 +52,13 @@
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-mock</artifactId>
+            <version>1.2.8</version>
+            <optional>true</optional>
+        </dependency>
+
+       <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-core</artifactId>
             <version>1.2.8</version>
             <optional>true</optional>
         </dependency>

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java
 Sat Nov  3 04:32:28 2007
@@ -33,6 +33,7 @@
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.StringWriter;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -132,13 +133,14 @@
             if (actionConfig.getResults().get(extCode) != null) {
                 resultCode = extCode;
             } else {
-                ByteArrayOutputStream bout = new ByteArrayOutputStream();
-
-                resultCode = handler.fromObject(target, resultCode, bout);
-                if (bout.size() > 0) {
-                    res.setContentLength(bout.size());
+                StringWriter writer = new StringWriter();
+                resultCode = handler.fromObject(target, resultCode, writer);
+                String text = writer.toString();
+                if (text.length() > 0) {
+                    byte[] data = text.getBytes("UTF-8");
+                    res.setContentLength(data.length);
                     res.setContentType(handler.getContentType());
-                    res.getOutputStream().write(bout.toByteArray());
+                    res.getOutputStream().write(data);
                     res.getOutputStream().close();
                 }
             }

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java
 Sat Nov  3 04:32:28 2007
@@ -57,7 +57,7 @@
         }
         
         if (request.getContentLength() > 0) {
-            handler.toObject(request.getInputStream(), target);
+            handler.toObject(request.getReader(), target);
         }
         return invocation.invoke();
     }

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java
 Sat Nov  3 04:32:28 2007
@@ -157,7 +157,7 @@
        }
        
        /**
-        * Intercept [EMAIL PROTECTED] ActionInvocation} and processes the 
errors using the [EMAIL PROTECTED] ContentTypeHandler}
+        * Intercept [EMAIL PROTECTED] ActionInvocation} and processes the 
errors using the [EMAIL PROTECTED] 
org.apache.struts2.rest.handler.ContentTypeHandler}
         * appropriate for the request.  
         * 
         * @return String result name

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/ContentTypeHandler.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/ContentTypeHandler.java?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/ContentTypeHandler.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/ContentTypeHandler.java
 Sat Nov  3 04:32:28 2007
@@ -20,9 +20,7 @@
  */
 package org.apache.struts2.rest.handler;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
 
 import com.opensymphony.xwork2.ActionInvocation;
 
@@ -36,7 +34,7 @@
      * @param in The input stream, usually the body of the request
      * @param target The target, usually the action class
      */
-    void toObject(InputStream in, Object target);
+    void toObject(Reader in, Object target) throws IOException;
     
     /**
      * Writes content to the stream
@@ -47,7 +45,7 @@
      * @return The new result code
      * @throws IOException If unable to write to the output stream
      */
-    String fromObject(Object obj, String resultCode, OutputStream stream) 
throws IOException;
+    String fromObject(Object obj, String resultCode, Writer stream) throws 
IOException;
     
     /**
      * Gets the content type for this handler

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java
 Sat Nov  3 04:32:28 2007
@@ -21,23 +21,19 @@
 package org.apache.struts2.rest.handler;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Collections;
-
-import com.opensymphony.xwork2.Action;
-import com.opensymphony.xwork2.ActionInvocation;
+import java.io.Reader;
+import java.io.Writer;
 
 /**
  * Handles HTML content, usually just a simple passthrough to the framework
  */
 public class HtmlHandler implements ContentTypeHandler {
 
-    public String fromObject(Object obj, String resultCode, OutputStream out) 
throws IOException {
+    public String fromObject(Object obj, String resultCode, Writer out) throws 
IOException {
         return resultCode;
     }
 
-    public void toObject(InputStream in, Object target) {
+    public void toObject(Reader in, Object target) {
     }
 
     public String getExtension() {

Copied: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java
 (from r589075, 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamJsonHandler.java)
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java?p2=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java&p1=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamJsonHandler.java&r1=589075&r2=591601&rev=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamJsonHandler.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java
 Sat Nov  3 04:32:28 2007
@@ -20,20 +20,37 @@
  */
 package org.apache.struts2.rest.handler;
 
-import com.thoughtworks.xstream.XStream;
-import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
+import java.io.*;
+
+import net.sf.json.JSONObject;
+import net.sf.json.JsonConfig;
 
 /**
- * Handles JSON content using XStream
+ * Handles JSON content using json-lib
  */
-public class XStreamJsonHandler extends XStreamHandler {
+public class JsonLibHandler implements ContentTypeHandler {
+
+    public void toObject(Reader in, Object target) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        char[] buffer = new char[1024];
+        int len = 0;
+        while ((len = in.read(buffer)) > 0) {
+            sb.append(buffer, 0, len);
+        }
+        JSONObject jsonObject = JSONObject.fromObject(sb.toString());
+        JSONObject.toBean(jsonObject, target, new JsonConfig());
+    }
+
+    public String fromObject(Object obj, String resultCode, Writer stream) 
throws IOException {
+        if (obj != null) {
+            JSONObject jsonObject = JSONObject.fromObject(obj);
+            stream.write(jsonObject.toString());
+        }
+        return null;
+
 
-    @Override
-    protected XStream createXStream() {
-        return new XStream(new JettisonMappedXmlDriver());
     }
 
-    @Override
     public String getContentType() {
         return "text/javascript";
     }

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java
 Sat Nov  3 04:32:28 2007
@@ -21,8 +21,8 @@
 package org.apache.struts2.rest.handler;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
 
 import com.thoughtworks.xstream.XStream;
 
@@ -31,7 +31,7 @@
  */
 public class XStreamHandler implements ContentTypeHandler {
 
-    public String fromObject(Object obj, String resultCode, OutputStream out) 
throws IOException {
+    public String fromObject(Object obj, String resultCode, Writer out) throws 
IOException {
         if (obj != null) {
             XStream xstream = createXStream();
             xstream.toXML(obj, out);
@@ -39,7 +39,7 @@
         return null;
     }
 
-    public void toObject(InputStream in, Object target) {
+    public void toObject(Reader in, Object target) {
         XStream xstream = createXStream();
         xstream.fromXML(in, target);
     }

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/resources/struts-plugin.xml
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/resources/struts-plugin.xml?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/resources/struts-plugin.xml 
(original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/resources/struts-plugin.xml 
Sat Nov  3 04:32:28 2007
@@ -14,7 +14,7 @@
        <bean class="org.apache.struts2.rest.ContentTypeHandlerManager" />
 
     <bean type="org.apache.struts2.rest.handler.ContentTypeHandler" name="xml" 
class="org.apache.struts2.rest.handler.XStreamHandler" />
-    <bean type="org.apache.struts2.rest.handler.ContentTypeHandler" 
name="json" class="org.apache.struts2.rest.handler.XStreamJsonHandler" />
+    <bean type="org.apache.struts2.rest.handler.ContentTypeHandler" 
name="json" class="org.apache.struts2.rest.handler.JsonLibHandler" />
     <bean type="org.apache.struts2.rest.handler.ContentTypeHandler" 
name="html" class="org.apache.struts2.rest.handler.HtmlHandler" />
 
     <constant name="struts.actionProxyFactory" value="rest" />

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java?rev=591601&r1=591600&r2=591601&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
 Sat Nov  3 04:32:28 2007
@@ -34,8 +34,8 @@
 import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -69,9 +69,9 @@
 
         String obj = "mystring";
         ContentTypeHandler handler = new ContentTypeHandler() {
-            public void toObject(InputStream in, Object target) {}
-            public String fromObject(Object obj, String resultCode, 
OutputStream stream) throws IOException {
-                stream.write(obj.toString().getBytes());
+            public void toObject(Reader in, Object target) {}
+            public String fromObject(Object obj, String resultCode, Writer 
stream) throws IOException {
+                stream.write(obj.toString());
                 return resultCode;
             }
             public String getContentType() { return "foo"; }

Added: 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/Contact.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/Contact.java?rev=591601&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/Contact.java
 (added)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/Contact.java
 Sat Nov  3 04:32:28 2007
@@ -0,0 +1,91 @@
+/*
+ * $Id: Restful2ActionMapper.java 540819 2007-05-23 02:48:36Z mrdon $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.rest.handler;
+
+import java.util.HashMap;
+
+public class Contact {
+    private String name;
+    private boolean important;
+    private int age;
+
+    public Contact() {}
+    
+    public Contact(String name, boolean important, int age) {
+        this.name = name;
+        this.important = important;
+        this.age = age;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public boolean isImportant() {
+        return important;
+    }
+
+    public void setImportant(boolean important) {
+        this.important = important;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Contact contact = (Contact) o;
+
+        if (age != contact.age) return false;
+        if (important != contact.important) return false;
+        if (name != null ? !name.equals(contact.name) : contact.name != null) 
return false;
+
+        return true;
+    }
+
+    public String toString() {
+        HashMap map = new HashMap();
+        map.put("age", age);
+        map.put("important", important);
+        map.put("name", name);
+        return map.toString();
+    }
+
+    public int hashCode() {
+        int result;
+        result = (name != null ? name.hashCode() : 0);
+        result = 31 * result + (important ? 1 : 0);
+        result = 31 * result + age;
+        return result;
+    }
+}

Added: 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java?rev=591601&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
 (added)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java
 Sat Nov  3 04:32:28 2007
@@ -0,0 +1,51 @@
+/*
+ * $Id: Restful2ActionMapper.java 540819 2007-05-23 02:48:36Z mrdon $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.rest.handler;
+
+import junit.framework.TestCase;
+
+import java.io.StringWriter;
+import java.io.IOException;
+import java.io.StringReader;
+
+public class JsonLibHandlerTest extends TestCase {
+
+    public void testFromObject() throws IOException {
+        Contact contact = new Contact("bob", true, 44);
+
+        StringWriter writer = new StringWriter();
+        JsonLibHandler handler = new JsonLibHandler();
+        handler.fromObject(contact, "success", writer);
+
+        assertEquals("{\"age\":44,\"important\":true,\"name\":\"bob\"}", 
writer.toString());
+    }
+
+    public void testToObject() throws IOException {
+        Contact contact = new Contact("bob", true, 44);
+
+        Contact target = new Contact();
+        StringReader reader = new 
StringReader("{\"age\":44,\"important\":true,\"name\":\"bob\"}");
+        JsonLibHandler handler = new JsonLibHandler();
+        handler.toObject(reader, target);
+
+        assertEquals(contact, target);
+    }
+}


Reply via email to