wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r810226 - in /incubator/wink/trunk: wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/ wink-common/src/main/resources/META-INF/core/ wink-common/src/main/resources/org/apache/wink/common/internal/i18n/ wink-ser...
Date Tue, 01 Sep 2009 20:45:20 GMT
Author: bluk
Date: Tue Sep  1 20:45:19 2009
New Revision: 810226

URL: http://svn.apache.org/viewvc?rev=810226&view=rev
Log:
Add JSON read/write capabilities with JSONArray

- Add feature to directly read JSONObjects
- Add feature to read/write JSONArrays
- Removed extend AbstractJAXBProvider from JSONProvider
  since it wasn't needed

Added:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonArrayProvider.java
      - copied, changed from r810125, incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java
Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java
    incubator/wink/trunk/wink-common/src/main/resources/META-INF/core/wink-providers
    incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/JsonProviderTest.java

Copied: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonArrayProvider.java
(from r810125, incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java)
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonArrayProvider.java?p2=incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonArrayProvider.java&p1=incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java&r1=810125&r2=810226&rev=810226&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonArrayProvider.java
Tue Sep  1 20:45:19 2009
@@ -17,15 +17,16 @@
  *  under the License.
  *  
  *******************************************************************************/
-
 package org.apache.wink.common.internal.providers.entity.json;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
@@ -33,29 +34,32 @@
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.wink.common.RestConstants;
 import org.apache.wink.common.internal.i18n.Messages;
-import org.apache.wink.common.internal.providers.entity.xml.AbstractJAXBProvider;
 import org.apache.wink.common.internal.utils.MediaTypeUtils;
 import org.apache.wink.common.utils.ProviderUtils;
-import org.json.JSONObject;
+import org.json.JSONArray;
 import org.json.JSONException;
+import org.json.JSONTokener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @Provider
+@Consumes( {MediaType.APPLICATION_JSON, MediaTypeUtils.JAVASCRIPT})
 @Produces( {MediaType.APPLICATION_JSON, MediaTypeUtils.JAVASCRIPT})
