camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r790870 - in /camel/trunk/components/camel-cxf/src: main/java/org/apache/camel/component/cxf/ main/java/org/apache/camel/component/cxf/jaxrs/ test/java/org/apache/camel/component/cxf/cxfbean/ test/java/org/apache/camel/component/cxf/jaxrs/ ...
Date Fri, 03 Jul 2009 11:45:42 GMT
Author: ningjiang
Date: Fri Jul  3 11:45:41 2009
New Revision: 790870

URL: http://svn.apache.org/viewvc?rev=790870&view=rev
Log:
CAMEL_1659 fixed the SubResourceLocation issue on the CxfRsProducer

Modified:
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConstants.java
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/cxfbean/CxfBeanTest.java
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsRouterTest.java
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerService.java
    camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConstants.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConstants.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConstants.java
(original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConstants.java
Fri Jul  3 11:45:41 2009
@@ -45,6 +45,8 @@
     String CAMEL_CXF_RS_USING_HTTP_API = "CamelCxfRsUsingHttpAPI";
     String CAMEL_CXF_RS_VAR_VALUES = "CamelCxfRsVarValues";
     String CAMEL_CXF_RS_RESPONSE_CLASS = "CamelCxfRsResponseClass";
+    String CAMEL_CXF_RS_METHOD = "CamelCxfRsMethod";
+    String CAMEL_CXF_RS_OPERATION_RESOURCE_INFO_STACK = "CamelCxfRsOperationResourceInfoStack";
 }
 
 

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
(original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
Fri Jul  3 11:45:41 2009
@@ -17,6 +17,8 @@
 package org.apache.camel.component.cxf.jaxrs;
 
 import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.logging.Level;
 
 import javax.ws.rs.WebApplicationException;
 
@@ -24,6 +26,7 @@
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.cxf.jaxrs.JAXRSInvoker;
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
 import org.apache.cxf.message.Exchange;
 
 public class CxfRsInvoker extends JAXRSInvoker {
@@ -39,7 +42,12 @@
     protected Object performInvocation(Exchange cxfExchange, final Object serviceObject,
Method method,
                                        Object[] paramArray) throws Exception {
         paramArray = insertExchange(method, paramArray, cxfExchange);
-        
+        OperationResourceInfo ori = cxfExchange.get(OperationResourceInfo.class);       

+        if (ori.isSubResourceLocator()) {
+            // don't delegate the sub resource locator call to camel processor
+            return method.invoke(serviceObject, paramArray);
+        }
+       
         ExchangePattern ep = ExchangePattern.InOut;
         if (method.getReturnType() == Void.class) {
             ep = ExchangePattern.InOnly;

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
(original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
Fri Jul  3 11:45:41 2009
@@ -23,7 +23,6 @@
 
 import javax.ws.rs.core.Response;
 
-import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.component.cxf.CxfConstants;
@@ -32,6 +31,7 @@
 import org.apache.cxf.jaxrs.client.Client;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
 import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.message.MessageContentsList;
 
 /**
  * CxfRsProducer binds a Camel exchange to a CXF exchange, acts as a CXF 
@@ -58,26 +58,41 @@
     }
     
     @SuppressWarnings("unchecked")
-    private void invokeHttpClient(Exchange exchange) {
+    protected void invokeHttpClient(Exchange exchange) {
         Message inMessage = exchange.getIn();       
         WebClient client = cfb.createWebClient();
         String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class); 
-        Class responseClass = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS,
Class.class);
-        String path = inMessage.getHeader(Exchange.HTTP_RELATIVE_PATH, String.class);
-        client.path(path);
-        Object body = inMessage.getBody();
-        Object response = null;
+        Class responseClass = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS,
Class.class);        
+        String path = inMessage.getHeader(Exchange.HTTP_PATH, String.class);
+       
+        if (path != null) {
+            client.path(path);
+        } 
+        Object body = null;
+        if (!"GET".equals(httpMethod)) {
+            // need to check the request object
+            body = checkRequestObject(inMessage.getBody());            
+        }
+        
+        /*String acceptContentType = inMessage.getHeader(Exchange.ACCEPT_CONTENT_TYPE, String.class);
+        if (acceptContentType != null) {            
+            client.accept(acceptContentType);            
+        }*/
+        Object response = null;        
         if (responseClass == null) {
             response = client.invoke(httpMethod, body, InputStream.class);
+        } else if (responseClass.equals(Response.class)) {
+            response = client.invoke(httpMethod, body);
         } else {
             response = client.invoke(httpMethod, body, responseClass);
         }
+       
         if (exchange.getPattern().isOutCapable()) {
             exchange.getOut().setBody(response);
         }
     }
 
-    private void invokeProxyClient(Exchange exchange) throws Exception {
+    protected void invokeProxyClient(Exchange exchange) throws Exception {
         Message inMessage = exchange.getIn();
         Object[] varValues = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, Object[].class);
         String methodName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class);
@@ -94,6 +109,7 @@
         // get the method
         Method method = findRightMethod(sfb.getResourceClasses(), methodName, getParameterTypes(parameters));
         // Will send out the message to
+        // Need to deal with the sub resource class
         Object response = method.invoke(target, parameters);
         if (exchange.getPattern().isOutCapable()) {
             exchange.getOut().setBody(response);
@@ -118,6 +134,19 @@
             + "withe these parameter " + arrayToString(parameterTypes));
     }
     
+    private Object checkRequestObject(Object request) {
+        if (request != null) {
+            if (request instanceof MessageContentsList) {
+                request = ((MessageContentsList)request).get(0);
+            } else if (request instanceof List) {
+                request = ((List)request).get(0);
+            } else if (request.getClass().isArray()) {
+                request = ((Object[])request)[0];
+            }
+        }
+        return request;
+    }
+    
     private Class[] getParameterTypes(Object[] objects) {
         Class[] answer = new Class[objects.length];
         int i = 0;

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
(original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
Fri Jul  3 11:45:41 2009
@@ -18,12 +18,23 @@
 package org.apache.camel.component.cxf.jaxrs;
 
 import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.component.cxf.CxfConstants;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.spi.HeaderFilterStrategyAware;
+import org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor;
+import org.apache.cxf.jaxrs.model.MethodInvocationInfo;
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.jaxrs.model.OperationResourceInfoStack;
+import org.apache.cxf.jaxrs.model.URITemplate;
 
 public class DefaultCxfRsBinding implements CxfRsBinding, HeaderFilterStrategyAware {
     private HeaderFilterStrategy headerFilterStrategy;
@@ -33,6 +44,7 @@
         if (camelExchange.isFailed()) {
             throw camelExchange.getException();
         }
+        
         return camelExchange.getOut().getBody();
     }
 
@@ -44,6 +56,8 @@
         org.apache.cxf.message.Message cxfMessage = cxfExchange.getInMessage();
         
         copyMessageHeader(cxfMessage, camelMessage, org.apache.cxf.message.Message.REQUEST_URI,
Exchange.HTTP_URI);
+                
+        copyMessageHeader(cxfMessage, camelMessage, JAXRSInInterceptor.RELATIVE_PATH, Exchange.HTTP_RELATIVE_PATH);
         
         copyMessageHeader(cxfMessage, camelMessage, org.apache.cxf.message.Message.HTTP_REQUEST_METHOD,
Exchange.HTTP_METHOD);
         
@@ -56,8 +70,15 @@
         copyMessageHeader(cxfMessage, camelMessage, org.apache.cxf.message.Message.QUERY_STRING,
Exchange.HTTP_QUERY);
         
         copyMessageHeader(cxfMessage, camelMessage, org.apache.cxf.message.Message.ACCEPT_CONTENT_TYPE,
Exchange.ACCEPT_CONTENT_TYPE);
-                
+        
+        camelMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, method.getReturnType());
+               
+        copyOperationResourceInfoStack(cxfMessage, camelMessage);
+        
         camelMessage.setHeader(CxfConstants.OPERATION_NAME, method.getName());
+        
+        camelMessage.setHeader(CxfConstants.CAMEL_CXF_RS_METHOD, method);
+        
         camelMessage.setBody(paramArray);        
     }
 
@@ -70,10 +91,21 @@
         headerFilterStrategy = strategy;        
     }
     
-    private void copyMessageHeader(org.apache.cxf.message.Message cxfMessage, Message camelMessage,
String cxfKey, String camelKey) {
+    protected void copyMessageHeader(org.apache.cxf.message.Message cxfMessage, Message camelMessage,
String cxfKey, String camelKey) {
         if (cxfMessage.get(cxfKey) != null) {
             camelMessage.setHeader(camelKey, cxfMessage.get(cxfKey));
         }
     }
+    
+    protected void copyOperationResourceInfoStack(org.apache.cxf.message.Message cxfMessage,
Message camelMessage) {
+        OperationResourceInfoStack stack = cxfMessage.get(OperationResourceInfoStack.class);
+        if (stack != null) {
+            // make a copy of the operation resource info for looking up the sub resource
location
+            OperationResourceInfoStack copyStack = (OperationResourceInfoStack)stack.clone();
+            camelMessage.setHeader(CxfConstants.CAMEL_CXF_RS_OPERATION_RESOURCE_INFO_STACK,
copyStack);
+                        
+        }
+      
+    }
 
-}
+}
\ No newline at end of file

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/cxfbean/CxfBeanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/cxfbean/CxfBeanTest.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/cxfbean/CxfBeanTest.java
(original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/cxfbean/CxfBeanTest.java
Fri Jul  3 11:45:41 2009
@@ -127,7 +127,7 @@
         HttpClient httpclient = new HttpClient();
 
         try {
-            assertEquals(301, httpclient.executeMethod(post));
+            assertEquals(201, httpclient.executeMethod(post));
             assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Customer><id>125</id><name>Jack</name></Customer>",
                     post.getResponseBodyAsString());
         } finally {

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
(original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
Fri Jul  3 11:45:41 2009
@@ -71,6 +71,8 @@
 
         InputStream in = url.openStream();
         assertEquals("{\"Customer\":{\"id\":126,\"name\":\"Willem\"}}", CxfUtils.getStringFromInputStream(in));
+       
     }
+        
 
 }

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsRouterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsRouterTest.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsRouterTest.java
(original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsRouterTest.java
Fri Jul  3 11:45:41 2009
@@ -22,12 +22,20 @@
 
 import org.apache.camel.component.cxf.util.CxfUtils;
 import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
 import org.junit.Test;
-
 import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
+import static org.junit.Assert.assertEquals;
+
 public class CxfRsRouterTest extends CamelSpringTestSupport {
+    private static final String PUT_REQUEST = "<Customer><name>Mary</name><id>123</id></Customer>";
+    private static final String POST_REQUEST = "<Customer><name>Jack</name></Customer>";
 
     @Override
     protected AbstractXmlApplicationContext createApplicationContext() {        
@@ -40,8 +48,64 @@
 
         InputStream in = url.openStream();
         assertEquals("{\"Customer\":{\"id\":123,\"name\":\"John\"}}", CxfUtils.getStringFromInputStream(in));
-        // TODO we need to support the subResourceLocator
-                
+    }
+    
+    @Test
+    public void testGetSubResource() throws Exception {
+        URL url = new URL("http://localhost:9000/customerservice/orders/223/products/323");
+        InputStream in = url.openStream();
+        assertEquals("{\"Product\":{\"description\":\"product 323\",\"id\":323}}", CxfUtils.getStringFromInputStream(in));
+    }
+    
+    @Test
+    public void testPutConsumer() throws Exception {
+        PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers");
+        RequestEntity entity = new StringRequestEntity(PUT_REQUEST, "text/xml", "ISO-8859-1");
+        put.setRequestEntity(entity);
+        HttpClient httpclient = new HttpClient();
+
+        try {
+            assertEquals(200, httpclient.executeMethod(put));
+            assertEquals("", put.getResponseBodyAsString());
+        } finally {
+            put.releaseConnection();
+        }
+    }
+    
+    @Test
+    public void testPostConsumer() throws Exception {
+        PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers");
+        post.addRequestHeader("Accept" , "text/xml");
+        RequestEntity entity = new StringRequestEntity(POST_REQUEST, "text/xml", "ISO-8859-1");
+        post.setRequestEntity(entity);
+        HttpClient httpclient = new HttpClient();
+
+        try {
+            assertEquals(200, httpclient.executeMethod(post));
+            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Customer><id>124</id><name>Jack</name></Customer>",
+                    post.getResponseBodyAsString());
+        } finally {
+            post.releaseConnection();
+        }
+
+    }
+    
+    @Test
+    public void testPostConsumerUniqueResponseCode() throws Exception {
+        PostMethod post = new PostMethod("http://localhost:9000/customerservice/customersUniqueResponseCode");
+        post.addRequestHeader("Accept" , "text/xml");
+        RequestEntity entity = new StringRequestEntity(POST_REQUEST, "text/xml", "ISO-8859-1");
+        post.setRequestEntity(entity);
+        HttpClient httpclient = new HttpClient();
+
+        try {
+            assertEquals(201, httpclient.executeMethod(post));
+            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Customer><id>124</id><name>Jack</name></Customer>",
+                    post.getResponseBodyAsString());
+        } finally {
+            post.releaseConnection();
+        }
+
     }
 
 }

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerService.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerService.java?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerService.java
(original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerService.java
Fri Jul  3 11:45:41 2009
@@ -83,7 +83,7 @@
 
         customers.put(customer.getId(), customer);
         
-        return Response.status(301).entity(customer).build();
+        return Response.status(201).entity(customer).build();
     }
 
     @DELETE

Modified: camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml?rev=790870&r1=790869&r2=790870&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml
(original)
+++ camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringRouter.xml
Fri Jul  3 11:45:41 2009
@@ -33,21 +33,28 @@
       <ref bean="customerService"/>
     </jaxrs:serviceBeans>       
   </jaxrs:server>
+  
+  <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider"/>
 
   <bean id="customerService" class="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"
/>
    
   <cxf:rsServer id="rsServer" address="http://localhost:9000"
-    serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"/>
+    serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" />
 
 
   <cxf:rsClient id="rsClient" address="http://localhost:9002"
     serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"/>
+  
 
   <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
     <route>
        <from uri="cxfrs://bean://rsServer"/>
+       <setHeader headerName="CamelCxfRsUsingHttpAPI">
+         <constant>True</constant>        
+       </setHeader>
        <to uri="cxfrs://bean://rsClient"/>
     </route>
   </camelContext>
 
 </beans>
+



Mime
View raw message