CAMEL-7396 Added defaultMethodName option to camel-xmlrpc
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9207ab7a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9207ab7a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9207ab7a Branch: refs/heads/master Commit: 9207ab7acaaa972aa007b261d1e59c79368add54 Parents: 4e36dd2 Author: Willem Jiang <[email protected]> Authored: Mon Apr 28 10:51:15 2014 +0800 Committer: Willem Jiang <[email protected]> Committed: Mon Apr 28 10:54:54 2014 +0800 ---------------------------------------------------------------------- .../camel/component/xmlrpc/XmlRpcEndpoint.java | 9 +++++++++ .../camel/component/xmlrpc/XmlRpcProducer.java | 21 ++++++++++++++++---- .../component/xmlrpc/XmlRpcEndpointTest.java | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9207ab7a/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java b/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java index 8806c10..2cbe7d3 100644 --- a/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java +++ b/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java @@ -32,6 +32,7 @@ import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; */ public class XmlRpcEndpoint extends DefaultEndpoint { private String address; + private String defaultMethodName; private XmlRpcClientConfigurer clientConfigurer; private XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl(); @@ -97,6 +98,14 @@ public class XmlRpcEndpoint extends DefaultEndpoint { public XmlRpcClientConfigImpl getClientConfig() { return clientConfig; } + + public String getDefaultMethodName() { + return defaultMethodName; + } + + public void setDefaultMethodName(String defaultMethodName) { + this.defaultMethodName = defaultMethodName; + } http://git-wip-us.apache.org/repos/asf/camel/blob/9207ab7a/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcProducer.java b/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcProducer.java index 1dd2c8b..0ae31a9 100644 --- a/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcProducer.java +++ b/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcProducer.java @@ -23,6 +23,7 @@ import org.apache.camel.AsyncProcessor; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; import org.apache.xmlrpc.client.XmlRpcClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,10 +44,16 @@ public class XmlRpcProducer extends DefaultProducer implements AsyncProcessor { public void process(Exchange exchange) throws Exception { LOG.trace("Process exchange: {} in the sync way.", exchange); Message in = exchange.getIn(); - String operationName = in.getHeader(XmlRpcConstants.METHOD_NAME, String.class); + String methodName = in.getHeader(XmlRpcConstants.METHOD_NAME, String.class); + if (ObjectHelper.isEmpty(methodName)) { + methodName = endpoint.getDefaultMethodName(); + } + if (ObjectHelper.isEmpty(methodName)) { + throw new IllegalArgumentException("CamelXmlRpcMethodName header is empty, please set the message header or defaultMethodName option on the endpoint."); + } try { //TODO need to use the binding to handle the requests - Object result = client.execute(operationName, in.getBody(List.class)); + Object result = client.execute(methodName, in.getBody(List.class)); //TODO what if the request is one way operation // copy the in message header to the out message exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders()); @@ -60,11 +67,17 @@ public class XmlRpcProducer extends DefaultProducer implements AsyncProcessor { public boolean process(Exchange exchange, AsyncCallback callback) { LOG.trace("Process exchange: {} in the async way.", exchange); Message in = exchange.getIn(); - String operationName = in.getHeader(XmlRpcConstants.METHOD_NAME, String.class); + String methodName = in.getHeader(XmlRpcConstants.METHOD_NAME, String.class); + if (ObjectHelper.isEmpty(methodName)) { + methodName = endpoint.getDefaultMethodName(); + } + if (ObjectHelper.isEmpty(methodName)) { + throw new IllegalArgumentException("CamelXmlRpcMethodName header is empty, please set the message header or defaultMethodName option on the endpoint."); + } XmlRpcAsyncCallback xmlRpcAsyncCallback = new XmlRpcAsyncCallback(exchange, callback); //TODO need to use the binding to handle the requests try { - client.executeAsync(operationName, in.getBody(List.class), xmlRpcAsyncCallback); + client.executeAsync(methodName, in.getBody(List.class), xmlRpcAsyncCallback); return false; } catch (Exception ex) { exchange.setException(ex); http://git-wip-us.apache.org/repos/asf/camel/blob/9207ab7a/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java b/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java index 5833dca..efee676 100644 --- a/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java +++ b/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java @@ -39,12 +39,13 @@ public class XmlRpcEndpointTest extends Assert { // create the endpoint with parameters @Test public void testEndpointSetting() throws Exception { - XmlRpcEndpoint endpoint = (XmlRpcEndpoint)camelContext.getEndpoint("xmlrpc:http://www.example.com?userAgent=myAgent&gzipCompressing=true&connectionTimeout=30"); + XmlRpcEndpoint endpoint = (XmlRpcEndpoint)camelContext.getEndpoint("xmlrpc:http://www.example.com?userAgent=myAgent&gzipCompressing=true&connectionTimeout=30&defaultMethodName=echo"); XmlRpcClientConfigImpl clientConfig = endpoint.getClientConfig(); assertEquals("Get a wrong userAgent", "myAgent", clientConfig.getUserAgent()); assertEquals("Get a wrong gzipCompressing", true, clientConfig.isGzipCompressing()); assertEquals("Get a wrong connectionTimeout", 30, clientConfig.getConnectionTimeout()); assertEquals("Get a wrong endpoint address", "http://www.example.com", endpoint.getAddress()); + assertEquals("Get a worng default method name", "echo", endpoint.getDefaultMethodName()); } @Test
