You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/10/08 13:37:51 UTC
svn commit: r1005785 - in /camel/trunk:
components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/
components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/
components/camel-cxf/src/test/java/org/apache/camel/component/...
Author: davsclaus
Date: Fri Oct 8 11:37:50 2010
New Revision: 1005785
URL: http://svn.apache.org/viewvc?rev=1005785&view=rev
Log:
CAMEL-3208: cxfrs now uses fallback converter to make it easier to grab response body in a type you want.
Added:
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/testbean/
camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/testbean/jaxb.index
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest.java
- copied, changed from r1005729, camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/RestletDomainServiceTest.java
camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest-context.xml
- copied, changed from r1005729, camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.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/jaxrs/CxfRsProducerTest.java
camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml
Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java?rev=1005785&r1=1005784&r2=1005785&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java (original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java Fri Oct 8 11:37:50 2010
@@ -23,7 +23,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-
import javax.ws.rs.core.Response;
import javax.xml.soap.SOAPMessage;
@@ -174,7 +173,8 @@ public final class CxfConverter {
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value,
TypeConverterRegistry registry) {
-
+
+ // CXF-WS MessageContentsList class
if (MessageContentsList.class.isAssignableFrom(value.getClass())) {
MessageContentsList list = (MessageContentsList)value;
@@ -193,6 +193,20 @@ public final class CxfConverter {
// return void to indicate its not possible to convert at this time
return (T) Void.TYPE;
}
+
+ // CXF-RS Response class
+ if (Response.class.isAssignableFrom(value.getClass())) {
+ Response response = (Response) value;
+ Object entity = response.getEntity();
+
+ TypeConverter tc = registry.lookup(type, entity.getClass());
+ if (tc != null) {
+ return tc.convertTo(type, exchange, entity);
+ }
+
+ // return void to indicate its not possible to convert at this time
+ return (T) Void.TYPE;
+ }
return null;
}
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=1005785&r1=1005784&r2=1005785&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 Oct 8 11:37:50 2010
@@ -171,12 +171,11 @@ public class DefaultCxfRsBinding impleme
request = camelMessage.getBody();
if (request instanceof List) {
request = ((List<?>)request).get(0);
- } else if (request.getClass().isArray()) {
+ } else if (request != null && request.getClass().isArray()) {
request = ((Object[])request)[0];
}
return request;
-
}
/**
Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java?rev=1005785&r1=1005784&r2=1005785&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java (original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java Fri Oct 8 11:37:50 2010
@@ -27,6 +27,7 @@ import org.apache.camel.Processor;
import org.apache.camel.component.cxf.CxfConstants;
import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -50,7 +51,6 @@ public class CxfRsProducerTest extends C
public void testGetConstumerWithClientProxyAPI() {
// START SNIPPET: ProxyExample
Exchange exchange = template.send("direct://proxy", new Processor() {
-
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
@@ -62,7 +62,6 @@ public class CxfRsProducerTest extends C
// camel will put this object into an Object[] itself
inMessage.setBody("123");
}
-
});
// get the response message
@@ -78,7 +77,6 @@ public class CxfRsProducerTest extends C
public void testGetConstumerWithHttpCentralClientAPI() {
// START SNIPPET: HttpExample
Exchange exchange = template.send("direct://http", new Processor() {
-
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
@@ -93,7 +91,6 @@ public class CxfRsProducerTest extends C
// since we use the Get method, so we don't need to set the message body
inMessage.setBody(null);
}
-
});
// get the response message
@@ -107,9 +104,7 @@ public class CxfRsProducerTest extends C
@Test
public void testGetConstumerWithCxfRsEndpoint() {
-
Exchange exchange = template.send("cxfrs://http://localhost:9002?httpClientAPI=true", new Processor() {
-
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
@@ -122,7 +117,6 @@ public class CxfRsProducerTest extends C
// since we use the Get method, so we don't need to set the message body
inMessage.setBody(null);
}
-
});
// get the response message
@@ -131,13 +125,11 @@ public class CxfRsProducerTest extends C
assertNotNull("The response should not be null ", response);
assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123");
assertEquals("Get a wrong customer name", response.getName(), "John");
-
}
@Test
public void testAddCustomerUniqueResponseCode() {
Exchange exchange = template.send("cxfrs://http://localhost:9002?httpClientAPI=true", new Processor() {
-
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
@@ -153,7 +145,6 @@ public class CxfRsProducerTest extends C
customer.setName("Willem");
inMessage.setBody(customer);
}
-
});
// get the response message
@@ -186,16 +177,12 @@ public class CxfRsProducerTest extends C
assertNotNull("The response should not be null ", response);
assertEquals("The response value is wrong", "q1=12&q2=13", response);
-
-
}
@Test
public void testProducerWithQueryParametersHeader() {
-
Exchange exchange = template.send("cxfrs://http://localhost:9003/testQuery?httpClientAPI=true&q1=12&q2=13"
-
- , new Processor() {
+ , new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
@@ -219,9 +206,29 @@ public class CxfRsProducerTest extends C
assertNotNull("The response should not be null ", response);
assertEquals("The response value is wrong", "q1=new&q2=world", response);
-
-
}
-
+
+ @Test
+ @Ignore("TODO: Fixme")
+ public void testRestServerDirectlyGetCustomer() {
+ // we cannot convert directly to Customer as we need camel-jaxb
+ String response = template.requestBodyAndHeader("cxfrs:http://localhost:9002/customerservice/customers/123",
+ null, Exchange.HTTP_METHOD, "GET", String.class);
+
+ assertNotNull("The response should not be null ", response);
+ }
+
+ @Test
+ public void testRestServerDirectlyAddCustomer() {
+ Customer input = new Customer();
+ input.setName("Donald Duck");
+
+ // we cannot convert directly to Customer as we need camel-jaxb
+ String response = template.requestBodyAndHeader("cxfrs:http://localhost:9002/customerservice/customers",
+ input, Exchange.HTTP_METHOD, "POST", String.class);
+
+ assertNotNull(response);
+ assertTrue(response.endsWith("<name>Donald Duck</name></Customer>"));
+ }
}
Added: camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/testbean/jaxb.index
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/testbean/jaxb.index?rev=1005785&view=auto
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/testbean/jaxb.index (added)
+++ camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/testbean/jaxb.index Fri Oct 8 11:37:50 2010
@@ -0,0 +1,19 @@
+## ------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements. See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License. You may obtain a copy of the License at
+##
+## http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ------------------------------------------------------------------------
+Customer
+Order
+Product
\ No newline at end of file
Copied: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest.java (from r1005729, camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/RestletDomainServiceTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest.java?p2=camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest.java&p1=camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/RestletDomainServiceTest.java&r1=1005729&r2=1005785&rev=1005785&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/RestletDomainServiceTest.java (original)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest.java Fri Oct 8 11:37:50 2010
@@ -16,9 +16,11 @@
*/
package org.apache.camel.itest.restlet.example;
+import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.junit.Assert;
+import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
@@ -28,26 +30,58 @@ import org.springframework.test.context.
* @version $Revision$
*/
@ContextConfiguration
-public class RestletDomainServiceTest extends AbstractJUnit4SpringContextTests {
+public class CxfRSDomainServiceTest extends AbstractJUnit4SpringContextTests {
+
+ @Autowired
+ protected CamelContext context;
@Autowired
protected ProducerTemplate template;
@Test
- public void testAddDomain() throws Exception {
+ public void testAddDomainString() throws Exception {
String input = "<checkDomainRequest><id>123</id><name>www.google.com</name><username>test</username><password>test</password></checkDomainRequest>";
- String response = template.requestBodyAndHeader("restlet:http://localhost:9000/domainservice/domains?restletMethod=POST",
- input, Exchange.CONTENT_TYPE, "application/xml", String.class);
+ String response = template.requestBody("cxfrs:http://localhost:9000/domainservice/domains", input, String.class);
Assert.assertNotNull(response);
Assert.assertTrue("Should contains response", response.endsWith("<CheckDomainResponse><requestId>123</requestId><responseBody>OK</responseBody></CheckDomainResponse>"));
}
@Test
- public void testGetDomain() throws Exception {
- String response = template.requestBody("restlet:http://localhost:9000/domainservice/domains/123?restletMethod=GET", null, String.class);
+ public void testAddDomainStringObject() throws Exception {
+ String input = "<checkDomainRequest><id>123</id><name>www.google.com</name><username>test</username><password>test</password></checkDomainRequest>";
+
+ CheckDomainAvailabilityRestResponse response = template.requestBody("cxfrs:http://localhost:9000/domainservice/domains",
+ input, CheckDomainAvailabilityRestResponse.class);
+
+ Assert.assertNotNull(response);
+ Assert.assertEquals("123", response.getRequestId());
+ Assert.assertEquals("OK", response.getResponseBody());
+ }
+ @Test
+ public void testAddDomainBothObjects() throws Exception {
+ CheckDomainRequest input = new CheckDomainRequest();
+ input.setId(123);
+ input.setName("www.google.com");
+ input.setUsername("test");
+ input.setPassword("test");
+
+ CheckDomainAvailabilityRestResponse response = template.requestBody("cxfrs:http://localhost:9000/domainservice/domains",
+ input, CheckDomainAvailabilityRestResponse.class);
+
+ Assert.assertNotNull(response);
+ Assert.assertEquals("123", response.getRequestId());
+ Assert.assertEquals("OK", response.getResponseBody());
+ }
+
+ @Test
+ @Ignore
+ public void testGetDomain() throws Exception {
+ // TODO: Must make CXF-RS easier to use
+ Object response = template.requestBodyAndHeader("cxfrs:http://localhost:9000/domainservice/domains/123", null,
+ Exchange.HTTP_METHOD, "GET");
Assert.assertEquals("{www.google.com}", response);
}
Copied: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest-context.xml (from r1005729, camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest-context.xml?p2=camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest-context.xml&p1=camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml&r1=1005729&r2=1005785&rev=1005785&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml (original)
+++ camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/CxfRSDomainServiceTest-context.xml Fri Oct 8 11:37:50 2010
@@ -27,8 +27,6 @@
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml"/>
- <!--<bean id="domainService" class="org.apache.camel.itest.restlet.example.DomainService"/>-->
-
<!-- setup REST server -->
<jaxrs:server id="domainServiceRest" address="http://localhost:9000/">
<jaxrs:serviceBeans>
Modified: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml?rev=1005785&r1=1005784&r2=1005785&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml (original)
+++ camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/restlet/example/RestletDomainServiceTest-context.xml Fri Oct 8 11:37:50 2010
@@ -27,8 +27,6 @@
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml"/>
- <!--<bean id="domainService" class="org.apache.camel.itest.restlet.example.DomainService"/>-->
-
<!-- setup REST server -->
<jaxrs:server id="domainServiceRest" address="http://localhost:9000/">
<jaxrs:serviceBeans>