Author: jliu
Date: Mon Dec 24 08:20:27 2007
New Revision: 606708
URL: http://svn.apache.org/viewvc?rev=606708&view=rev
Log:
Read object from JSON.
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_json.txt
(with props)
Modified:
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Modified:
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
Mon Dec 24 08:20:27 2007
@@ -20,7 +20,6 @@
package org.apache.cxf.jaxrs;
-import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
@@ -78,20 +77,18 @@
Map<String, String> values = new HashMap<String, String>();
Message msg = exchange.getInMessage();
- String subResourcePath =
(String)msg.get(JAXRSInInterceptor.SUBRESOURCE_PATH);
+ String subResourcePath =
(String)msg.get(JAXRSInInterceptor.RELATIVE_PATH);
String httpMethod = (String)msg.get(Message.HTTP_REQUEST_METHOD);
ClassResourceInfo subCri =
JAXRSUtils.findSubResourceClass(classResourceInfo, result.getClass());
OperationResourceInfo subOri = JAXRSUtils.findTargetMethod(subCri,
subResourcePath,
httpMethod, values);
exchange.put(OperationResourceInfo.class, subOri);
-
+ msg.put(JAXRSInInterceptor.RELATIVE_PATH, subResourcePath);
// work out request parameters for the sub-resouce class. Here we
// presume Inputstream has not been consumed yet by the root
resource class.
//I.e., only one place either in the root resource or sub-resouce
class can
//have a parameter that read from entitybody.
- InputStream is = msg.getContent(InputStream.class);
- List<Object> newParams =
JAXRSUtils.processParameters(subOri.getMethod(), subResourcePath,
-
httpMethod, values, is);
+ List<Object> newParams =
JAXRSUtils.processParameters(subOri.getMethod(), values, msg);
msg.setContent(List.class, newParams);
return this.invoke(exchange, newParams);
Modified:
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
Mon Dec 24 08:20:27 2007
@@ -28,6 +28,8 @@
import java.util.List;
import java.util.Map;
+import javax.activation.MimeType;
+import javax.activation.MimeTypeParseException;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.QueryParam;
@@ -37,9 +39,12 @@
import javax.ws.rs.ext.ProviderFactory;
import org.apache.cxf.common.util.PrimitiveUtils;
+import org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.model.URITemplate;
+import org.apache.cxf.jaxrs.provider.ProviderFactoryImpl;
+import org.apache.cxf.message.Message;
public final class JAXRSUtils {
@@ -87,22 +92,17 @@
return null;
}
- public static List<Object> processParameters(Method method, String path,
String httpMethod,
- Map<String, String> values,
InputStream is) {
+ //Message contains following information: PATH, HTTP_REQUEST_METHOD,
CONTENT_TYPE, InputStream.
+ public static List<Object> processParameters(Method method, Map<String,
String> values, Message message) {
Class[] parameterTypes = method.getParameterTypes();
Type[] genericParameterTypes = method.getGenericParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
- boolean readFromEntityBody = false;
- if ("PUT".equals(httpMethod) || "POST".equals(httpMethod)) {
- readFromEntityBody = true;
- }
-
List<Object> params = new ArrayList<Object>(parameterTypes.length);
for (int i = 0; i < parameterTypes.length; i++) {
Object param = processParameter(parameterTypes[i],
genericParameterTypes[i],
- parameterAnnotations[i],
readFromEntityBody, path, values, is);
+ parameterAnnotations[i], values,
message);
params.add(param);
}
@@ -111,27 +111,36 @@
}
private static Object processParameter(Class<?> parameterClass, Type
parameterType,
- Annotation[] parameterAnnotations, boolean
readFromEntityBody,
- String path, Map<String, String> values,
InputStream is) {
- Object result = null;
- if (parameterAnnotations == null || parameterAnnotations.length == 0) {
- if (readFromEntityBody) {
- result = readFromEntityBody(parameterClass, is);
+ Annotation[] parameterAnnotations,
Map<String, String> values,
+ Message message) {
+ InputStream is = message.getContent(InputStream.class);
+ String contentTypes = (String)message.get(Message.CONTENT_TYPE);
+ if (contentTypes != null) {
+ try {
+ MimeType mt = new MimeType(contentTypes);
+ contentTypes = mt.getBaseType();
+ } catch (MimeTypeParseException e) {
+ // ignore
}
- return result;
- }
-
- Annotation annotation = parameterAnnotations[0];
- if (annotation.annotationType() == UriParam.class) {
- result = readFromUriParam((UriParam)annotation, parameterClass,
parameterType,
+ }
+ String path = (String)message.get(JAXRSInInterceptor.RELATIVE_PATH);
+ String httpMethod = (String)message.get(Message.HTTP_REQUEST_METHOD);
+
+ Object result = null;
+
+ if ((parameterAnnotations == null || parameterAnnotations.length == 0)
+ && ("PUT".equals(httpMethod) || "POST".equals(httpMethod))) {
+ result = readFromEntityBody(parameterClass, is, contentTypes);
+ } else if (parameterAnnotations[0].annotationType() == UriParam.class)
{
+ result = readFromUriParam((UriParam)parameterAnnotations[0],
parameterClass, parameterType,
parameterAnnotations, path, values);
- } else if (annotation.annotationType() == QueryParam.class) {
+ } else if (parameterAnnotations[0].annotationType() ==
QueryParam.class) {
//TODO
- } else if (annotation.annotationType() == MatrixParam.class) {
+ } else if (parameterAnnotations[0].annotationType() ==
MatrixParam.class) {
//TODO
- } else if (annotation.annotationType() == HeaderParam.class) {
+ } else if (parameterAnnotations[0].annotationType() ==
HeaderParam.class) {
//TODO
- } else if (annotation.annotationType() == HttpContext.class) {
+ } else if (parameterAnnotations[0].annotationType() ==
HttpContext.class) {
//TODO
}
@@ -139,9 +148,11 @@
}
@SuppressWarnings("unchecked")
- private static Object readFromEntityBody(Class targetTypeClass,
InputStream is) {
+ private static Object readFromEntityBody(Class targetTypeClass,
InputStream is, String contentTypes) {
Object result = null;
- EntityProvider provider =
ProviderFactory.getInstance().createEntityProvider(targetTypeClass);
+ //Refactor once we move to JSR-311 0.5 API
+ EntityProvider provider =
((ProviderFactoryImpl)ProviderFactory.getInstance())
+ .createEntityProvider(targetTypeClass, new String[]{contentTypes},
true);
try {
result = provider.readFrom(targetTypeClass, null, null, is);
Modified:
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
Mon Dec 24 08:20:27 2007
@@ -19,7 +19,6 @@
package org.apache.cxf.jaxrs.interceptor;
-import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -37,7 +36,6 @@
public class JAXRSInInterceptor extends AbstractPhaseInterceptor<Message> {
public static final String RELATIVE_PATH = "relative.path";
- public static final String SUBRESOURCE_PATH = "subresource.path";
//private static final Logger LOG =
Logger.getLogger(RESTDispatchInterceptor.class.getName());
//private static final ResourceBundle BUNDLE =
BundleUtils.getBundle(RESTDispatchInterceptor.class);
@@ -50,7 +48,7 @@
String path = (String)message.get(Message.PATH_INFO);
String address = (String)message.get(Message.BASE_PATH);
String httpMethod = (String)message.get(Message.HTTP_REQUEST_METHOD);
-
+
if (address.startsWith("http")) {
int idx = address.indexOf('/', 7);
if (idx != -1) {
@@ -82,14 +80,12 @@
//throw new Fault(new org.apache.cxf.common.i18n.Message("NO_OP",
BUNDLE, method, path));
}
message.getExchange().put(OperationResourceInfo.class, ori);
- message.put(SUBRESOURCE_PATH,
values.get(URITemplate.RIGHT_HAND_VALUE));
+ message.put(RELATIVE_PATH, values.get(URITemplate.RIGHT_HAND_VALUE));
//2. Process parameters
- InputStream is = message.getContent(InputStream.class);
- List<Object> params = JAXRSUtils.processParameters(ori.getMethod(),
path, httpMethod, values, is);
+ List<Object> params = JAXRSUtils
+ .processParameters(ori.getMethod(), values, message);
message.setContent(List.class, params);
-
}
-
}
Modified:
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Mon Dec 24 08:20:27 2007
@@ -38,8 +38,10 @@
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
+import org.codehaus.jettison.mapped.MappedXMLInputFactory;
import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
@ProduceMime("application/json")
@@ -57,9 +59,18 @@
try {
JAXBContext context = getJAXBContext(type);
Unmarshaller unmarshaller = context.createUnmarshaller();
- return unmarshaller.unmarshal(is);
+
+ Map<String, String> nstojns = new HashMap<String, String>();
+
+ MappedXMLInputFactory factory = new MappedXMLInputFactory(nstojns);
+ XMLStreamReader xsw = factory.createXMLStreamReader(is);
+ Object obj = unmarshaller.unmarshal(xsw);
+ xsw.close();
+ return obj;
} catch (JAXBException e) {
e.printStackTrace();
+ } catch (XMLStreamException e) {
+ e.printStackTrace();
}
return null;
Modified:
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
Mon Dec 24 08:20:27 2007
@@ -66,7 +66,6 @@
}
@Test
- @org.junit.Ignore("random failures")
public void testFindTargetResourceClassWithSubResource() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStore.class);
Modified:
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
(original)
+++
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
Mon Dec 24 08:20:27 2007
@@ -78,6 +78,14 @@
EntityProvider provider =
((ProviderFactoryImpl)ProviderFactory.getInstance())
.createEntityProvider(String.class, methodMimeTypes, false);
assertTrue(provider instanceof TestStringProvider);
+ }
+
+ @Test
+ public void testGetJSONProviderConsumeMime() throws Exception {
+ String[] methodMimeTypes = {"application/json"};
+ EntityProvider provider =
((ProviderFactoryImpl)ProviderFactory.getInstance())
+ .createEntityProvider(org.apache.cxf.jaxrs.resources.Book.class,
methodMimeTypes, true);
+ assertTrue(provider instanceof JSONProvider);
}
private int indexOf(List<EntityProvider> providers, Class providerType) {
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Mon Dec 24 08:20:27 2007
@@ -124,6 +124,23 @@
XMLUtils.printDOM(ds.getNode());
return ds;
}
+
+ @HttpMethod("PUT")
+ @UriTemplate("/bookswithjson/")
+ public Response updateBookJSON(Book book) {
+ System.out.println("----invoking updateBook, book name is: " +
book.getName());
+ Book b = books.get(book.getId());
+
+ Response r;
+ if (b != null) {
+ books.put(book.getId(), book);
+ r = Response.Builder.ok().build();
+ } else {
+ r = Response.Builder.notModified().build();
+ }
+
+ return r;
+ }
@HttpMethod("DELETE")
@UriTemplate("/books/{bookId}/")
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=606708&r1=606707&r2=606708&view=diff
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Mon Dec 24 08:20:27 2007
@@ -205,6 +205,54 @@
}
@Test
+ public void testUpdateBookWithJSON() throws Exception {
+ String endpointAddress =
"http://localhost:9080/bookstore/bookswithjson";
+
+ String inputFile =
getClass().getResource("resources/update_book_json.txt").getFile();
+ File input = new File(inputFile);
+ PutMethod put = new PutMethod(endpointAddress);
+ RequestEntity entity = new FileRequestEntity(input, "application/json;
charset=ISO-8859-1");
+ put.setRequestEntity(entity);
+ HttpClient httpclient = new HttpClient();
+
+ try {
+ int result = httpclient.executeMethod(put);
+ assertEquals(200, result);
+ } finally {
+ // Release current connection to the connection pool once you are
+ // done
+ put.releaseConnection();
+ }
+
+ // Verify result
+ endpointAddress = "http://localhost:9080/bookstore/books/123";
+ URL url = new URL(endpointAddress);
+ InputStream in = url.openStream();
+ assertNotNull(in);
+
+ InputStream expected =
getClass().getResourceAsStream("resources/expected_update_book.txt");
+
+ assertEquals(getStringFromInputStream(expected),
getStringFromInputStream(in));
+
+ // Roll back changes:
+ String inputFile1 =
getClass().getResource("resources/expected_get_book123.txt").getFile();
+ File input1 = new File(inputFile1);
+ PutMethod put1 = new PutMethod(endpointAddress);
+ RequestEntity entity1 = new FileRequestEntity(input1, "text/xml;
charset=ISO-8859-1");
+ put1.setRequestEntity(entity1);
+ HttpClient httpclient1 = new HttpClient();
+
+ try {
+ int result = httpclient1.executeMethod(put);
+ assertEquals(200, result);
+ } finally {
+ // Release current connection to the connection pool once you are
+ // done
+ put1.releaseConnection();
+ }
+ }
+
+ @Test
public void testUpdateBookFailed() throws Exception {
String endpointAddress =
"http://localhost:9080/bookstore/books";
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_json.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_json.txt?rev=606708&view=auto
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_json.txt
(added)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_json.txt
Mon Dec 24 08:20:27 2007
@@ -0,0 +1 @@
+{"Book":{"id":"123","name":"CXF in Action - 3"}}
\ No newline at end of file
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_json.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/update_book_json.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain