You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2010/10/20 18:37:07 UTC
svn commit: r1025634 - in /incubator/wink/trunk: ./ wink-providers/
wink-providers/wink-json4j-provider/ wink-providers/wink-json4j-provider/src/
wink-providers/wink-json4j-provider/src/main/
wink-providers/wink-json4j-provider/src/main/java/ wink-prov...
Author: bluk
Date: Wed Oct 20 16:37:06 2010
New Revision: 1025634
URL: http://svn.apache.org/viewvc?rev=1025634&view=rev
Log:
Add JSON4J providers for JSON4JArray/Object
Added:
incubator/wink/trunk/wink-providers/wink-json4j-provider/
incubator/wink/trunk/wink-providers/wink-json4j-provider/pom.xml
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JArrayProvider.java
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JObjectProvider.java
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/resources/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/resources/META-INF/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/resources/META-INF/wink-application
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/json/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/json/internal/
incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/json/internal/Json4JProviderTest.java
Modified:
incubator/wink/trunk/pom.xml
incubator/wink/trunk/wink-providers/pom.xml
Modified: incubator/wink/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/pom.xml?rev=1025634&r1=1025633&r2=1025634&view=diff
==============================================================================
--- incubator/wink/trunk/pom.xml (original)
+++ incubator/wink/trunk/pom.xml Wed Oct 20 16:37:06 2010
@@ -442,6 +442,11 @@
<artifactId>wink-webdav</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.wink</groupId>
+ <artifactId>wink-json4j</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<!-- 3rd party dependencies -->
<dependency>
Modified: incubator/wink/trunk/wink-providers/pom.xml
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/pom.xml?rev=1025634&r1=1025633&r2=1025634&view=diff
==============================================================================
--- incubator/wink/trunk/wink-providers/pom.xml (original)
+++ incubator/wink/trunk/wink-providers/pom.xml Wed Oct 20 16:37:06 2010
@@ -33,6 +33,7 @@
<module>wink-jettison-provider</module>
<module>wink-abdera-provider</module>
<module>wink-jackson-provider</module>
+ <module>wink-json4j-provider</module>
</modules>
<dependencies>
<dependency>
Added: incubator/wink/trunk/wink-providers/wink-json4j-provider/pom.xml
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-json4j-provider/pom.xml?rev=1025634&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-json4j-provider/pom.xml (added)
+++ incubator/wink/trunk/wink-providers/wink-json4j-provider/pom.xml Wed Oct 20 16:37:06 2010
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <artifactId>wink-providers</artifactId>
+ <groupId>org.apache.wink</groupId>
+ <version>1.1.2-incubating-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>wink-json4j-provider</artifactId>
+ <name>Apache Wink Providers :: JSON4J Provider</name>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.wink</groupId>
+ <artifactId>wink-common</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.wink</groupId>
+ <artifactId>wink-component-test-support</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.wink</groupId>
+ <artifactId>wink-json4j</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-j2ee_1.4_spec</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.wink</groupId>
+ <artifactId>wink-server</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>woodstox</groupId>
+ <artifactId>wstx-asl</artifactId>
+ <version>3.2.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
Added: incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JArrayProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JArrayProvider.java?rev=1025634&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JArrayProvider.java (added)
+++ incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JArrayProvider.java Wed Oct 20 16:37:06 2010
@@ -0,0 +1,104 @@
+/*
+ * 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.wink.providers.json4j;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+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.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.wink.common.internal.utils.MediaTypeUtils;
+import org.apache.wink.common.utils.ProviderUtils;
+import org.apache.wink.json4j.JSONArray;
+import org.apache.wink.json4j.JSONException;
+
+@Provider
+@Consumes(value = {MediaType.APPLICATION_JSON, "application/javascript"})
+@Produces(value = {MediaType.APPLICATION_JSON, "application/javascript"})
+public class JSON4JArrayProvider implements MessageBodyWriter<JSONArray>,
+ MessageBodyReader<JSONArray> {
+
+ public boolean isReadable(Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return JSONArray.class == clazz;
+ }
+
+ public JSONArray readFrom(Class<JSONArray> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType,
+ MultivaluedMap<String, String> headers,
+ InputStream is) throws IOException, WebApplicationException {
+ try {
+ return new JSONArray(new InputStreamReader(is, ProviderUtils.getCharset(mediaType)),
+ false);
+ } catch (JSONException e) {
+ throw new WebApplicationException(e, 400);
+ }
+ }
+
+ public long getSize(JSONArray obj,
+ Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return -1;
+ }
+
+ public boolean isWriteable(Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return JSONArray.class.isAssignableFrom(clazz);
+ }
+
+ public void writeTo(JSONArray arr,
+ Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType,
+ MultivaluedMap<String, Object> headers,
+ OutputStream os) throws IOException, WebApplicationException {
+
+ mediaType = MediaTypeUtils.setDefaultCharsetOnMediaTypeHeader(headers, mediaType);
+ OutputStreamWriter writer = new OutputStreamWriter(os, ProviderUtils.getCharset(mediaType));
+ try {
+ Writer json4jWriter = arr.write(writer);
+ json4jWriter.flush();
+ writer.flush();
+ } catch (JSONException e) {
+ throw new WebApplicationException(e);
+ }
+ }
+}
Added: incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JObjectProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JObjectProvider.java?rev=1025634&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JObjectProvider.java (added)
+++ incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/java/org/apache/wink/providers/json4j/JSON4JObjectProvider.java Wed Oct 20 16:37:06 2010
@@ -0,0 +1,102 @@
+/*
+ * 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.wink.providers.json4j;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+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.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.wink.common.internal.utils.MediaTypeUtils;
+import org.apache.wink.common.utils.ProviderUtils;
+import org.apache.wink.json4j.JSONException;
+import org.apache.wink.json4j.JSONObject;
+
+@Provider
+@Consumes(value = {MediaType.APPLICATION_JSON, "application/javascript"})
+@Produces(value = {MediaType.APPLICATION_JSON, "application/javascript"})
+public class JSON4JObjectProvider implements MessageBodyWriter<JSONObject>,
+ MessageBodyReader<JSONObject> {
+
+ public boolean isReadable(Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return JSONObject.class == clazz;
+ }
+
+ public JSONObject readFrom(Class<JSONObject> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType,
+ MultivaluedMap<String, String> headers,
+ InputStream is) throws IOException, WebApplicationException {
+ try {
+ return new JSONObject(new InputStreamReader(is, ProviderUtils.getCharset(mediaType)));
+ } catch (JSONException e) {
+ throw new WebApplicationException(e, 400);
+ }
+ }
+
+ public long getSize(JSONObject obj,
+ Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return -1;
+ }
+
+ public boolean isWriteable(Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return JSONObject.class.isAssignableFrom(clazz);
+ }
+
+ public void writeTo(JSONObject obj,
+ Class<?> clazz,
+ Type type,
+ Annotation[] annotations,
+ MediaType mediaType,
+ MultivaluedMap<String, Object> headers,
+ OutputStream os) throws IOException, WebApplicationException {
+ mediaType = MediaTypeUtils.setDefaultCharsetOnMediaTypeHeader(headers, mediaType);
+ OutputStreamWriter writer = new OutputStreamWriter(os, ProviderUtils.getCharset(mediaType));
+ try {
+ Writer json4jWriter = obj.write(writer);
+ json4jWriter.flush();
+ writer.flush();
+ } catch (JSONException e) {
+ throw new WebApplicationException(e, 500);
+ }
+ }
+}
Added: incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/resources/META-INF/wink-application
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/resources/META-INF/wink-application?rev=1025634&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/resources/META-INF/wink-application (added)
+++ incubator/wink/trunk/wink-providers/wink-json4j-provider/src/main/resources/META-INF/wink-application Wed Oct 20 16:37:06 2010
@@ -0,0 +1,2 @@
+org.apache.wink.providers.json4j.JSON4JArrayProvider
+org.apache.wink.providers.json4j.JSON4JObjectProvider
Added: incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/json/internal/Json4JProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/json/internal/Json4JProviderTest.java?rev=1025634&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/json/internal/Json4JProviderTest.java (added)
+++ incubator/wink/trunk/wink-providers/wink-json4j-provider/src/test/java/org/apache/wink/providers/json/internal/Json4JProviderTest.java Wed Oct 20 16:37:06 2010
@@ -0,0 +1,148 @@
+/*
+ * 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.wink.providers.json.internal;
+
+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;
+
+import org.apache.wink.json4j.JSONArray;
+import org.apache.wink.json4j.JSONException;
+import org.apache.wink.json4j.JSONObject;
+import org.apache.wink.providers.json4j.JSON4JArrayProvider;
+import org.apache.wink.providers.json4j.JSON4JObjectProvider;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class Json4JProviderTest extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class<?>[] {TestResource.class, JSON4JArrayProvider.class,
+ JSON4JObjectProvider.class};
+ }
+
+ private static final String JSON =
+ "{\"entry\": {\n" + " \"id\": \"entry:id\",\n"
+ + " \"title\": {\n"
+ + " \"content\": \"entry title\",\n"
+ + " \"type\": \"text\"\n"
+ + " }\n"
+ + "}}";
+
+ private static final String JSON_ARRAY = "[" + JSON + ", {\"test\":\"ing\"}]";
+
+ private void compairJsonContent(final String expected, final String actual)
+ throws JSONException {
+ JSONObject result = new JSONObject(actual);
+ JSONObject want = new JSONObject(expected);
+ assertEquals(result.toString(), want.toString());
+ }
+
+ @Path("test")
+ public static class TestResource {
+
+ @GET
+ @Path("json")
+ @Produces("application/json")
+ public JSONObject getJson() throws Exception {
+ 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"));
+ }
+ }
+
+ public void testGetJson() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/test/json", "application/json");
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ 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 = new JSONObject(response.getContentAsString());
+ JSONObject want = new JSONObject(JSON);
+ want.put("foo", "bar");
+ assertEquals(want.toString(), result.toString());
+ }
+
+ 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 StringReader(response.getContentAsString()));
+ JSONArray want = new JSONArray(JSON_ARRAY);
+ assertEquals(want.toString(), result.toString());
+ }
+
+ 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 StringReader(response.getContentAsString()));
+ JSONArray want = new JSONArray(JSON_ARRAY).put(Collections.singletonMap("foo", "bar"));
+ assertEquals(want.toString(), result.toString());
+ }
+}