Author: jliu
Date: Wed Sep 19 06:39:43 2007
New Revision: 577301
URL: http://svn.apache.org/viewvc?rev=577301&view=rev
Log:
Select appropriate EntityProvider according to @ProduceMime annotation declared
on the resource method
Added:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cdjson.txt
(with props)
Modified:
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/model/OperationResourceInfo.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
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
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=577301&r1=577300&r2=577301&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
Wed Sep 19 06:39:43 2007
@@ -22,13 +22,13 @@
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;
import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.jaxrs.provider.ProviderFactoryImpl;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageContentsList;
@@ -78,17 +78,17 @@
targetType = ((List)responseObj).get(0).getClass();
}*/
+
+ //TODO: decide the output media type based on resource
method/resource class/provider
+ String[] methodMineTypes =
exchange.get(OperationResourceInfo.class).getProduceMimeTypes();
- EntityProvider provider =
ProviderFactory.getInstance().createEntityProvider(targetType);
+ EntityProvider provider =
((ProviderFactoryImpl)ProviderFactory.getInstance())
+ .createEntityProvider(targetType, methodMineTypes, false);
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();
+ if (!"*/*".equals(methodMineTypes[0])) {
+ message.put(Message.CONTENT_TYPE, methodMineTypes[0]);
}
- message.put(Message.CONTENT_TYPE, mineType[0]);
provider.writeTo(responseObj, null, out);
Modified:
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java?rev=577301&r1=577300&r2=577301&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
(original)
+++
incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
Wed Sep 19 06:39:43 2007
@@ -22,10 +22,9 @@
import java.lang.reflect.Method;
import java.util.List;
+import javax.ws.rs.ProduceMime;
import javax.ws.rs.ext.EntityProvider;
-import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
-
public class OperationResourceInfo {
private URITemplate uriTemplate;
private ClassResourceInfo classResourceInfo;
@@ -84,8 +83,23 @@
return entityProviderList;
}
- protected EntityProvider findEntityProvider() {
- return new JAXBElementProvider();
-
+ public String[] getProduceMimeTypes() {
+ //TODO:
+ /*
+ * These annotations MAY be applied to a resource class method, a
+ * resource class, or to an EntityProvider. Declarations on a resource
+ * class method override any on the resource class; declarations on an
+ * EntityProvider for a method argument or return type override those
on
+ * a resource class or resource method. In the absence of either of
+ * these annotations, support for any media type (¡°*¡±) is assumed.
+ */
+
+ String[] mineTypes = {"*/*"};
+ ProduceMime c = method.getAnnotation(ProduceMime.class);
+ if (c != null) {
+ mineTypes = c.value();
+ }
+
+ return mineTypes;
}
}
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=577301&r1=577300&r2=577301&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
Wed Sep 19 06:39:43 2007
@@ -25,6 +25,7 @@
import java.util.List;
import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.Response.Builder;
import javax.ws.rs.ext.EntityProvider;
import javax.ws.rs.ext.HeaderProvider;
@@ -64,6 +65,32 @@
}
@SuppressWarnings("unchecked")
+ public <T> EntityProvider<T> createEntityProvider(Class<T> type, String[]
requestedMineTypes,
+ boolean isConsumeMime) {
+
+ for (EntityProvider<T> ep : entityProviders) {
+ String[] supportedMimeTypes = {"*/*"};
+ if (isConsumeMime) {
+ ConsumeMime c = ep.getClass().getAnnotation(ConsumeMime.class);
+ if (c != null) {
+ supportedMimeTypes = c.value();
+ }
+ } else {
+ ProduceMime c = ep.getClass().getAnnotation(ProduceMime.class);
+ if (c != null) {
+ supportedMimeTypes = c.value();
+ }
+ }
+
+ if (matchMineTypes(supportedMimeTypes, requestedMineTypes) &&
ep.supports(type)) {
+ return ep;
+ }
+ }
+
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
public <T> HeaderProvider<T> createHeaderProvider(Class<T> type) {
for (HeaderProvider<T> hp : headerProviders) {
if (hp.supports(type)) {
@@ -72,6 +99,19 @@
}
return null;
+ }
+
+ private boolean matchMineTypes(String[] supportedMimeTypes, String[]
requestedMimeTypes) {
+ //TODO:
+ for (String supportedMimeType : supportedMimeTypes) {
+ for (String requestedMimeType : requestedMimeTypes) {
+ if (supportedMimeType.equals(requestedMimeType)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
}
/*
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=577301&r1=577300&r2=577301&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
Wed Sep 19 06:39:43 2007
@@ -83,21 +83,6 @@
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()) {
- return b;
- }
- }
-
- return null;
- }
-
@HttpMethod("POST")
@UriTemplate("/books")
public Response addBook(Book book) {
@@ -159,9 +144,14 @@
return r;
}
- @UriTemplate("/cds/{CDId}/")
- public CD getCD(@UriParam("CDId") String id) {
- System.out.println("----invoking getCD with cdId: " + id);
+ @HttpMethod("GET")
+ @UriTemplate("/cd/{CDId}/")
+ @ProduceMime("application/json")
+ //FIXME: getCDJSON and getCDs dont have to use different URLs, but we
seems have problem
+ //to match "/cds/" and "/cds/123" correctly using ".*?" as suggested by
spec. The former
+ //one's pattern is "/cds/(/)?" the later one is "/cds/(.*?)(/)?
+ public CD getCDJSON(@UriParam("CDId") String id) {
+ System.out.println("----invoking getCDJSON with cdId: " + id);
long idNumber = Long.parseLong(id);
for (CD b : cds) {
if (idNumber == b.getId()) {
@@ -179,6 +169,18 @@
CDs c = new CDs();
c.setCD(cds);
return c;
+ }
+
+ //FIXME: wont work if remove this method, has to set hasSubResource to true
+ @UriTemplate("/cds")
+ public Response addCD(CD cd) {
+/* System.out.println("----invoking addCD, cd name is: " +
cd.getName());
+ cd.setId(++cd);
+
+ cds.add(cd);
+
+ return Response.Builder.ok(book).build();*/
+ return null;
}
}
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=577301&r1=577300&r2=577301&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
Wed Sep 19 06:39:43 2007
@@ -33,7 +33,6 @@
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;
@@ -176,16 +175,16 @@
}
@Test
- @Ignore
- public void testGetCDsJSON() throws Exception {
+ public void testGetCDJSON() throws Exception {
String endpointAddress =
- "http://localhost:9080/xml/bookstore/cds";
+ "http://localhost:9080/xml/bookstore/cd/123";
URL url = new URL(endpointAddress);
InputStream in = url.openStream();
assertNotNull(in);
+ //System.out.println("---" + getStringFromInputStream(in));
InputStream expected = getClass()
- .getResourceAsStream("resources/expected_get_cds.txt");
+ .getResourceAsStream("resources/expected_get_cdjson.txt");
//System.out.println("---" + getStringFromInputStream(in));
assertEquals(getStringFromInputStream(expected),
getStringFromInputStream(in));
Added:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cdjson.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cdjson.txt?rev=577301&view=auto
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cdjson.txt
(added)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cdjson.txt
Wed Sep 19 06:39:43 2007
@@ -0,0 +1 @@
+{"CD":{"id":"123","name":"BOHEMIAN RHAPSODY"}}
\ No newline at end of file
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cdjson.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cdjson.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt?rev=577301&r1=577300&r2=577301&view=diff
==============================================================================
---
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
(original)
+++
incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt
Wed Sep 19 06:39:43 2007
@@ -1 +1 @@
-<CDs><CD><id>123</id><name>BOHEMIAN
RHAYSODY</name></CD><CD><id>124</id><name>BICYCLE RACE</name></CD></CDs>
\ No newline at end of file
+<CDs><CD><id>123</id><name>BOHEMIAN
RHAPSODY</name></CD><CD><id>124</id><name>BICYCLE RACE</name></CD></CDs>
\ No newline at end of file