cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6200] Initial support for setting custom model java types and having default methods used with regular methods
Date Wed, 28 Jan 2015 17:28:21 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 2783922f0 -> 93c7a9839


[CXF-6200] Initial support for setting custom model java types and having default methods
used with regular methods


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/93c7a983
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/93c7a983
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/93c7a983

Branch: refs/heads/3.0.x-fixes
Commit: 93c7a983914b27c2af3e614608abaef317b23a91
Parents: 2783922
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed Jan 28 17:26:57 2015 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed Jan 28 17:28:05 2015 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/JAXRSServiceFactoryBean.java      |  2 +-
 .../org/apache/cxf/jaxrs/model/Parameter.java   | 11 ++-
 .../apache/cxf/jaxrs/utils/ResourceUtils.java   | 88 +++++++++++---------
 .../src/main/resources/schemas/jaxrs-common.xsd |  1 +
 ...AXRSClientServerUserResourceDefaultTest.java | 23 ++++-
 5 files changed, 83 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/93c7a983/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceFactoryBean.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceFactoryBean.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceFactoryBean.java
index 6230e24..b674185 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceFactoryBean.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceFactoryBean.java
@@ -189,7 +189,7 @@ public class JAXRSServiceFactoryBean extends AbstractServiceFactoryBean
{
         Map<String, UserResource> map = userResourcesAsMap(resources);
         for (Class<?> sClass : sClasses) {
             ClassResourceInfo cri = ResourceUtils.createServiceClassResourceInfo(
-                map, map.get(sClass.getName()), sClass, false, true, enableStatic, getBus());
+                map, map.get(sClass.getName()), sClass, true, enableStatic, getBus());
             if (cri != null) {
                 classResourceInfos.add(cri);
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/93c7a983/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/Parameter.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/Parameter.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/Parameter.java
index 4e04680..d2fbe40 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/Parameter.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/Parameter.java
@@ -28,6 +28,7 @@ public class Parameter {
     private String aValue;
     private boolean isEncoded;
     private String defaultValue;
+    private Class<?> javaType;
     
     public Parameter() {
         
@@ -91,8 +92,14 @@ public class Parameter {
     public void setDefaultValue(String dValue) {
         defaultValue = dValue;
     }
+    public void setJavaType(Class<?> jType) {
+        this.javaType = jType;
+    }
     public Class<?> getJavaType() {
-        //TODO: support the conversion from "xs:int" or Java class names (DOMSource, etc)
-        return type == ParameterType.REQUEST_BODY ? InputStream.class : String.class;
+        if (javaType == null) {
+            return type == ParameterType.REQUEST_BODY ? InputStream.class : String.class;
+        } else {
+            return javaType;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/93c7a983/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
index c549936..f524f10 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
@@ -75,9 +75,11 @@ import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.ext.DefaultMethod;
 import org.apache.cxf.jaxrs.ext.xml.ElementClass;
 import org.apache.cxf.jaxrs.ext.xml.XMLName;
 import org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider;
@@ -188,13 +190,12 @@ public final class ResourceUtils {
         Bus bus) {
         final boolean isDefaultClass = defaultClass != null;
         Class<?> sClass = !isDefaultClass  ? loadClass(model.getName()) : defaultClass;
-        return createServiceClassResourceInfo(resources, model, sClass, isDefaultClass, 
-                                              isRoot, enableStatic, bus);
+        return createServiceClassResourceInfo(resources, model, sClass, isRoot, enableStatic,
bus);
     }
     
     public static ClassResourceInfo createServiceClassResourceInfo(
         Map<String, UserResource> resources, UserResource model, 
-        Class<?> sClass, boolean isDefaultClass, boolean isRoot, boolean enableStatic,
Bus bus) {
+        Class<?> sClass, boolean isRoot, boolean enableStatic, Bus bus) {
         if (model == null) {
             throw new RuntimeException("Resource class " + sClass.getName() + " has no model
info");
         }
@@ -203,47 +204,50 @@ public final class ResourceUtils {
                                   model.getConsumes(), model.getProduces(), bus);
         URITemplate t = URITemplate.createTemplate(model.getPath());
         cri.setURITemplate(t);
+        
         MethodDispatcher md = new MethodDispatcher();
         Map<String, UserOperation> ops = model.getOperationsAsMap();
-        if (!isDefaultClass) {
-            for (Method m : cri.getServiceClass().getMethods()) {
-                UserOperation op = ops.get(m.getName());
-                if (op == null || op.getName() == null) {
-                    continue;
-                }
-                OperationResourceInfo ori = 
-                    new OperationResourceInfo(m, cri, URITemplate.createTemplate(op.getPath()),
-                                              op.getVerb(), op.getConsumes(), op.getProduces(),
-                                              op.getParameters(),
-                                              op.isOneway());
-                String rClassName = m.getReturnType().getName();
-                if (op.getVerb() == null) {
-                    if (resources.containsKey(rClassName)) {
-                        ClassResourceInfo subCri = rClassName.equals(model.getName()) ? cri

-                            : createServiceClassResourceInfo(resources, resources.get(rClassName),
-                                                             m.getReturnType(), false, false,
enableStatic, bus);
-                        if (subCri != null) {
-                            cri.addSubClassResourceInfo(subCri);
-                            md.bind(ori, m);
-                        }
+        
+        Method defaultMethod = null;
+        Map<String, Method> methodNames = new HashMap<String, Method>();
+        for (Method m : cri.getServiceClass().getMethods()) {
+            if (m.getAnnotation(DefaultMethod.class) != null) {
+                // if needed we can also support multiple default methods
+                defaultMethod = m;
+            }
+            methodNames.put(m.getName(), m);
+        }
+        
+        for (Map.Entry<String, UserOperation> entry : ops.entrySet()) {
+            UserOperation op = entry.getValue();
+            Method actualMethod = methodNames.get(op.getName());
+            if (actualMethod == null) {
+                actualMethod = defaultMethod; 
+            }
+            if (actualMethod == null) {
+                continue;
+            }
+            OperationResourceInfo ori = 
+                new OperationResourceInfo(actualMethod, cri, URITemplate.createTemplate(op.getPath()),
+                                          op.getVerb(), op.getConsumes(), op.getProduces(),
+                                          op.getParameters(),
+                                          op.isOneway());
+            String rClassName = actualMethod.getReturnType().getName();
+            if (op.getVerb() == null) {
+                if (resources.containsKey(rClassName)) {
+                    ClassResourceInfo subCri = rClassName.equals(model.getName()) ? cri 
+                        : createServiceClassResourceInfo(resources, resources.get(rClassName),
+                                                         actualMethod.getReturnType(), false,
enableStatic, bus);
+                    if (subCri != null) {
+                        cri.addSubClassResourceInfo(subCri);
+                        md.bind(ori, actualMethod);
                     }
-                } else {
-                    md.bind(ori, m);
                 }
+            } else {
+                md.bind(ori, actualMethod);
             }
-        } else {
-            Method m = cri.getServiceClass().getMethods()[0];
-            for (Map.Entry<String, UserOperation> entry : ops.entrySet()) {
-                UserOperation op = entry.getValue();
-                OperationResourceInfo ori = 
-                    new OperationResourceInfo(m, cri, URITemplate.createTemplate(op.getPath()),
-                                              op.getVerb(), op.getConsumes(), op.getProduces(),
-                                              op.getParameters(),
-                                              op.isOneway());
-                md.bind(ori, m);
-            }
-            
         }
+        
         cri.setMethodDispatcher(md);
         return checkMethodDispatcher(cri) ? cri : null;
 
@@ -701,6 +705,14 @@ public final class ResourceUtils {
             Parameter p = new Parameter(paramEl.getAttribute("type"), i, paramEl.getAttribute("name"));
             p.setEncoded(Boolean.valueOf(paramEl.getAttribute("encoded")));
             p.setDefaultValue(paramEl.getAttribute("defaultValue"));
+            String pClass = paramEl.getAttribute("class");
+            if (!StringUtils.isEmpty(pClass)) {
+                try { 
+                    p.setJavaType(ClassLoaderUtils.loadClass(pClass, ResourceUtils.class));
+                } catch (Exception ex) {
+                    throw new RuntimeException(ex);
+                }
+            }
             params.add(p);
         }
         op.setParameters(params);

http://git-wip-us.apache.org/repos/asf/cxf/blob/93c7a983/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs-common.xsd
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs-common.xsd b/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs-common.xsd
index 8dbd6df..b42570e 100644
--- a/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs-common.xsd
+++ b/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs-common.xsd
@@ -60,6 +60,7 @@
                               </xsd:attribute>
                               <xsd:attribute name="defaultValue" type="xsd:string"/>
                               <xsd:attribute name="encoded" type="xsd:boolean"/>
+                              <xsd:attribute name="class" type="xsd:string"/>
                            </xsd:complexType>
                          </xsd:element>
                       </xsd:sequence>

http://git-wip-us.apache.org/repos/asf/cxf/blob/93c7a983/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerUserResourceDefaultTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerUserResourceDefaultTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerUserResourceDefaultTest.java
index eaabad5..259a114 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerUserResourceDefaultTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerUserResourceDefaultTest.java
@@ -45,6 +45,8 @@ import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxrs.JAXRSInvoker;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.ext.DefaultMethod;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.MessageContextImpl;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
@@ -83,8 +85,15 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
             op.setVerb("GET");
             op.setParameters(Collections.singletonList(new Parameter(ParameterType.PATH,
"id")));
             
+            UserOperation op2 = new UserOperation();
+            op2.setPath("echobook");
+            op2.setName("echo");
+            op2.setVerb("POST");
+            op2.setParameters(Collections.singletonList(new Parameter(ParameterType.REQUEST_BODY,
null)));
+            
             List<UserOperation> ops = new ArrayList<UserOperation>();
             ops.add(op);
+            ops.add(op2);
             
             ur.setOperations(ops);
             
@@ -132,6 +141,14 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
                       "application/xml", 200, 999);
     }
     
+    @Test
+    public void testEchoBook() throws Exception {
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/default/echobook");
+        Book b = wc.type("application/xml").accept("application/xml").post(new Book("echo",
333L), Book.class);
+        assertEquals("echo", b.getName());
+        assertEquals(333L, b.getId());
+    }
+    
     private void getAndCompare(String address, 
                                String acceptType,
                                int expectedStatus,
@@ -166,6 +183,7 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
         private HttpHeaders headers;
         private Map<String, Book> books = Collections.singletonMap("123", new Book("CXF
in Action", 123L));
         @Path("{a:.*}")
+        @DefaultMethod
         public Response handle() {
             if (HttpMethod.GET.equals(request.getMethod())) {
                 String id = ui.getPathParameters().getFirst("id");
@@ -175,6 +193,9 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
                 throw new NotAllowedException("GET");
             }
         }
+        public Book echo(Book book) {
+            return book;
+        }
     }
     
     public static class CustomModelInvoker extends JAXRSInvoker {
@@ -183,7 +204,7 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
         public Object invoke(Exchange exchange, Object request, Object resourceObject) {
             MessageContext mc = new MessageContextImpl(exchange.getInMessage());
             List<Object> params = CastUtils.cast((List<?>)request);
-            if (params.size() > 0) {
+            if (params.size() == 1 && "999".equals(params.get(0))) {
                 Long bookId = Long.valueOf(params.get(0).toString());
                 Book book = new Book("CXF in Action", bookId);
                 Response r = Response.ok(book, 


Mime
View raw message