cancel
Showing results for 
Search instead for 
Did you mean: 

starting process through REST API

opal
Champ in-the-making
Champ in-the-making
Hi,

Does anybody know how to start the process through the REST API? I try do that in the following way:

@Test
public void testStartProcess() {
   try {
      DefaultHttpClient dhc = new DefaultHttpClient();
      HttpPost hp = new HttpPost("http://localhost:8080/activiti-explorer/process-instance");
      hp.setEntity(new StringEntity("{\"processDefinitionId\":\"helloworld:1\"}", "UTF-8"));
      HttpResponse hr = dhc.execute(hp);
      System.out.println(IOUtils.toString(hr.getEntity().getContent()));
   } catch (Exception e) {
      e.printStackTrace();
   }
}

But the response I obtain is:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
   org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
   org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
   org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
   org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
root cause
java.lang.NullPointerException
   org.apache.catalina.util.CharsetMapper.getCharset(CharsetMapper.java:106)
   org.apache.catalina.connector.Response.setLocale(Response.java:852)
   org.apache.catalina.connector.ResponseFacade.setLocale(ResponseFacade.java:323)
   javax.servlet.ServletResponseWrapper.setLocale(ServletResponseWrapper.java:201)
   org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1035)
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
   org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
   org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
   org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
   org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)

I don't know if I invoke correct webapp (activiti-explorer)? Do I also have to login to activiti before starting the process?
6 REPLIES 6

opal
Champ in-the-making
Champ in-the-making
Ok, it's better now. I've searched docs properly and now with this code:

DefaultHttpClient dhc = new DefaultHttpClient();
HttpPost hp = new HttpPost("http://localhost:8080/activiti-rest/service/process-instance");
hp.setEntity(new StringEntity("{\"processDefinitionId\":\"helloworld:1\"}", "UTF-8"));
HttpResponse processResponse = dhc.execute(hp);
System.out.println(IOUtils.toString(processResponse.getEntity().getContent()));
dhc.getConnectionManager().shutdown();

I get this response:
This request requires HTTP authentication ().

So the question now is how to perform authentication?

opal
Champ in-the-making
Champ in-the-making
Ok, done. Here is the final version of the code:

DefaultHttpClient dhc = new DefaultHttpClient();
dhc.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials("kermit", "kermit"));
HttpPost hp = new HttpPost("http://localhost:8080/activiti-rest/service/process-instance");
hp.setEntity(new StringEntity("{\"processDefinitionId\":\"helloworld:1\"}", "UTF-8"));
HttpResponse processResponse = dhc.execute(hp);
System.out.println(IOUtils.toString(processResponse.getEntity().getContent()));
dhc.getConnectionManager().shutdown();

I'm sorry for multiple posts, but at least it'll be easy to find the solution in one place.

frederikherema1
Star Contributor
Star Contributor
The rest-services in activiti use BASIC HTTP authentication. I presume you are using Apache HTTPComponents, which exposes an easy way to set the authentication, eg:

DefaultHttpClient httpclient = new DefaultHttpClient();

httpclient.getCredentialsProvider().setCredentials(
  new AuthScope("localhost", 8080),
  new UsernamePasswordCredentials("kermit", "kermit"));


frederikherema1
Star Contributor
Star Contributor
Didn't see your last reply. Looks like you found the solution Smiley Wink

opal
Champ in-the-making
Champ in-the-making
Yes ;] I've already found the solution. Nevertheless thanks for Your input.

gumba
Champ in-the-making
Champ in-the-making
hi all I tried to implement your solution but a get an error, How did you managed to solve your error:
First I tried this one:
   DefaultHttpClient dhttpclient = new DefaultHttpClient();

   dhttpclient.getCredentialsProvider().setCredentials(
       new AuthScope("localhost", 8080),
       new UsernamePasswordCredentials("kermit", "kermit"));
   HttpPost postUrl = new HttpPost(
     "http://localhost:8080/activiti-rest/service/process-instance");

  
   postUrl.setEntity(new StringEntity(
     "{\"processDefinitionId\":\"MailSender:1:16516\"}", "UTF-8"));

   HttpResponse processResponse = dhttpclient.execute(postUrl);

   System.out.println(IOUtils.toString(processResponse.getEntity()
     .getContent()));

   dhttpclient.getConnectionManager().shutdown();

I got the following error:
<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Forbidden</p>
<p>Authentication is required</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>



Then with this one a got another
the code:
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;

public class RestAPI {

/**
  * @param args
  */
public static void main(String[] args) {
  try {
   // TODO Auto-generated method stub
   DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080),
            new UsernamePasswordCredentials("kermit", "kermit"));
                
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(new HttpHost("localhost", 8080, "http"), basicAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        HttpPost postMethod = new HttpPost("http://localhost:8080/activiti-rest/service/process-instance");
     
           postMethod.setEntity(new StringEntity("{\"processDefinitionKey\":\"MailSender\"}", "UTF-8"));
           HttpResponse response = client.execute(postMethod, localcontext);
           System.out.println(IOUtils.toString(response.getEntity().getContent()));
           client.getConnectionManager().shutdown();
        } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }
 
       
       
}

}


the error:

<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Internal Server Error</p>
<p>Failed to retrieve the process definition parameters</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>


What am I doing wrong?