This is an automated email from the ASF dual-hosted git repository.

davsclaus 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 729d69db23f Refactor and fix camel-cxf-soap first example (#15573)
729d69db23f is described below

commit 729d69db23fdec1ef94af4dadbe2b0eb7dd352aa
Author: Lukas Lowinger <[email protected]>
AuthorDate: Tue Sep 17 11:29:01 2024 +0200

    Refactor and fix camel-cxf-soap first example (#15573)
---
 .../src/main/docs/cxf-component.adoc               | 119 ++++++++-------------
 1 file changed, 47 insertions(+), 72 deletions(-)

diff --git 
a/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc 
b/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc
index c646a081b07..5571ad46aba 100644
--- a/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc
+++ b/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc
@@ -567,7 +567,13 @@ and setDefaultBus properties from spring configuration 
file.
 
 == Examples
 
-=== How to create a simple CXF service with POJO data format
+=== How to consume a message from a Camel CXF endpoint in POJO data format
+
+The Camel CXF endpoint consumer POJO data format is based on the
+http://cxf.apache.org/docs/invokers.html[CXF invoker], so the
+message header has a property with the name of
+`CxfConstants.OPERATION_NAME` and the message body is a list of the SEI
+operation parameters.
 
 Having simple java web service interface:
 
@@ -576,100 +582,69 @@ Having simple java web service interface:
 ----
 package org.apache.camel.component.cxf.soap.server;
 
-@WebService(targetNamespace = 
"http://server.soap.cxf.component.camel.apache.org/";, name = "EchoService")
-public interface EchoService {
-
-    String echo(String text);
-}
-----
-
-And implementation:
-
-[source,java]
-----
-
-package org.apache.camel.component.cxf.soap.server;
+import jakarta.jws.WebService;
 
-@WebService(name = "EchoService", serviceName = "EchoService", targetNamespace 
= "http://server.soap.cxf.component.camel.apache.org/";)
-public class EchoServiceImpl implements EchoService {
+@WebService(targetNamespace = 
"http://server.soap.cxf.component.camel.apache.org/";, name = "TextService", 
serviceName = "TextService")
+public interface TextService {
 
-    @Override
-    public String echo(String text) {
-        return text;
-    }
+    String upperCase(String text);
 
+    String lowerCase(String text);
 }
 ----
 
-We can then create the simplest CXF service (note we didn't specify the `POJO` 
mode, as it is the default mode):
+We can then create the simplest CXF service (note we didn't specify the `POJO` 
mode, as it is the default mode) as follows:
 
 [source,java]
 ----
-    
from("cxf:echoServiceResponseFromImpl?serviceClass=org.apache.camel.component.cxf.soap.server.EchoServiceImpl&address=/echo-impl")//
 no body set here; the response comes from EchoServiceImpl
-                .log("${body}");
+    
from("cxf:textServiceResponseFromRoute?serviceClass=org.apache.camel.component.cxf.soap.server.TextService&address=/text-route",
+            .process(exchange -> {
+                String operation = (String) 
exchange.getIn().getHeader(CxfConstants.OPERATION_NAME);
+                String inputArg = ((MessageContentsList) 
exchange.getIn().getBody()).get(0).toString();
+                String result = null;
+                if (operation.equals("upperCase")) {
+                    result = inputArg.toUpperCase();
+                }
+                else if (operation.equals("lowerCase")) {
+                    result = inputArg.toLowerCase();
+                }
+                exchange.getIn().setBody(result);
+            });                                     
 ----
 
-For more complicated implementation of the service (more "Camel way"), we can 
set the body from the route instead:
+For more dynamic implementation, we can create the implementation class as 
follows:
 
 [source,java]
 ----
-    
from("cxf:echoServiceResponseFromRoute?serviceClass=org.apache.camel.component.cxf.soap.server.EchoServiceImpl&address=/echo-route")
-                .setBody(exchange -> 
exchange.getMessage().getBody(String.class) + " from Camel route");
-----
-
-
-=== How to consume a message from a Camel CXF endpoint in POJO data format
 
-The Camel CXF endpoint consumer POJO data format is based on the
-http://cxf.apache.org/docs/invokers.html[CXF invoker], so the
-message header has a property with the name of
-`CxfConstants.OPERATION_NAME` and the message body is a list of the SEI
-method parameters.
+package org.apache.camel.component.cxf.soap.server;
 
-Consider the 
https://github.com/apache/camel/blob/main/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/wsdl_first/PersonProcessor.java[PersonProcessor]
 example code:
+import jakarta.jws.WebService;
 
-[source,java]
-----
-public class PersonProcessor implements Processor {
+@WebService(name = "TextService", serviceName = "TextService", targetNamespace 
= "http://server.soap.cxf.component.camel.apache.org/";)
+public class TextServiceImpl implements TextService {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(PersonProcessor.class);
+    @Override
+    public String upperCase(String text) {
+        return text.toUpperCase();
+    }
 
     @Override
-    @SuppressWarnings("unchecked")
-    public void process(Exchange exchange) throws Exception {
-        LOG.info("processing exchange in camel");
+    public String lowerCase(String text) {
+        return text.toLowerCase();
+    }
+}
 
-        BindingOperationInfo boi = (BindingOperationInfo) 
exchange.getProperty(BindingOperationInfo.class.getName());
-        if (boi != null) {
-            LOG.info("boi.isUnwrapped" + boi.isUnwrapped());
-        }
-        // Get the parameter list which element is the holder.
-        MessageContentsList msgList = (MessageContentsList) 
exchange.getIn().getBody();
-        Holder<String> personId = (Holder<String>) msgList.get(0);
-        Holder<String> ssn = (Holder<String>) msgList.get(1);
-        Holder<String> name = (Holder<String>) msgList.get(2);
-
-        if (personId.value == null || personId.value.length() == 0) {
-            LOG.info("person id 123, so throwing exception");
-            // Try to throw out the soap fault message
-            org.apache.camel.wsdl_first.types.UnknownPersonFault personFault
-                    = new 
org.apache.camel.wsdl_first.types.UnknownPersonFault();
-            personFault.setPersonId("");
-            org.apache.camel.wsdl_first.UnknownPersonFault fault
-                    = new org.apache.camel.wsdl_first.UnknownPersonFault("Get 
the null value of person name", personFault);
-            exchange.getMessage().setBody(fault);
-            return;
-        }
+----
 
-        name.value = "Bonjour";
-        ssn.value = "123";
-        LOG.info("setting Bonjour as the response");
-        // Set the response message, the first element is the return value of 
the operation,
-        // the others are the holders of method parameters
-        exchange.getMessage().setBody(new Object[] { null, personId, ssn, name 
});
-    }
 
-}
+Then we can use 
https://camel.apache.org/components/next/bean-component.html[Bean] (don't 
forget to add `camel-bean` dependency to your project) invocation based on 
requested operation name:
+
+[source,java]
+----
+    
from("cxf:textServiceResponseFromImpl?serviceClass=org.apache.camel.component.cxf.soap.server.TextService&address=/text-impl")
+                .process(exchange -> 
exchange.getIn().setHeader(BeanConstants.BEAN_METHOD_NAME, 
exchange.getIn().getHeader(CxfConstants.OPERATION_NAME)))
+                
.to("bean:org.apache.camel.component.cxf.soap.server.TextServiceImpl");
 ----
 
 === How to prepare the message for the Camel CXF endpoint in POJO data format

Reply via email to