Author: ningjiang
Date: Mon May 30 08:58:11 2011
New Revision: 1129070
URL: http://svn.apache.org/viewvc?rev=1129070&view=rev
Log:
CAMEL-4030 Camel CxfConsumer should be able to populate the CxfResponse from
camel exchange according to the exchange pattern
Added:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerResponseTest.java
(with props)
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java?rev=1129070&r1=1129069&r2=1129070&view=diff
==============================================================================
---
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
(original)
+++
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
Mon May 30 08:58:11 2011
@@ -174,10 +174,7 @@ public class CxfConsumer extends Default
checkFailure(camelExchange);
- // bind the Camel response into a CXF response
- if (camelExchange.getPattern().isOutCapable()) {
- binding.populateCxfResponseFromExchange(camelExchange,
cxfExchange);
- }
+ binding.populateCxfResponseFromExchange(camelExchange,
cxfExchange);
// check failure again as fault could be discovered by
converter
checkFailure(camelExchange);
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java?rev=1129070&r1=1129069&r2=1129070&view=diff
==============================================================================
---
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
(original)
+++
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
Mon May 30 08:58:11 2011
@@ -271,8 +271,15 @@ public class DefaultCxfBinding implement
// create response context
Map<String, Object> responseContext = new HashMap<String, Object>();
+ org.apache.camel.Message response;
+ if (camelExchange.getPattern().isOutCapable()) {
+ response = camelExchange.getOut();
+ } else {
+ response = camelExchange.getIn();
+ }
+
// propagate response context
- Map<String, Object> camelHeaders = camelExchange.getOut().getHeaders();
+ Map<String, Object> camelHeaders = response.getHeaders();
extractInvocationContextFromCamel(camelExchange, camelHeaders,
responseContext, Client.RESPONSE_CONTEXT);
@@ -297,7 +304,7 @@ public class DefaultCxfBinding implement
LOG.trace("Set out response context = {}", responseContext);
// set body
- Object outBody =
DefaultCxfBinding.getBodyFromCamel(camelExchange.getOut(), dataFormat);
+ Object outBody = DefaultCxfBinding.getBodyFromCamel(response,
dataFormat);
if (outBody != null) {
if (dataFormat == DataFormat.PAYLOAD) {
Added:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerResponseTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerResponseTest.java?rev=1129070&view=auto
==============================================================================
---
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerResponseTest.java
(added)
+++
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerResponseTest.java
Mon May 30 08:58:11 2011
@@ -0,0 +1,123 @@
+/**
+ * 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.
+ */
+
+package org.apache.camel.component.cxf;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletRequest;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.frontend.ClientFactoryBean;
+import org.apache.cxf.frontend.ClientProxyFactoryBean;
+import org.junit.Test;
+
+
+public class CxfConsumerResponseTest extends CamelTestSupport {
+ protected static final String SIMPLE_ENDPOINT_ADDRESS =
"http://localhost:28080/test";
+ protected static final String SIMPLE_ENDPOINT_URI = "cxf://" +
SIMPLE_ENDPOINT_ADDRESS
+ + "?serviceClass=org.apache.camel.component.cxf.HelloService"
+ + "&publishedEndpointUrl=http://www.simple.com/services/test";
+
+ private static final String ECHO_OPERATION = "echo";
+ private static final String ECHO_BOOLEAN_OPERATION = "echoBoolean";
+ private static final String PING_OPERATION = "ping";
+ private static final String TEST_MESSAGE = "Hello World!";
+ private static int pingCounter;
+
+ // START SNIPPET: example
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+
from(SIMPLE_ENDPOINT_URI).inOnly("log:test").choice().when(header(CxfConstants.OPERATION_NAME).isEqualTo(ECHO_OPERATION)).process(new
Processor() {
+ public void process(final Exchange exchange) {
+ assertEquals(DataFormat.POJO,
exchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class));
+ Message in = exchange.getIn();
+ // check the remote IP from the cxfMessage
+ org.apache.cxf.message.Message cxfMessage =
in.getHeader(CxfConstants.CAMEL_CXF_MESSAGE,
org.apache.cxf.message.Message.class);
+ assertNotNull("Should get the cxfMessage instance from
message header", cxfMessage);
+ ServletRequest request =
(ServletRequest)cxfMessage.get("HTTP.REQUEST");
+ assertNotNull("Should get the ServletRequest",
request);
+ assertNotNull("Should get the RemoteAddress" +
request.getRemoteAddr());
+ // Get the parameter list
+ List<?> parameter = in.getBody(List.class);
+ // Get the operation name
+ String operation =
(String)in.getHeader(CxfConstants.OPERATION_NAME);
+ Object result = operation + " " +
(String)parameter.get(0);
+ // Put the result back
+ exchange.getIn().setBody(result);
+ // set up the response context which force start
document
+ Map<String, Object> map = new HashMap<String,
Object>();
+ map.put("org.apache.cxf.stax.force-start-document",
Boolean.TRUE);
+ exchange.getIn().setHeader(Client.RESPONSE_CONTEXT,
map);
+ }
+ })
+
.when(header(CxfConstants.OPERATION_NAME).isEqualTo(ECHO_BOOLEAN_OPERATION)).process(new
Processor() {
+ public void process(final Exchange exchange) {
+ Message in = exchange.getIn();
+ // Get the parameter list
+ List<?> parameter = in.getBody(List.class);
+ // Put the result back
+ exchange.getOut().setBody((Boolean)parameter.get(0));
+ }
+ })
+
.when(header(CxfConstants.OPERATION_NAME).isEqualTo(PING_OPERATION)).process(new
Processor() {
+ public void process(final Exchange exchange) {
+ pingCounter++;
+ }
+
+ });
+
+ }
+ };
+ }
+ // END SNIPPET: example
+
+ @Test
+ public void testInvokingServiceFromCXFClient() throws Exception {
+ ClientProxyFactoryBean proxyFactory = new ClientProxyFactoryBean();
+ ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean();
+ clientBean.setAddress(SIMPLE_ENDPOINT_ADDRESS);
+ clientBean.setServiceClass(HelloService.class);
+ clientBean.setBus(BusFactory.getDefaultBus());
+
+ HelloService client = (HelloService) proxyFactory.create();
+
+ String result = client.echo(TEST_MESSAGE);
+ assertEquals("We should get the echo string result from router",
result, "echo " + TEST_MESSAGE);
+
+ Boolean bool = client.echoBoolean(Boolean.TRUE);
+ assertNotNull("The result should not be null", bool);
+ assertEquals("We should get the echo boolean result from router ",
bool.toString(), "true");
+
+ int beforeCallingPing = pingCounter;
+ client.ping();
+ int afterCallingPing = pingCounter;
+ assertTrue("The ping operation doesn't be called", afterCallingPing -
beforeCallingPing == 1);
+ }
+
+
+}
Propchange:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerResponseTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerResponseTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java?rev=1129070&r1=1129069&r2=1129070&view=diff
==============================================================================
---
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
(original)
+++
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
Mon May 30 08:58:11 2011
@@ -29,6 +29,7 @@ import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.impl.DefaultHeaderFilterStrategy;
@@ -131,7 +132,7 @@ public class DefaultCxfBindingTest exten
public void testPopupalteCxfResponseFromExchange() {
DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
- Exchange exchange = new DefaultExchange(context);
+ Exchange exchange = new DefaultExchange(context,
ExchangePattern.InOut);
org.apache.cxf.message.Exchange cxfExchange = new
org.apache.cxf.message.ExchangeImpl();
exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY,
DataFormat.PAYLOAD);