Author: jliu
Date: Tue Sep 18 05:37:56 2007
New Revision: 576867
URL: http://svn.apache.org/viewvc?rev=576867&view=rev
Log:
JSON support
Added:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(with props)
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Modified: incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml?rev=576867&r1=576866&r2=576867&view=diff
==============================================================================
--- incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml (original)
+++ incubator/cxf/branches/jliu/rt/frontend/jaxrs/pom.xml Tue Sep 18 05:37:56
2007
@@ -77,6 +77,12 @@
<artifactId>geronimo-servlet_2.5_spec</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.codehaus.jettison</groupId>
+ <artifactId>jettison</artifactId>
+ <version>1.0-RC2</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-testutils</artifactId>
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=576867&r1=576866&r2=576867&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
(original)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
Tue Sep 18 05:37:56 2007
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.OutputStream;
+import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.EntityProvider;
import javax.ws.rs.ext.ProviderFactory;
@@ -81,7 +82,16 @@
EntityProvider provider =
ProviderFactory.getInstance().createEntityProvider(targetType);
try {
+ //TODO: decide the output media type based on resource
method/resource class/provider
+ ProduceMime c =
provider.getClass().getAnnotation(ProduceMime.class);
+ String[] mineType = {"*/*"};
+ if (c != null) {
+ mineType = c.value();
+ }
+ message.put(Message.CONTENT_TYPE, mineType[0]);
+
provider.writeTo(responseObj, null, out);
+
} catch (IOException e) {
e.printStackTrace();
}
Added:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=576867&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(added)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Tue Sep 18 05:37:56 2007
@@ -0,0 +1,98 @@
+/**
+ * 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.cxf.jaxrs.provider;
+
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.ProduceMime;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.EntityProvider;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
+
[EMAIL PROTECTED]("application/json")
[EMAIL PROTECTED]("application/json")
+public final class JSONProvider implements EntityProvider<Object> {
+
+ static Map<Class, JAXBContext> jaxbContexts = new WeakHashMap<Class,
JAXBContext>();
+
+ public boolean supports(Class<?> type) {
+ return type.getAnnotation(XmlRootElement.class) != null;
+ }
+
+ public Object readFrom(Class<Object> type, String mediaType,
MultivaluedMap<String, String> headers,
+ InputStream is) {
+ try {
+ JAXBContext context = getJAXBContext(type);
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+ return unmarshaller.unmarshal(is);
+ } catch (JAXBException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+ public void writeTo(Object obj, MultivaluedMap<String, Object> headers,
OutputStream os) {
+ try {
+ JAXBContext context = getJAXBContext(obj.getClass());
+ Marshaller marshaller = context.createMarshaller();
+ //marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
+
+ // Set up the JSON StAX implementation
+ Map<String, String> nstojns = new HashMap<String, String>();
+
+ XMLOutputFactory factory = new MappedXMLOutputFactory(nstojns);
+ XMLStreamWriter xsw = factory.createXMLStreamWriter(os);
+ marshaller.marshal(obj, xsw);
+ xsw.close();
+
+ } catch (JAXBException e) {
+ e.printStackTrace();
+ } catch (XMLStreamException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private JAXBContext getJAXBContext(Class type) throws JAXBException {
+ synchronized (jaxbContexts) {
+ JAXBContext context = jaxbContexts.get(type);
+ if (context == null) {
+ context = JAXBContext.newInstance(type);
+ jaxbContexts.put(type, context);
+ }
+ return context;
+ }
+ }
+}
Propchange:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java?rev=576867&r1=576866&r2=576867&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
(original)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
Tue Sep 18 05:37:56 2007
@@ -20,8 +20,11 @@
package org.apache.cxf.jaxrs.provider;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
+import javax.ws.rs.ConsumeMime;
import javax.ws.rs.core.Response.Builder;
import javax.ws.rs.ext.EntityProvider;
import javax.ws.rs.ext.HeaderProvider;
@@ -37,7 +40,9 @@
public ProviderFactoryImpl() {
//TODO: search for EntityProviders from classpath or config file.
entityProviders.add(new JAXBElementProvider());
- //sort();
+ entityProviders.add(new JSONProvider());
+
+ sort();
}
public <T> T createInstance(Class<T> type) {
@@ -75,8 +80,33 @@
* explicitly lists a media types is sorted before a provider that lists
*.
* Quality parameter values are also used such that x/y;q=1.0 < x/y;q=0.7.
*/
- protected void sort() {
+ private void sort() {
+ Collections.sort(entityProviders, new EntityProviderComparator());
+ }
+
+
+ private static class EntityProviderComparator implements
Comparator<EntityProvider> {
+ public int compare(EntityProvider e1, EntityProvider e2) {
+ ConsumeMime c = e1.getClass().getAnnotation(ConsumeMime.class);
+ String[] mineType1 = {"*/*"};
+ if (c != null) {
+ mineType1 = c.value();
+ }
+
+ ConsumeMime c2 = e2.getClass().getAnnotation(ConsumeMime.class);
+ String[] mineType2 = {"*/*"};
+ if (c2 != null) {
+ mineType2 = c2.value();
+ }
+
+ return compareString(mineType1[0], mineType2[0]);
+
+ }
+ private int compareString(String str1, String str2) {
+ //TODO:
+ return str2.compareTo(str1);
+ }
}
}
Modified:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=576867&r1=576866&r2=576867&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Tue Sep 18 05:37:56 2007
@@ -26,6 +26,7 @@
import javax.ws.rs.HttpMethod;
+import javax.ws.rs.ProduceMime;
import javax.ws.rs.UriParam;
import javax.ws.rs.UriTemplate;
import javax.ws.rs.core.HttpContext;
@@ -72,6 +73,21 @@
@UriTemplate("/books/{bookId}/")
public Book getBook(@UriParam("bookId") String id) {
System.out.println("----invoking getBook with cdId: " + id);
+ long idNumber = Long.parseLong(id);
+ for (Book b : books) {
+ if (idNumber == b.getId()) {
+ return b;
+ }
+ }
+
+ return null;
+ }
+
+ @HttpMethod("GET")
+ @UriTemplate("/books/{bookId}/")
+ @ProduceMime("application/json")
+ public Book getBookJSON(@UriParam("bookId") String id) {
+ System.out.println("----invoking getBookJSON with cdId: " + id);
long idNumber = Long.parseLong(id);
for (Book b : books) {
if (idNumber == b.getId()) {
Modified:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=576867&r1=576866&r2=576867&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Tue Sep 18 05:37:56 2007
@@ -33,6 +33,7 @@
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.junit.BeforeClass;
+import org.junit.Ignore;
import org.junit.Test;
@@ -158,10 +159,25 @@
post.releaseConnection();
}
}
-
-
+
@Test
public void testGetCDs() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/xml/bookstore/cds";
+ URL url = new URL(endpointAddress);
+ InputStream in = url.openStream();
+ assertNotNull(in);
+
+ InputStream expected = getClass()
+ .getResourceAsStream("resources/expected_get_cds.txt");
+
+ //System.out.println("---" + getStringFromInputStream(in));
+ assertEquals(getStringFromInputStream(expected),
getStringFromInputStream(in));
+ }
+
+ @Test
+ @Ignore
+ public void testGetCDsJSON() throws Exception {
String endpointAddress =
"http://localhost:9080/xml/bookstore/cds";
URL url = new URL(endpointAddress);