Discussion:
[jboss-user] [jBPM] - Starting process instance using the jBPM REST API and setting parameters
Márton Steierlein
2012-03-12 15:01:16 UTC
Permalink
Márton Steierlein [https://community.jboss.org/people/stmarci] created the discussion

"Starting process instance using the jBPM REST API and setting parameters"

To view the discussion, visit: https://community.jboss.org/message/723219#723219

--------------------------------------------------------------
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/ 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
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/723219#723219]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Maciej Swiderski
2012-03-16 06:59:53 UTC
Permalink
Maciej Swiderski [https://community.jboss.org/people/swiderski.maciej] created the discussion

"Re: Starting process instance using the jBPM REST API and setting parameters"

To view the discussion, visit: https://community.jboss.org/message/724229#724229

--------------------------------------------------------------
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
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/724229#724229]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Márton Steierlein
2012-03-19 08:25:24 UTC
Permalink
Márton Steierlein [https://community.jboss.org/people/stmarci] created the discussion

"Re: Starting process instance using the jBPM REST API and setting parameters"

To view the discussion, visit: https://community.jboss.org/message/724602#724602

--------------------------------------------------------------
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!
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/724602#724602]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Jose Miguel Loor
2013-02-20 22:33:32 UTC
Permalink
Jose Miguel Loor [https://community.jboss.org/people/jmiguel77] created the discussion

"Re: Starting process instance using the jBPM REST API and setting parameters"

To view the discussion, visit: https://community.jboss.org/message/798679#798679

--------------------------------------------------------------
Hi

According to this post
https://community.jboss.org/message/612597#612597 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
                              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 ??
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/798679#798679]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
snowstorm tech
2013-02-22 06:13:26 UTC
Permalink
snowstorm tech [https://community.jboss.org/people/snowstormuser] created the discussion

"Re: Starting process instance using the jBPM REST API and setting parameters"

To view the discussion, visit: https://community.jboss.org/message/799005#799005

--------------------------------------------------------------
Hi https://community.jboss.org/people/jmiguel77 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.
*/
/**
*
*/
public class StartProcessWV {
     private static final String authentication_url = " http://localhost:8080/gwt-console-server/rs/identity/secure/j_security_check 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 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 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 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:// 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));
}
}
please change the process id and your process should have the process form.

I hope so it will helpfull for you.
Sorry for my english
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/799005#799005]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Loading...