cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1072065 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/client/ test/java/org/apache/cxf/jaxrs/client/
Date Fri, 18 Feb 2011 17:08:17 GMT
Author: sergeyb
Date: Fri Feb 18 17:08:17 2011
New Revision: 1072065

URL: http://svn.apache.org/viewvc?rev=1072065&view=rev
Log:
CXF-3350: selecting the correct resource from the complex user model

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java?rev=1072065&r1=1072064&r2=1072065&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
Fri Feb 18 17:08:17 2011
@@ -184,7 +184,7 @@ public final class JAXRSClientFactory {
      */
     public static <T> T createFromModel(String baseAddress, Class<T> cls, String
modelRef, 
                                List<?> providers, String configLocation) {
-        JAXRSClientFactoryBean bean = WebClient.getBean(baseAddress, configLocation);
+        JAXRSClientFactoryBean bean = getBean(baseAddress, cls, configLocation);
         bean.setProviders(providers);
         bean.setModelRef(modelRef);
         return bean.create(cls);
@@ -201,7 +201,7 @@ public final class JAXRSClientFactory {
      */
     public static <T> T createFromModel(String baseAddress, Class<T> cls, String
modelRef, 
                                         List<?> providers, boolean threadSafe) {
-        JAXRSClientFactoryBean bean = WebClient.getBean(baseAddress, null);
+        JAXRSClientFactoryBean bean = getBean(baseAddress, cls, null);
         bean.setProviders(providers);
         bean.setModelRef(modelRef);
         if (threadSafe) {
@@ -232,7 +232,7 @@ public final class JAXRSClientFactory {
      */
     public static <T> T createFromModel(String baseAddress, Class<T> cls, List<UserResource>
modelBeans,
                                List<?> providers, String configLocation) {
-        JAXRSClientFactoryBean bean = WebClient.getBean(baseAddress, configLocation);
+        JAXRSClientFactoryBean bean = getBean(baseAddress, cls, configLocation);
         bean.setProviders(providers);
         bean.setModelBeans(modelBeans);
         return bean.create(cls);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java?rev=1072065&r1=1072064&r2=1072065&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
Fri Feb 18 17:08:17 2011
@@ -48,6 +48,7 @@ public class JAXRSClientFactoryBean exte
     private MultivaluedMap<String, String> headers;
     private ClientState initialState;
     private boolean threadSafe; 
+    private Class serviceClass;
     
     public JAXRSClientFactoryBean() {
         this(new JAXRSServiceFactoryBean());
@@ -123,7 +124,7 @@ public class JAXRSClientFactoryBean exte
      * Sets the resource class
      * @param cls the resource class
      */
-    public void setResourceClass(Class cls) {
+    public void setResourceClass(Class<?> cls) {
         setServiceClass(cls);
     }
     
@@ -131,11 +132,20 @@ public class JAXRSClientFactoryBean exte
      * Sets the resource class, may be called from a Spring handler 
      * @param cls the resource class
      */
-    public void setServiceClass(Class cls) {
+    public void setServiceClass(Class<?> cls) {
+        this.serviceClass = cls;
         serviceFactory.setResourceClass(cls);
     }
     
     /**
+     * Returns the service class 
+     * @param cls the service class
+     */
+    public Class<?> getServiceClass() {
+        return serviceClass;
+    }
+    
+    /**
      * Sets the headers new proxy or WebClient instances will be
      * initialized with.
      * 
@@ -228,7 +238,22 @@ public class JAXRSClientFactoryBean exte
         ClassResourceInfo cri = null;
         try {
             Endpoint ep = createEndpoint();
-            cri = serviceFactory.getClassResourceInfo().get(0);
+            if (getServiceClass() != null) {
+                for (ClassResourceInfo info : serviceFactory.getClassResourceInfo()) {
+                    if (info.getServiceClass().isAssignableFrom(getServiceClass())) {
+                        cri = info;
+                        break;
+                    }
+                }
+                if (cri == null) {
+                    // can not happen in the reality
+                    throw new RuntimeException("Service class " + getServiceClass().getName()
+                                               + " is not recognized");
+                }
+            } else {
+                cri = serviceFactory.getClassResourceInfo().get(0);
+            }
+            
             boolean isRoot = cri.getURITemplate() != null;
             ClientProxyImpl proxyImpl = null;
             ClientState actualState = getActualState();

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java?rev=1072065&r1=1072064&r2=1072065&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
Fri Feb 18 17:08:17 2011
@@ -20,12 +20,16 @@
 package org.apache.cxf.jaxrs.client;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.feature.AbstractFeature;
 import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.interceptor.InterceptorProvider;
+import org.apache.cxf.jaxrs.model.UserOperation;
+import org.apache.cxf.jaxrs.model.UserResource;
+import org.apache.cxf.jaxrs.resources.Book;
 import org.apache.cxf.jaxrs.resources.BookStore;
 import org.apache.cxf.jaxrs.resources.BookStoreSubresourcesOnly;
 import org.apache.cxf.message.Message;
@@ -48,6 +52,46 @@ public class JAXRSClientFactoryBeanTest 
     }
     
     @Test
+    public void testCreateClientWithUserResource() throws Exception {
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+        bean.setAddress("http://bar");
+        UserResource r = new UserResource();
+        r.setName(BookStore.class.getName());
+        r.setPath("/");
+        UserOperation op = new UserOperation();
+        op.setName("getDescription");
+        op.setVerb("GET");
+        r.setOperations(Collections.singletonList(op));
+        bean.setModelBeans(r);
+        assertTrue(bean.create() instanceof BookStore);
+    }
+    
+    @Test
+    public void testCreateClientWithTwoUserResources() throws Exception {
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+        bean.setAddress("http://bar");
+        UserResource r1 = new UserResource();
+        r1.setName(BookStore.class.getName());
+        r1.setPath("/store");
+        UserOperation op = new UserOperation();
+        op.setName("getDescription");
+        op.setVerb("GET");
+        r1.setOperations(Collections.singletonList(op));
+        
+        UserResource r2 = new UserResource();
+        r2.setName(Book.class.getName());
+        r2.setPath("/book");
+        UserOperation op2 = new UserOperation();
+        op2.setName("getName");
+        op2.setVerb("GET");
+        r2.setOperations(Collections.singletonList(op2));
+        
+        bean.setModelBeans(r1, r2);
+        bean.setServiceClass(Book.class);
+        assertTrue(bean.create() instanceof Book);
+    }
+    
+    @Test
     public void testGetConduit() throws Exception {
         JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
         bean.setAddress("http://bar");



Mime
View raw message