-public class JsonProvider extends AbstractJAXBProvider implements MessageBodyWriter<JSONObject>
{
+public class JsonArrayProvider implements MessageBodyWriter<JSONArray>,
+    MessageBodyReader<JSONArray> {
 
     private static final Logger logger = LoggerFactory.getLogger(JsonProvider.class);
 
     @Context
     private UriInfo             uriInfo;
 
-    public long getSize(JSONObject t,
+    public long getSize(JSONArray t,
                         Class<?> type,
                         Type genericType,
                         Annotation[] annotations,
@@ -67,10 +71,10 @@
                                Type genericType,
                                Annotation[] annotations,
                                MediaType mediaType) {
-        return type == JSONObject.class;
+        return type == JSONArray.class;
     }
 
-    public void writeTo(JSONObject t,
+    public void writeTo(JSONArray t,
                         Class<?> type,
                         Type genericType,
                         Annotation[] annotations,
@@ -81,7 +85,7 @@
         try {
             jsonString = t.toString(2);
         } catch (JSONException e) {
-            logger.error(Messages.getMessage("jsonFailWriteJSONObject"));
+            logger.error(Messages.getMessage("jsonFailWriteJSONArray"));
             throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
         }
 
@@ -100,4 +104,26 @@
         writer.flush();
     }
 
+    public boolean isReadable(Class<?> type,
+                              Type genericType,
+                              Annotation[] annotations,
+                              MediaType mediaType) {
+        return type == JSONArray.class;
+    }
+
+    public JSONArray readFrom(Class<JSONArray> type,
+                              Type genericType,
+                              Annotation[] annotations,
+                              MediaType mediaType,
+                              MultivaluedMap<String, String> httpHeaders,
+                              InputStream entityStream) throws IOException, WebApplicationException
{
+        try {
+            return new JSONArray(new JSONTokener(ProviderUtils
+                .createReader(entityStream, mediaType)));
+        } catch (JSONException e) {
+            logger.error(Messages.getMessage("jsonFailReadJSONArray"));
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+        }
+    }
+
 }

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java?rev=810226&r1=810225&r2=810226&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/json/JsonProvider.java
Tue Sep  1 20:45:19 2009
@@ -21,11 +21,13 @@
 package org.apache.wink.common.internal.providers.entity.json;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
@@ -33,22 +35,25 @@
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.wink.common.RestConstants;
 import org.apache.wink.common.internal.i18n.Messages;
-import org.apache.wink.common.internal.providers.entity.xml.AbstractJAXBProvider;
 import org.apache.wink.common.internal.utils.MediaTypeUtils;
 import org.apache.wink.common.utils.ProviderUtils;
-import org.json.JSONObject;
 import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @Provider
+@Consumes( {MediaType.APPLICATION_JSON, MediaTypeUtils.JAVASCRIPT})
 @Produces( {MediaType.APPLICATION_JSON, MediaTypeUtils.JAVASCRIPT})
-public class JsonProvider extends AbstractJAXBProvider implements MessageBodyWriter<JSONObject>
{
+public class JsonProvider implements MessageBodyWriter<JSONObject>,
+    MessageBodyReader<JSONObject> {
 
     private static final Logger logger = LoggerFactory.getLogger(JsonProvider.class);
 
@@ -100,4 +105,27 @@
         writer.flush();
     }
 
+    public boolean isReadable(Class<?> type,
+                              Type genericType,
+                              Annotation[] annotations,
+                              MediaType mediaType) {
+        return type == JSONObject.class;
+    }
+
+    public JSONObject readFrom(Class<JSONObject> type,
+                               Type genericType,
+                               Annotation[] annotations,
+                               MediaType mediaType,
+                               MultivaluedMap<String, String> httpHeaders,
+                               InputStream entityStream) throws IOException,
+        WebApplicationException {
+        try {
+            return new JSONObject(new JSONTokener(ProviderUtils.createReader(entityStream,
+                                                                             mediaType)));
+        } catch (JSONException e) {
+            logger.error(Messages.getMessage("jsonFailReadJSONObject"));
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+        }
+    }
+
 }

Modified: incubator/wink/trunk/wink-common/src/main/resources/META-INF/core/wink-providers
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/resources/META-INF/core/wink-providers?rev=810226&r1=810225&r2=810226&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/resources/META-INF/core/wink-providers (original)
+++ incubator/wink/trunk/wink-common/src/main/resources/META-INF/core/wink-providers Tue Sep
 1 20:45:19 2009
@@ -61,6 +61,7 @@
 
 # Json
 org.apache.wink.common.internal.providers.entity.json.JsonProvider
+org.apache.wink.common.internal.providers.entity.json.JsonArrayProvider
 org.apache.wink.common.internal.providers.entity.json.JsonJAXBProvider
 org.apache.wink.common.internal.providers.entity.json.JsonSyndEntryProvider
 org.apache.wink.common.internal.providers.entity.json.JsonSyndFeedProvider

Modified: incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties?rev=810226&r1=810225&r2=810226&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
(original)
+++ incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
Tue Sep  1 20:45:19 2009
@@ -82,6 +82,9 @@
 jsonFailConvertXMLToJSON=Failed to convert XML to JSON
 jsonFailConvertJAXBToJSON=Failed to convert JAXB object {} to JSONObject
 jsonFailWriteJSONObject=Failed to write JSONObject
+jsonFailReadJSONObject=Failed to read JSONObject
+jsonFailWriteJSONArray=Failed to write JSONArray
+jsonFailReadJSONArray=Failed to read JSONArray
 
 # JAXB Provider
 jaxbObjectFactoryNotFound=ObjectFactory for {} was not found

Modified: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/JsonProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/JsonProviderTest.java?rev=810226&r1=810225&r2=810226&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/JsonProviderTest.java
(original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/JsonProviderTest.java
Tue Sep  1 20:45:19 2009
@@ -20,8 +20,12 @@
 
 package org.apache.wink.server.internal.providers.entity;
 
+import java.io.StringReader;
+import java.util.Collections;
+
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
@@ -43,8 +47,10 @@
 import org.apache.wink.common.model.synd.SyndText;
 import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
 import org.apache.wink.test.mock.MockRequestConstructor;
+import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.json.JSONTokener;
 import org.springframework.mock.web.MockHttpServletRequest;
 import org.springframework.mock.web.MockHttpServletResponse;
 
@@ -83,6 +89,8 @@
                                                               + "  }\n"
                                                               + "}}";
 
+    private static final String    JSON_ARRAY         = "[" + JSON + ", {\"test\":\"ing\"}]";
+
     private static final String    JSON_AS_ATOM_ENTRY =
                                                           "{\"entry\": {\n" + "  \"@xmlns\":
{\"$\": \"http:\\/\\/www.w3.org\\/2005\\/Atom\"},\n"
                                                               + "  \"id\": {\n"
@@ -113,6 +121,29 @@
             return new JSONObject(JSON);
         }
 
+        @POST
+        @Path("json")
+        @Consumes(MediaType.APPLICATION_JSON)
+        @Produces(MediaType.APPLICATION_JSON)
+        public JSONObject postJson(JSONObject object) throws Exception {
+            return object.put("foo", "bar");
+        }
+
+        @GET
+        @Path("jsonarray")
+        @Produces(MediaType.APPLICATION_JSON)
+        public JSONArray getJsonArray() throws Exception {
+            return new JSONArray(JSON_ARRAY);
+        }
+
+        @POST
+        @Path("jsonarray")
+        @Consumes(MediaType.APPLICATION_JSON)
+        @Produces(MediaType.APPLICATION_JSON)
+        public JSONArray postJson(JSONArray array) throws Exception {
+            return array.put(Collections.singletonMap("foo", "bar"));
+        }
+
         @GET
         @Path("jsonfeed")
         @Produces("application/json")
@@ -251,6 +282,48 @@
         compairJsonContent(JSON, response.getContentAsString());
     }
 
+    public void testPostJson() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("POST",
+                                                        "/test/json",
+                                                        "application/json",
+                                                        MediaType.APPLICATION_JSON,
+                                                        JSON.getBytes());
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        JSONObject result = JSONUtils.objectForString(response.getContentAsString());
+        JSONObject want = JSONUtils.objectForString(JSON).put("foo", "bar");
+        assertTrue(JSONUtils.equals(want, result));
+    }
+
+    public void testGetJsonArray() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("GET",
+                                                        "/test/jsonarray",
+                                                        "application/json");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        JSONArray result =
+            new JSONArray(new JSONTokener(new StringReader(response.getContentAsString())));
+        JSONArray want = new JSONArray(JSON_ARRAY);
+        assertTrue(JSONUtils.equals(want, result));
+    }
+
+    public void testPostJsonArray() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("POST",
+                                                        "/test/jsonarray",
+                                                        "application/json",
+                                                        MediaType.APPLICATION_JSON,
+                                                        JSON_ARRAY.getBytes());
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        JSONArray result =
+            new JSONArray(new JSONTokener(new StringReader(response.getContentAsString())));
+        JSONArray want = new JSONArray(JSON_ARRAY).put(Collections.singletonMap("foo", "bar"));
+        assertTrue(JSONUtils.equals(want, result));
+    }
+
     public void testGetJsonFeed() throws Exception {
         MockHttpServletRequest request =
             MockRequestConstructor



Mime
View raw message