in Activiti

How to extend Activiti REST

Activiti includes a REST API to the activiti engine that can be installed deploying activiti-rest.war application to your server ( for instance Tomcat ), activiti-rest can also be a part of your own application and in that way exposing REST services to the world.

We will focus on stand alone application deployment for now, and see how to extend the application and add new services that can be called.

Creating the project

Open eclipse and create new maven project

Create new maven project

Create new maven project

After clicking Next dialog for typing Group Id and  Artifact Id will appear, entering your domain name and project name like demonstrated in the next picture.

Entering Group Id and Artifact Id

Entering Group Id and Artifact Id

Clicking Finish will create the project.

Sorting out maven dependencies and plugins

In the created project we have pom.xml file located in the root of the project, lets open it . In the next listing we have what is to be put in this file.

 

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.alfrescoblog</groupId>
 <artifactId>alfrescoblogrestactiviti</artifactId>
 <version>0.0.1</version>
 <packaging>jar</packaging>

 <name>alfrescoblgorestactiviti</name>
 <url>http://maven.apache.org</url>

 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <activitiVersion>5.15.1</activitiVersion>
   <jdk.version>1.7</jdk.version>
 </properties>

 <dependencies>
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>3.8.1</version>
     <scope>test</scope>
   </dependency>
   <dependency>
     <groupId>org.activiti</groupId>
     <artifactId>activiti-rest</artifactId>
     <version>${activitiVersion}</version>
   </dependency>
 </dependencies>

 <build>
   <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.2</version>
       <configuration>
         <source>${jdk.version}</source>
         <target>${jdk.version}</target>
       </configuration>
    </plugin>
   </plugins>
 </build>
</project>

We have two dependences junit and activiti-rest that we will require for writing tests and extending activiti-rest it self. Current activiti-rest version is 5.15.1 that was lastest at the time of writing this post.

Creating new REST service

Activiti has plenty of REST services already created and ready for use, they all extend class SecuredResource that allow developer to :

  • Retrieve attribute using
    public String getAttribute(String name)
  • Retrieve query parameter as specified Type
    getQueryParameterAs...
  • Check for authentication
    protected boolean authenticate()

    Most of REST services are allowed only to authenticated users, this this method should be called before anything else is done in our service.

Many more things is allowed for this you should check source of the class.

Our new REST service will be simple Hello World service that returns “Hello World” when called.

package com.alfrescoblog.rest.service.api.runtime.test;

import org.activiti.rest.common.api.SecuredResource;
import org.restlet.resource.Get;

public class TestResource extends SecuredResource {
 
 
 @Get
 public String test(){
   if (!authenticate()) {
     return null;
   }
 return "Hello World";
 }
}

We have created a class called TestResource that extends SecuredResource previously explained . Adding a method with annotation @Get means that this method will be called after HTTP GET request to specified address .

HTTP request that can be used are:

  • GET
  • POST
  • PUT
  • DELETE
  • OPTIONS

Now lets put this resource to certain path.

Putting resource to specified path

Activiti-rest has one class that is called ActivitiRestServicesApplication, that holds things together and sets restlet.com up. We can find code of this class here. We need to extend it and attach our Resource to the Router.

package com.alfrescoblog.activitirestedit;


import org.activiti.rest.common.filter.JsonpFilter;
import org.activiti.rest.service.application.ActivitiRestServicesApplication;
import org.restlet.Restlet;
import org.restlet.routing.Router;
import org.restlet.security.ChallengeAuthenticator;

import com.alfrescoblog.rest.service.api.runtime.test.TestResource;



public class AlfrescoBlogApplication extends ActivitiRestServicesApplication{
 
 @Override
 public synchronized Restlet createInboundRoot() {
 
 ChallengeAuthenticator restlet = (ChallengeAuthenticator) super.createInboundRoot();
 Router router = (Router) ((JsonpFilter)restlet.getNext()).getNext();
 
 
 router.attach("/alfrescoblog/test", TestResource.class);
 
 return restlet;
 }
}

Class AlfrescoBlogApplication extends ActivitiRestServicesApplication, gets defatuls Router and attaches the TestResource on path /alfrescoblog/test .

Overriding ActivitiRestServicesApplication

We need to explain activiti-rest that our Application Class is star in this game. Lets observe web.xml of activiti-rest web application

  <display-name>Activiti REST</display-name>
 <!-- Restlet adapter --> 
 <servlet> 
 <servlet-name>RestletServlet</servlet-name> 
 <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
 <init-param>
 <!-- Application class name -->
 <param-name>org.restlet.application</param-name>
 <param-value>org.activiti.rest.service.application.ActivitiRestServicesApplication</param-value>
...
 </servlet-mapping>

It is easy to see that instead of ActivitiRestServicesApplication we must put our Application class currently called AlfrescoBlogApplication.

 <!-- Application class name -->
 <param-name>org.restlet.application</param-name>
 <param-value>com.alfrescoblog.activitirestedit.AlfrescoBlogApplication</param-value>

Deploy REST service

Using maven command

mvn package

will result in building this project into jar file that we specified into our pom.xml file previously. Adding this jar to activiti-rest.war and modifying web.xml file as we explained will result in adding our REST service to desired path.

This REST service is located in localhost:8080/activiti-rest/service/

http://localhost:8080/activiti-rest/service/alfrescoblog/test

Summary

We have learned so far on

  • How to create maven project
  • How to add Resource
  • How to put Resource on desired path
  • How to override activitiApplication class

This concludes this article more will come in the future on how to use activiti services…

Don't be shellfish...Tweet about this on TwitterShare on LinkedInShare on Google+Share on RedditShare on Facebook

Was this helpful ?

  1. Hi, This was a great blog. Please can you provide similar steps for activiti 5.17 which uses spring mvc as the rest framework

    • Hello Himani,
      thank you!

      Extending Activiti-rest by adding more controllers is simple as 1 2 3.

      1: you need to create java project :)
      2: just add one class like this one to this package or add some spring config …

      package org.activiti.rest.service.api.members.activity;

      @RestController
      public class MyNewRestApi {

      private static Logger logger = Logger.getLogger(MyNewRestApi.class);

      @Resource
      private ManagementService managementService;

      @RequestMapping(value = "/mynewuri/getmehere", method = RequestMethod.GET)
      public String isUserActive(java.security.Principal principal) {
      return "Hello world";
      }
      }

      3: Share the alfrescoblog.com :)