We will see how to invoke SOAP Web service with Floawble java service task. You should practice the previous tutorial to be clear with the java service task. We use the NumberToWord operation from NumberConvertion SOAP Web service to translate numbers to words (http://www.dataaccess.com/webservicesserver/NumberConversion.wso)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.flowable</groupId> <artifactId>org.flowable</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.flowable</groupId> <artifactId>flowable-engine</artifactId> <version>6.7.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.sun.xml.messaging.saaj</groupId> <artifactId>saaj-impl</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>jakarta.xml.soap</groupId> <artifactId>jakarta.xml.soap-api</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>com.sun.activation</groupId> <artifactId>jakarta.activation</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> <version>2.1.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <artifactSet> <includes> <include>jakarta.xml.soap:jakarta.xml.soap-api</include> <include>com.sun.xml.messaging.saaj:saaj-impl</include> <include>com.sun.activation:jakarta.activation</include> <include>org.jvnet.staxex:stax-ex</include> </includes> </artifactSet> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
package org.flowable; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.delegate.JavaDelegate; import org.w3c.dom.NodeList; import jakarta.xml.soap.MessageFactory; import jakarta.xml.soap.SOAPBody; import jakarta.xml.soap.SOAPConnection; import jakarta.xml.soap.SOAPConnectionFactory; import jakarta.xml.soap.SOAPElement; import jakarta.xml.soap.SOAPEnvelope; import jakarta.xml.soap.SOAPException; import jakarta.xml.soap.SOAPMessage; import jakarta.xml.soap.SOAPPart; public class MySOAPDelegate implements JavaDelegate { public static boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false; } return true; } public void execute(DelegateExecution execution) { String var = (String) execution.getVariable("input"); String result = ""; if (!isNumeric(var)) { result = "Invalid Number"; } else { MessageFactory messageFactory; try { messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); String serverURI = "http://www.dataaccess.com/webservicesserver/"; // SOAP Envelope SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("ex", serverURI); // SOAP Body SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyElem = soapBody.addChildElement("NumberToWords", "ex"); SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("ubiNum", "ex"); soapBodyElem1.addTextNode(var); soapMessage.saveChanges(); // Create SOAP Connection SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); // Send SOAP Message to SOAP Server String url = "https://www.dataaccess.com/webservicesserver/NumberConversion.wso"; SOAPMessage soapResponse = soapConnection.call(soapMessage, url); NodeList nodeList = soapResponse.getSOAPBody().getElementsByTagName("m:NumberToWordsResult"); String test = soapResponse.getSOAPBody().getTextContent(); if (nodeList.getLength() > 0) result = nodeList.item(0).getTextContent(); else result = "Invalid Number"; soapConnection.close(); } catch (SOAPException e) { // TODO Auto-generated catch block e.printStackTrace(); } } execution.setVariable("input", result); } }
package forthREST; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; public class UserResource extends ServerResource { @Get public String toString() { String uid = (String) getRequestAttributes().get("uid"); if (uid.equals("123")){ return "<users>" + "<user>" + "<id>"+uid+"</id>" + "<name>John</name>" + "</user>" + "</users>"; } return "No information found."; } }
<users><user><id>123</id><name>John</name></user></users>