This is an automated email from the ASF dual-hosted git repository. ffang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 86f2fa577b8 [CAMEL-19324]Be able to convert all elements from CXF MessageContentsList.class to String.class if not in "CXF Context" (#10001) 86f2fa577b8 is described below commit 86f2fa577b868670a857bd320eb63928dc9bb2ed Author: Freeman(Yue) Fang <freeman.f...@gmail.com> AuthorDate: Tue May 9 09:05:02 2023 -0400 [CAMEL-19324]Be able to convert all elements from CXF MessageContentsList.class to String.class if not in "CXF Context" (#10001) --- .../camel/component/cxf/converter/CxfConverter.java | 20 +++++++++++++++++++- .../camel/component/cxf/converter/ConverterTest.java | 11 +++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/camel-cxf/camel-cxf-common/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java b/components/camel-cxf/camel-cxf-common/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java index 222b0c6dce8..c3870e210cc 100644 --- a/components/camel-cxf/camel-cxf-common/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java +++ b/components/camel-cxf/camel-cxf-common/src/main/java/org/apache/camel/component/cxf/converter/CxfConverter.java @@ -101,7 +101,8 @@ public final class CxfConverter { /** * Use a fallback type converter so we can convert the embedded list element if the value is MessageContentsList. * The algorithm of this converter finds the first non-null list element from the list and applies conversion to the - * list element. + * list element if can determine this MessageContentsList is used in CXF context(first element is the return value + * while others are Holders). * * @param type the desired type to be converted to * @param exchange optional exchange which can be null @@ -119,6 +120,23 @@ public final class CxfConverter { if (MessageContentsList.class.isAssignableFrom(value.getClass())) { MessageContentsList list = (MessageContentsList) value; + if (list.size() > 1 && type == String.class) { + //to check if the MessageContentsList is used in CXF context + //If not, use the general way to convert from List.class to String.class + boolean foundHolder = false; + for (Object embedded : list) { + if (embedded != null && embedded.getClass().getName().equals("javax.xml.ws.Holder")) { + foundHolder = true; + break; + } + } + if (!foundHolder) { + // this isn't a typical CXF MessageContentsList + // just using other fallback converters + return null; + } + } + // try to turn the first array element into the object that we want for (Object embedded : list) { if (embedded != null) { diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/converter/ConverterTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/converter/ConverterTest.java index 81fe256ac8b..7cf81bab063 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/converter/ConverterTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/converter/ConverterTest.java @@ -79,4 +79,15 @@ public class ConverterTest { assertNotNull(node); } + @Test + public void testMessageContentsListAsGeneralList() throws Exception { + CamelContext context = new DefaultCamelContext(); + Exchange exchange = new DefaultExchange(context); + MessageContentsList list = new MessageContentsList(); + list.add("hehe"); + list.add("haha"); + exchange.getIn().setBody(list); + String ret = exchange.getIn().getBody(String.class); + assertEquals(ret, "[hehe, haha]", "shouldn't miss list content"); + } }