1 2 Previous Next 21 Replies Latest reply: Nov 4, 2013 10:43 AM by bennixview RSS

Process Parameters through REST API?

Franck cdsosi Newbie

Hi,

 

I have been searching for quite a long time but can't get around with problem. Maybe some of you have a solution, I hope so actually !

 

I desgin and deploy a process which has some process variables. I want to start that process and of course set (initialize) the values to so that they can be used when the process starts.

I have to say that there is no kind of human task or form in my process.

 

Is it possible to do that simple action through the REST API.

 

I try to add the parameters in the post request of .../gwt-console-server/rs/process/definition/{id}/new_instance but with no success => I thought this particular REST API would have parsed the parameters and their values in the body and then try to match some of them with process variables but it seems that it is not the case or maybe I did in a bad way.

 

Anyone one can help ?

 

Thanks

 

Regards

  • 1. Re: Process Parameters through REST API?
    Thomas Setiabudi Expert

    Hi Franck cdsosi,

     

    have you get through the authorization (login) of the gwt console server before sending that request?

     

    I tried a simple get request and always get the 401 unauthorized response.

     

     

    Regards,

    Thomas Setiabudi

  • 2. Re: Process Parameters through REST API?
    Franck cdsosi Newbie

    Hi Thomas,

     

    Answer to your question is yes but keep reading a little further !!!

     

    You are lucky since I spent quite a lot of time yesterday on that actually

    1. The trick is to first make a call to REST API Operation you want (e.g. .../new_instance) and then you normally get something about login issue (keep your session instance alive )
    2. Then you authenticate with the same session
    3. Finally you call the REST API you want and it is working !

     

    For further information you can have a look to this post : https://community.jboss.org/message/605467#605467, you should fix your problem.

     

    Leave a feedback if it is ok for you !

     

    If you succeed in passing parameters to a process via REST API don't forget to share as well

     

    Anyone else can help on my problem?

     

    Thanks

  • 3. Re: Process Parameters through REST API?
    Franck cdsosi Newbie

    I read some kind of solution in https://community.jboss.org/thread/196709 but I have no form in my process

    I just want to input process variables values at process starting. This can be done with Java code (ksession.startProcess({process_defintion_id}, params);), don't we have the opportunity to do that with REST API without coding something on server side of course?

     

    Kind Regards

  • 4. Re: Process Parameters through REST API?
    Maciej Swiderski Master

    as stated in that post you refer there is REST api for that, it is just refer to form resource (which is kind of misleading) but it accepts regular HTTP POST calls. Just make sure that whenever you send your requests to that URL you send it with enctype="multipart/form-data"

     

    HTH

  • 5. Re: Process Parameters through REST API?
    Franck cdsosi Newbie

    Thanks Maciej for helping.

     

    I try what you told me but with no success.

    Here are my steps :

    1- Make a first REST API Call

    2- Make an authenticate call => OK

    3- Make a HTTP Post call using a MultipartEntity with a single parameter => only one process variable in my process, REST API function used : http://localhost:8080/gwt-console-server/rs/form/process/process_id/complete

     

    Whenever I execute that sequence I get a HTTP 401 error code dealing with authentifcation problems at step 3 although I authenticate successfully at step 2. Can you give me some further help so that I can accomplish the 3 steps mentionned.

    Actually at step 3 it is as if my authentification had been lost... When I sysout the response I get the same message at step 1 and 3.

     

    HTH for helping me

     

    Regards

  • 6. Re: Process Parameters through REST API?
    Thomas Setiabudi Expert

    Hi Franck cdsosi,

     

    Thank you for pointing me to the discussion about how to authenticate to the JBPM Console, I manage to get the authentication works.

     

    on the same discussion, they try to modify the jbpm console to accept parameters

    https://community.jboss.org/message/613793#613793

     

    maybe you can give it a try?

     

    Regards,

    Thomas Setiabudi

  • 7. Re: Process Parameters through REST API?
    Franck cdsosi Newbie

    Hello Thomas,

     

    Thanks for helping as well

     

    Actually I had this solution in mind which is quite simple that is to say coding a new REST function to accept parameters but that implies building the war and so on.

    My first goal is to test jBPM 5 by using what is effectively available and collect information from user documentation or community if needed (and it is a real need !)  to make basic self made processes working to realize what it is really possible to do with this BPM solution.

    I may have a try to a new REST function a bit later.

     

    Actually as some users said that it was possible to pass parameters through a REST API function (http://localhost:8080/gwt-console-server/rs/form/process/process_id/complete) I wanted to achieve that but with no success for the moment

     

    Kind Regards,

     

    Franck

  • 8. Re: Process Parameters through REST API?
    Thomas Setiabudi Expert

    Hi Franck,

     

    I tried the http://localhost:8080/gwt-console-server/rs/form/process/process_id/complete

     

    here is my code

     

    public String instantiateDemoEvaluation(String address, String username,
       String reason, String processName) {
      String responseString = "";
      List<NameValuePair> formparams = new ArrayList<NameValuePair>();
      formparams.add(new BasicNameValuePair("employee", "thomas"));
      formparams.add(new BasicNameValuePair("reason", "test"));

      HttpPost httpPost = new HttpPost("http://" + "localhost:8888"
        + "/gwt-console-server/rs/form/process/" + "com.sample.evaluation"
        + "/complete");
      InputStreamReader inputStreamReader = null;
      BufferedReader bufferedReader = null;

      try {
       UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
         "UTF-8");// UrlEncodedFormEntity(formparams,
            // "multipart/form-data");
      
       httpPost.setEntity(entity);
       HttpResponse response = httpClient.execute(httpPost);
       InputStream inputStream = response.getEntity().getContent();
       inputStreamReader = new InputStreamReader(inputStream);
       bufferedReader = new BufferedReader(inputStreamReader);
       StringBuilder stringBuilder = new StringBuilder();
       String line = bufferedReader.readLine();
       while (line != null) {
        stringBuilder.append(line);
        line = bufferedReader.readLine();
       }
       responseString = stringBuilder.toString();
      } catch (Exception e) {
       throw new RuntimeException(e);
      } finally {
       if (inputStreamReader != null) {
        try {
         inputStreamReader.close();
        } catch (Exception e) {
         throw new RuntimeException(e);
        }
       }
       if (bufferedReader != null) {
        try {
         bufferedReader.close();
        } catch (Exception e) {
         throw new RuntimeException(e);
        }
       }
      }

      return responseString;
    }

     

     

    and I got this error

     

    <html><head><title>JBoss Web/7.0.1.Final - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>org.jboss.resteasy.spi.UnhandledException: java.lang.RuntimeException: Timeout : unable to retrieve Task org.jboss.resteasy.core.SynchronousDispatcher.unwrapException(SynchronousDispatcher.java:345) org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:321) org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:534) org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496) org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) javax.servlet.http.HttpServlet.service(HttpServlet.java:847) org.jboss.bpm.console.server.util.GWTJsonFilter.doFilter(GWTJsonFilter.java:59)</pre></p><p><b>root cause</b> <pre>java.lang.RuntimeException: Timeout : unable to retrieve Task org.jbpm.task.service.responsehandlers.BlockingGetTaskResponseHandler.getTask(BlockingGetTaskResponseHandler.java:41) org.jbpm.task.service.SyncTaskServiceWrapper.getTask(SyncTaskServiceWrapper.java:369) org.jbpm.integration.console.forms.TaskFormDispatcher.provideForm(TaskFormDispatcher.java:64) org.jbpm.integration.console.forms.FormDispatcherComposite.provideForm(FormDispatcherComposite.java:50) org.jboss.bpm.console.server.FormProcessingFacade.provideForm(FormProcessingFacade.java:205) org.jboss.bpm.console.server.FormProcessingFacade.renderTaskUI(FormProcessingFacade.java:127) sun.reflect.GeneratedMethodAccessor101.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140) org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:255) org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:220) org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:209) org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:519) org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496) org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) javax.servlet.http.HttpServlet.service(HttpServlet.java:847) org.jboss.bpm.console.server.util.GWTJsonFilter.doFilter(GWTJsonFilter.java:59)</pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the JBoss Web/7.0.1.Final logs.</u></p><HR size="1" noshade="noshade"><h3>JBoss Web/7.0.1.Final</h3></body></html>

  • 9. Re: Process Parameters through REST API?
    Thomas Setiabudi Expert

    I though it is the problem with enctype multipart

     

    so I altered to this:

     

    public String instantiateDemoEvaluation(String address, String username,
       String reason, String processName) {
      String responseString = "";
      List<NameValuePair> formparams = new ArrayList<NameValuePair>();
      formparams.add(new BasicNameValuePair("employee", "thomas"));
      formparams.add(new BasicNameValuePair("reason", "test"));

      HttpPost httpPost = new HttpPost("http://" + "localhost:8888"
        + "/gwt-console-server/rs/form/process/" + "com.sample.evaluation"
        + "/complete");
      InputStreamReader inputStreamReader = null;
      BufferedReader bufferedReader = null;

      try {

     

      
       StringBody strUsername = new StringBody("thomas");
       StringBody strReason = new StringBody("test");
      
       MultipartEntity reqEntity = new MultipartEntity();
       reqEntity.addPart("employee", strUsername);
       reqEntity.addPart("reason", strReason);
      
      
       httpPost.setEntity(reqEntity);
       HttpResponse response = httpClient.execute(httpPost);
       InputStream inputStream = response.getEntity().getContent();
       inputStreamReader = new InputStreamReader(inputStream);
       bufferedReader = new BufferedReader(inputStreamReader);
       StringBuilder stringBuilder = new StringBuilder();
       String line = bufferedReader.readLine();
       while (line != null) {
        stringBuilder.append(line);
        line = bufferedReader.readLine();
       }
       responseString = stringBuilder.toString();
      } catch (Exception e) {
       throw new RuntimeException(e);
      } finally {
       if (inputStreamReader != null) {
        try {
         inputStreamReader.close();
        } catch (Exception e) {
         throw new RuntimeException(e);
        }
       }
       if (bufferedReader != null) {
        try {
         bufferedReader.close();
        } catch (Exception e) {
         throw new RuntimeException(e);
        }
       }
      }

      return responseString;
    }

     

    but I got the same error as my previous post

  • 10. Re: Process Parameters through REST API?
    Thomas Setiabudi Expert

    however, when I try the get form

    "http://localhost:8080/gwt-console-server/rs/form/process/com.sample.evaluation/render"

     

    I can get the form HTML just fine

     

    <html><body><h2>Start Performance Evaluation</h2><hr><form action="complete" method="POST" enctype="multipart/form-data">Please fill in your username: <input type="text" name="employee" /></BR>Reason:<BR/><textarea cols="50" rows="5" name="reason"></textarea></BR><input type="submit" value="Complete"></form></body></html>

     

     

     

    So any clue about how to use this Rest

    http://localhost:8080/gwt-console-server/rs/form/process/com.sample.evaluation/complete  ?

  • 11. Re: Process Parameters through REST API?
    Thomas Setiabudi Expert

    Hi Franck,

     

    Finally Made It!

     

    so here is how to do it.

     

    first, you have to render the form

     

    something like this

     

    responseString = requestGetService(
        "http://localhost:8080/gwt-console-server/rs/form/process/"
          + "com.sample.evaluation" + "/render", null, false);

      if (responseString.contains("<html>")) {
       responseString = authenticate(address, "krisv", "krisv");

       responseString = requestGetService(
         "http://localhost:8080/gwt-console-server/rs/form/process/"
           + "com.sample.evaluation" + "/render", null, false);
      }

     

    ok. this will get you the input form

     

    then call the form process complete like this

     

    String address = "http://localhost:8080/gwt-console-server/rs/form/process/com.sample.evaluation/complete";
      String responseString = "";
     
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("employee", "thomas");
      map.put("reason", "theReason");

      responseString = requestPostMultipartService(address, map);
      if (responseString.contains("<html>")) {
       responseString = authenticate(address, "krisv", "krisv");
       responseString = requestPostMultipartService(address, map);
      }

     

    the result should be:

    <div style='font-family:sans-serif; padding:10px;'><h3>Successfully processed input</h3><p/>You can now close this window.</div>

     

     

     

     

    and for the functions that I use are:

    private String requestPostMultipartService(String url,
       Map<String, Object> parameters) {
      String responseString = "";
      try {
       HttpPost httpPost = new HttpPost(url);
       if (parameters == null)
        parameters = new HashMap<String, Object>();

       MultipartEntity entity = new MultipartEntity(
         HttpMultipartMode.BROWSER_COMPATIBLE);

       Set<String> keys = parameters.keySet();
       for (Iterator<String> keysIterator = keys.iterator(); keysIterator
         .hasNext();) {
        String keyString = keysIterator.next();
        String value = parameters.get(keyString).toString();
        entity.addPart(keyString, new StringBody(value));
       }
       httpPost.setEntity(entity);
       HttpResponse response = httpClient.execute(httpPost);
       responseString = this.getRequestString(response);
      } catch (Exception e) {

      }
      return responseString;
    }

     

     

    and

     

    private String getRequestString(HttpResponse response) throws Exception {
      InputStreamReader inputStreamReader = null;
      BufferedReader bufferedReader = null;
      String req = "";
      InputStream inputStream = response.getEntity().getContent();
      inputStreamReader = new InputStreamReader(inputStream);
      bufferedReader = new BufferedReader(inputStreamReader);
      StringBuilder stringBuilder = new StringBuilder();
      String line = bufferedReader.readLine();
      while (line != null) {
       stringBuilder.append(line);
       line = bufferedReader.readLine();
      }
      req = stringBuilder.toString();
      return req;
    }

     

     

    just give it a try and let me know if it works for you

     

    Regards,

    Thomas Setiabudi

     

  • 12. Re: Process Parameters through REST API?
    Franck cdsosi Newbie

    Hi Thomas,

     

    Thanks for your investigations. I may have a look a bit later since holidays are coming soon and i have some other things to look at also...

     

    Anyway thanks again, I will write something when tests would have been made.

     

    Regards

     

    Franck

  • 13. Re: Process Parameters through REST API?
    Franck cdsosi Newbie

    Thanks Thomas for sharing your solution.

     

    I could not wait so I had to test and it is working but what a long way to achieve that...

     

    I think the best solution is to code a new REST function to make it work in an easy way and so that process instance information for instance can be retrieved.

     

    See you on other posts

     

    Regards,

     

    Franck

  • 14. Re: Process Parameters through REST API?
    Mai A. S Newbie

    Thanks a lot Thomas
    I have the same problem

    I am beginner at jbpm and I want to try your code

    could you please send the functions of requestGetService and authenticate

     

    Thanks a lot and sorry for disturbance.

1 2 Previous Next