JBoss Community

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

created by snowstorm tech in jBPM - View the full discussion

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

   

}

}

 

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

Start a new discussion in jBPM at Community