AnsweredAssumed Answered

Activiti 5.18 - Extending the REST API

Question asked by soaguy on Sep 4, 2015
Latest reply on Sep 8, 2015 by soaguy
Hello all,
            I am trying to create a custom rest service that utilizes the engine api. I created a maven spring project and coded the RestController. I am not sure where I should place this class for the DispatcherServlet to route to the 'dsw/sendResponse' endpoint. Do I need to make any changes to activiti-rest web.xml or any configuration file?

——– RestController —–

package org.activiti.rest.service.api.dsw;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.Execution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomRestController {

   @Autowired
   private RuntimeService runtimeService;

   @RequestMapping(value = "/dsw/sendResponse", method = RequestMethod.PUT, produces = "application/json")
   public MessageCorrelationResponse correlate(@RequestBody MessageCorrelationRequest messageRequest,
         HttpServletRequest request) {
      MessageCorrelationResponse messageResponse = new MessageCorrelationResponse();

      if (checkValidString(messageRequest.getMessageName()) && checkValidString(messageRequest.getCorrelationKey())
            && checkValidString(messageRequest.correlationKeyValue)
            && checkValidString(messageRequest.getMessagePayload())) {

         String messageName = messageRequest.getMessageName();
         // Locate the execution using the correlation
         Execution execution = runtimeService.createExecutionQuery().messageEventSubscriptionName(messageName)
               .variableValueEquals(messageRequest.getCorrelationKey(), messageRequest.correlationKeyValue)
               .singleResult();
         // Set message into map
         Map<String, Object> variableMap = new HashMap<String, Object>();
         variableMap.put("_MessageName", messageName);
         variableMap.put("_MessageContent", messageRequest.getMessagePayload());

         runtimeService.messageEventReceived(messageName, execution.getId(), variableMap);

         messageResponse.setResult("success");
      } else
         messageResponse.setResult("Invalid inputs");

      return messageResponse;

   }

   private boolean checkValidString(String str) {
      if (str != null && !str.isEmpty())
         return true;
      return false;

   }

}


—— activiti-rest webapp web.xml —
<blockcode>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0"
         metadata-complete="true">

    <display-name>Activiti REST</display-name>
    <!– All the Servlets and Filters are configured by this ServletContextListener : –>
    <listener>
        <listener-class>org.activiti.rest.servlet.WebConfigurer</listener-class>
    </listener>
</web-app>
</blockcode>

Outcomes