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 GetGeoIP operation from GeoIPService SOAP Web service (http://www.webservicex.net/geoipservice.asmx) to look up countries by IP addresses.
<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 void execute(DelegateExecution execution) throws Exception { String var = (String) execution.getVariable("ip"); MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); String serverURI = "http://www.webservicex.net/"; // SOAP Envelope SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("example", serverURI); // SOAP Body SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyElem = soapBody.addChildElement("GetGeoIP", "example"); SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("IPAddress", "example"); soapBodyElem1.addTextNode(var); MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", serverURI + "GetGeoIP"); soapMessage.saveChanges(); // Create SOAP Connection SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); // Send SOAP Message to SOAP Server String url = "http://www.webservicex.net/geoipservice.asmx"; SOAPMessage soapResponse = soapConnection.call(soapMessage, url); String result = ""; NodeList nodeList = soapResponse.getSOAPBody().getElementsByTagName("CountryName"); if (nodeList.getLength() > 0) result = nodeList.item(0).getTextContent(); else result = "Invalid IP address"; soapConnection.close(); execution.setVariable("result", 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>