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] Prototyping some code to support the invocation with the default method impl being invoked
Date Tue, 27 Jan 2015 17:19:57 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 319ff7490 -> f941e5b6e


[CXF-6200] Prototyping some code to support the invocation with the default method impl being
invoked


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

Branch: refs/heads/master
Commit: f941e5b6e30e0386bb50a5f7fe00baef8b2242d1
Parents: 319ff74
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Tue Jan 27 17:19:41 2015 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Tue Jan 27 17:19:41 2015 +0000

----------------------------------------------------------------------
 .../org/apache/cxf/jaxrs/model/Parameter.java   |  6 ++
 .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java  | 38 ++++++++----
 ...AXRSClientServerUserResourceDefaultTest.java | 62 +++++++++++++++++---
 3 files changed, 87 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/f941e5b6/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 d681a47..4e04680 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
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.jaxrs.model;
 
+import java.io.InputStream;
+
 
 public class Parameter {
     private ParameterType type;
@@ -89,4 +91,8 @@ public class Parameter {
     public void setDefaultValue(String dValue) {
         defaultValue = dValue;
     }
+    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;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/f941e5b6/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
index 2996973..8341e7c 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
@@ -85,6 +85,7 @@ import javax.xml.namespace.QName;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
+import org.apache.cxf.common.util.PropertyUtils;
 import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.DOMUtils;
@@ -161,7 +162,8 @@ public final class JAXRSUtils {
     private static final String PATH_SEGMENT_SEP = "/";
     private static final String REPORT_FAULT_MESSAGE_PROPERTY = "org.apache.cxf.jaxrs.report-fault-message";
     private static final String NO_CONTENT_EXCEPTION = "javax.ws.rs.core.NoContentException";
-    private static final String HTTP_CHARSET_PARAM = "charset"; 
+    private static final String HTTP_CHARSET_PARAM = "charset";
+    private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0]; 
     
     private JAXRSUtils() {        
     }
@@ -759,25 +761,37 @@ public final class JAXRSUtils {
                                                  Message message)
         throws IOException, WebApplicationException {
         
-        
-        
         Class<?>[] parameterTypes = ori.getInParameterTypes();
-        Parameter[] paramsInfo = ori.getParameters().toArray(new Parameter[ori.getParameters().size()]);
 
+        List<Parameter> paramsInfo = ori.getParameters();  
+        boolean preferModelParams = paramsInfo.size() > parameterTypes.length 
+            && PropertyUtils.isTrue(message.getContextualProperty("org.apache.cxf.preferModelParameters"));
+        
+        int parameterTypesLengh = preferModelParams ? paramsInfo.size() : parameterTypes.length;
         
         Type[] genericParameterTypes = ori.getInGenericParameterTypes();
         Annotation[][] anns = ori.getInParameterAnnotations();
-        List<Object> params = new ArrayList<Object>(parameterTypes.length);
+        List<Object> params = new ArrayList<Object>(parameterTypesLengh);
 
-        for (int i = 0; i < parameterTypes.length; i++) {
-            Class<?> param = parameterTypes[i]; 
-            Type genericParam = InjectionUtils.processGenericTypeIfNeeded(
-                ori.getClassResourceInfo().getServiceClass(), param, genericParameterTypes[i]);
-            param = InjectionUtils.updateParamClassToTypeIfNeeded(param, genericParam);
+        for (int i = 0; i < parameterTypesLengh; i++) {
+            Class<?> param = null;
+            Type genericParam = null;
+            Annotation[] paramAnns = null;
+            if (!preferModelParams) {
+                param = parameterTypes[i]; 
+                genericParam = InjectionUtils.processGenericTypeIfNeeded(
+                    ori.getClassResourceInfo().getServiceClass(), param, genericParameterTypes[i]);
+                param = InjectionUtils.updateParamClassToTypeIfNeeded(param, genericParam);
+                paramAnns = anns == null ? EMPTY_ANNOTATIONS : anns[i];
+            } else {
+                param = paramsInfo.get(i).getJavaType();
+                genericParam = param;
+                paramAnns = EMPTY_ANNOTATIONS;
+            }
             
             Object paramValue = processParameter(param, 
                                                  genericParam,
-                                                 anns == null ? new Annotation[0] : anns[i],
-                                                 paramsInfo[i], 
+                                                 paramAnns,
+                                                 paramsInfo.get(i), 
                                                  values, 
                                                  message,
                                                  ori);

http://git-wip-us.apache.org/repos/asf/cxf/blob/f941e5b6/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 c068886..eaabad5 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
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -28,6 +29,9 @@ import java.util.Map;
 import javax.ws.rs.HttpMethod;
 import javax.ws.rs.NotAllowedException;
 import javax.ws.rs.Path;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.PreMatching;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Request;
@@ -38,12 +42,19 @@ import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.httpclient.HttpClient;
 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.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.MessageContextImpl;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.jaxrs.model.Parameter;
 import org.apache.cxf.jaxrs.model.ParameterType;
 import org.apache.cxf.jaxrs.model.UserOperation;
 import org.apache.cxf.jaxrs.model.UserResource;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.MessageContentsList;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
 
@@ -59,11 +70,13 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
         org.apache.cxf.endpoint.Server server;
         protected void run() {
             JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
-            sf.setAddress("http://localhost:" + PORT + "/default");
+            sf.setInvoker(new CustomModelInvoker());
+            sf.setProvider(new PreMatchContainerRequestFilter());
+            sf.setAddress("http://localhost:" + PORT + "/");
             sf.getServiceFactory().setDefaultModelClass(DefaultResource.class);
             
             UserResource ur = new UserResource();
-            ur.setPath("/bookstoreNoAnnotations");
+            ur.setPath("/default");
             UserOperation op = new UserOperation();
             op.setPath("/books/{id}");
             op.setName("getBook");
@@ -108,14 +121,21 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
     }
     
     @Test
-    public void testGetBook123() throws Exception {
-        getAndCompare("http://localhost:" + PORT + "/default/bookstoreNoAnnotations/books/123",
-                      "application/xml", 200);
+    public void testGetBookInvokeService() throws Exception {
+        getAndCompare("http://localhost:" + PORT + "/default/books/123",
+                      "application/xml", 200, 123);
+    }
+    
+    @Test
+    public void testGetBookInvokerOnly() throws Exception {
+        getAndCompare("http://localhost:" + PORT + "/default/books/999",
+                      "application/xml", 200, 999);
     }
     
     private void getAndCompare(String address, 
                                String acceptType,
-                               int expectedStatus) throws Exception {
+                               int expectedStatus,
+                               long expectedId) throws Exception {
         GetMethod get = new GetMethod(address);
         get.setRequestHeader("Accept", acceptType);
         HttpClient httpClient = new HttpClient();
@@ -123,7 +143,7 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
             int result = httpClient.executeMethod(get);
             assertEquals(expectedStatus, result);
             Book book = readBook(get.getResponseBodyAsStream());
-            assertEquals(123, book.getId());
+            assertEquals(expectedId, book.getId());
             assertEquals("CXF in Action", book.getName());
         } finally {
             get.releaseConnection();
@@ -156,4 +176,32 @@ public class JAXRSClientServerUserResourceDefaultTest extends AbstractBusClientS
             }
         }
     }
+    
+    public static class CustomModelInvoker extends JAXRSInvoker {
+
+        @Override
+        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) {
+                Long bookId = Long.valueOf(params.get(0).toString());
+                Book book = new Book("CXF in Action", bookId);
+                Response r = Response.ok(book, 
+                                         mc.getHttpHeaders().getAcceptableMediaTypes().get(0)).build();
+                return new MessageContentsList(r);
+            } else { 
+                return super.invoke(exchange, request, resourceObject);
+            }
+            
+        }
+    }
+    @PreMatching
+    private static class PreMatchContainerRequestFilter implements ContainerRequestFilter
{
+        public void filter(ContainerRequestContext context) throws IOException {
+            if (context.getUriInfo().getRequestUri().toString().endsWith("999")) {
+                JAXRSUtils.getCurrentMessage().put("org.apache.cxf.preferModelParameters",
true);
+            }
+        }
+        
+    }
 }


Mime
View raw message