We will see how to invoke SOAP Web service with Activiti 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)
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.22.0</version> </dependency>
package org.activiti; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.JavaDelegate; import org.w3c.dom.NodeList; public class MySOAPDelegate implements JavaDelegate { public static boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { double d = Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false; } return true; } public void execute(DelegateExecution execution) throws Exception { String var = (String) execution.getVariable("input"); String result = ""; if(!isNumeric(var)){ result = "Invalid Number"; } else{ MessageFactory 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 = "http://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(); } 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>