Web service invocation with Activiti java service task

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.

Create an external Java class as a jar file

  1. In Eclipse, create a new Maven Project.
  2. In your project, edit the file pom.xml by adding:

        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-engine</artifactId>
          <version>5.22.0</version>
        </dependency>
  3. Create a package org.activiti.
  4. In the package org.activiti, create a class MySOAPDelegate with the following code:

    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);
    	}
    }
  5. Right click on your project and select Run As→Maven Install to build a jar file.
  6. Copy a jar file in the folder target to apache-tomcat-7.0.12\webapps\activiti-explorer\WEB-INF\lib
  7. Restart your tomcat.

Design the "Get IP Country" process model

  1. In Activiti, login as admin with kermit/kermit
  2. Go to Processes→Model workspace→New model.
  3. Enter new model Name: Get IP Country. Click Create.
  4. Firstly, enter the following process properties:
    1. Process identifier: getIPCountry
    2. Name: Get IP Country
  5. Design a process model by drag and drop model elements, you should have something similar to this picture:

    ?1000
  6. On the start event, specify the following properties:
    1. Initiator: initiator
    2. Form properties:
      1. id: ip, Name: IP Address, Type: string
  7. On the Get IP Country activity, specify the property Class: org.activiti.MySOAPDelegate
  8. On the Result activity, specify the following properties:
    1. Assignments→Assignee: ${initiator}
    2. Form properties:
      1. id: result, Name: Result, Type: string
  9. On the Reenter IP activity, specify the following properties:
    1. Assignments→Assignee: ${initiator}
    2. Documentation: Invalid IP address, please reenter.
    3. Form properties:
      1. id: ip, Name: IP Address, Type: string
  10. On the exclusive gateway, make sure that the property Exclusive is checked.
  11. On the sequence flow from the exclusive gateway to Result, specify the property Flow condition: ${result != 'Invalid IP address'}
  12. On the sequence flow from the exclusive gateway to Reenter IP, specify the property Flow condition: ${result == 'Invalid IP address'}
  13. Click on Save the model icon, then click on Save and close editor.
  14. In Processes→Model workspace, selected your Get IP Country process model, then go to Model action and select Deploy.

Executing a process model

  1. Go to Processes→Deployed process definitions, select your Get IP Country process model, then click on Start process.
  2. Fill the ip address (for example, 8.8.8.8), then click Start process.
  3. Go to Processes→My instances to observe the status of your executing process. The current task should be Result if you entered 8.8.8.8 as an ip address.


  4. Select Tasks→Inbox to see the result. It should be United States if you entered 8.8.8.8 as an ip address. Then click the Complete task button.


  5. Try to start the process again with invalid ip address (for example, 999.999.999.999). Observe the status of your executing process. The current task should be Reenter IP


  6. Select Tasks→Inbox to reenter the ip address. Enter an ip address then click the Complete task. If you entered invalid ip address again, you will need to reenter it until it's valid.


Exercices

  1. Can you invoke the sayHello operation from WS_HelloWorld service from first web service tutorial
  2. Create a process model with a java service task to access your REST resource. Please do these steps before designing your process model:
    1. Suppose that we have already done the tutorial about Getting a target resource based on the client request. Open the class UserResource.java file. Modify the code as following:

      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.";  
       
      	}  
      }
    2. Run the modified REST service by right clicking on the RESTDistributor.java file, select Run As → Java Application.
    3. Open http://localhost:8182/users/123 by your web browser. You should get the following:

      <users><user><id>123</id><name>John</name></user></users>
    4. Create a new Maven Project for an external Java class to access your REST resource, build it as a jar file and copy it to apache-tomcat-7.0.12\webapps\activiti-explorer\WEB-INF\lib.
    5. Restart your tomcat.
teaching_assistant/workflow/activiti_soapv1.txt · Last modified: 2020/12/07 19:00 by IT Courses (NNC)
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0