4 Replies Latest reply on Feb 22, 2013 1:17 AM by snowstorm tech

    Starting process instance using the jBPM REST API and setting parameters

    Márton Steierlein Newbie

      Greetings!

       

      I would like to start a new process instance using jBPM REST interface. I have managed to figure out that the following URL needs to be called in order to start a new process instance:

       

      http://localhost:8080/gwt-console-server/rs/process/definition/<processId>/new_instance/ (POST)

       

      The problem is that this process has 3 variables and has a form to it which is rendered when starting the process instance. What I need to accomplish is to use the above REST interface and set the parameters without this form using only the rest interface.

       

      Is it possible to set the parameters this way? If yes, how?

       

      Thank you for your time!

       

      Márton

        • 1. Re: Starting process instance using the jBPM REST API and setting parameters
          Maciej Swiderski Master

          I believe you can achieve that with different url, that is dedicated to form processing functionality. Or at least give it a try....

           

          http://localhost:8080/gwt-console-server/rs/form/process/<processId>/complete (POST)

           

          it will try to parse request body to retrieve all paramters and set them as process variables (if they are defined in the process). Unfortunately it will not return process instance id as it is dedicated to process forms.

           

          HTH

          1 of 1 people found this helpful
          • 2. Re: Starting process instance using the jBPM REST API and setting parameters
            Márton Steierlein Newbie

            Thank you very much, I really appreciate your help! I believe the problem is that these variables are quite big and I guess this poses a limit to the size of the form's fields (on the server side). However if I cannot figure out how to pass a HashMap to ...../new_instance then this will be my best shot.

             

            Thanks again!

            • 3. Re: Starting process instance using the jBPM REST API and setting parameters
              Jose Miguel Loor Apprentice

              Hi

               

              According to this post

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

               

              it is possible to add a method that accepts parameters in the REST API to initiate a process with variables

               

              The post is quite old, so i would believe it is already present in the API

               

              But i am using BRMS 5.3 and with this code:

               

               

                                            AbstractHttpClient client = new DefaultHttpClient();

                                            HttpPost request = new HttpPost(url);

               

                               if (parameters != null && !parameters.isEmpty()) {

                                         request.setEntity(new UrlEncodedFormEntity(parameters,"UTF-8"));

                                                      }

               

                                                      AuthScope as = new AuthScope(SERVER, 8080);

                                                      UsernamePasswordCredentials upc = new UsernamePasswordCredentials(

                                                                          USER, PASSWORD);

                                                      client.getCredentialsProvider().setCredentials(as, upc);

               

                                                      BasicHttpContext localContext = new BasicHttpContext();

                                                      BasicScheme basicAuth = new BasicScheme();

                                                      localContext.setAttribute("preemptive-auth", basicAuth);

               

                                                      return client.execute(request, localContext);

               

              the process is started, but without any variables

               

              any ideas ??

              • 4. Re: Starting process instance using the jBPM REST API and setting parameters
                snowstorm tech Newbie

                Hi Jose Miguel Loor,

                Use the following programme may be help you,

                 

                 

                 

                import java.io.BufferedReader;

                import java.io.InputStream;

                import java.io.InputStreamReader;

                import java.nio.charset.Charset;

                import java.util.ArrayList;

                import java.util.HashMap;

                import java.util.List;

                import java.util.*;

                 

                 

                import org.apache.http.HttpResponse;

                import org.apache.http.NameValuePair;

                import org.apache.http.client.entity.UrlEncodedFormEntity;

                import org.apache.http.client.methods.HttpGet;

                import org.apache.http.client.methods.HttpPost;

                import org.apache.http.entity.mime.HttpMultipartMode;

                import org.apache.http.entity.mime.MultipartEntity;

                import org.apache.http.entity.mime.content.ContentBody;

                import org.apache.http.entity.mime.content.StringBody;

                import org.apache.http.impl.client.DefaultHttpClient;

                import org.apache.http.message.BasicNameValuePair;

                 

                 

                /*

                * To change this template, choose Tools | Templates

                * and open the template in the editor.

                */

                 

                 

                /**

                *

                * @author Shahid

                */

                public class StartProcessWV {

                     private static final String authentication_url = "http://localhost:8080/gwt-console-server/rs/identity/secure/j_security_check";

                //private static final String process_start_url = "http://localhost:8080/gwt-console-server/rs/process/definition/defaultPackage.ProcessWV/new_instance";

                private static final String process_start_url = "http://localhost:8080/gwt-console-server/rs/form/process/defaultPackage.ProcessWV/complete";

                 

                private static final String render_form_url = "http://localhost:8080/gwt-console-server/rs/form/process/defaultPackage.ProcessWV/render";

                     public static String KEY_USERNAME = "j_username";

                public static String KEY_PASSWORD = "j_password";

                private DefaultHttpClient httpClient = new DefaultHttpClient(); // keep this out of the method in order to reuse the object for calling other services without losing session

                public String authenticate(String address, String username, String password) {

                        String responseString = "";

                      //  new NameValuePair("j_username", username)

                        List<NameValuePair> formparams = new ArrayList<NameValuePair>();

                        formparams.add(new BasicNameValuePair(KEY_USERNAME, username));

                        formparams.add(new BasicNameValuePair(KEY_PASSWORD, password));

                 

                HttpPost httpPost = new HttpPost( address );

                       // HttpPost httpPost = new HttpPost("http://" + address + "/gwt-console-server/rs/process/j_security_check");

                        InputStreamReader inputStreamReader = null;

                        BufferedReader bufferedReader = null;

                        try {

                            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");

                            //UrlEncodedFormEntity entity=new 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();

                         //   System.out.println("line==>"+line);

                            while (line != null) {

                                stringBuilder.append(line);

                                line = bufferedReader.readLine();

                            }

                            responseString = stringBuilder.toString();

                        } catch (Exception e) {

                            //throw new RuntimeException(e);

                            System.out.println("e = " + 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;

                    }

                public String requestPostService(String url, Map<String, Object> parameters, boolean multipart) {

                        String responseString = "";

                 

                        MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                        List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();

                        if (parameters == null) {

                            parameters = new HashMap<String, Object>();

                        }

                        Set<String> keys = parameters.keySet();

                        for (Iterator<String> keysIterator = keys.iterator(); keysIterator.hasNext();) {

                            String keyString = keysIterator.next();

                            String value = parameters.get(keyString).toString();

                            formparams.add(new BasicNameValuePair(keyString, value));

                            if (multipart) {

                                try {

                                    StringBody stringBody = new StringBody(value, Charset.forName("UTF-8"));

                                    //System.out.println(stringBody.);

                                    multiPartEntity.addPart(keyString, (ContentBody) stringBody);

                                } catch (Exception e) {

                                    throw new RuntimeException(e);

                                }

                            }

                        }

                        HttpPost httpPost = new HttpPost(url);

                 

                        InputStreamReader inputStreamReader = null;

                        BufferedReader bufferedReader = null;

                        try {

                            if (multipart) {

                                httpPost.setEntity(multiPartEntity);

                            } else {

                                UrlEncodedFormEntity entity =new UrlEncodedFormEntity(formparams, "UTF-8");// new 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;

                }

                public String requestGetService(String url, Map<String, Object> parameters, boolean multipart) {

                        String responseString = "";

                 

                 

                 

                        HttpGet httpGet = new HttpGet(url);

                 

                        InputStreamReader inputStreamReader = null;

                        BufferedReader bufferedReader = null;

                        try {

                 

                            HttpResponse response = httpClient.execute(httpGet);

                            InputStream inputStream = response.getEntity().getContent();

                            inputStreamReader = new InputStreamReader(inputStream);

                            bufferedReader = new BufferedReader(inputStreamReader);

                            StringBuilder stringBuilder = new StringBuilder();

                            String line = bufferedReader.readLine();

                           // System.out.println("line = " + line);

                            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;

                }

                public static  void main(String args[])

                {

                    StartProcessWV startProcessWV=new StartProcessWV();

                     Map map=new HashMap();

                    map.put("name", "Shahid Iqbal");

                  //  map.put("age", "29");

                    System.out.println("Login Form==> "+startProcessWV.requestGetService(render_form_url, null, true));

                 

                   System.out.println(startProcessWV.authenticate(authentication_url, "admin", "admin")+"\n");

                  System.out.println("Render Form==> "+startProcessWV.requestGetService(render_form_url, null, true)+"\n");

                   System.out.println("Process start Output==> "+startProcessWV.requestPostService(process_start_url, map, true));

                 

                }

                }

                 

                change the process id in this code and use the following jars

                jars.png

                Your process should have the process form.

                 

                I hope so it will helpfull for you.

                Sorry for my english