<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
<table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
<tbody>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
<tbody>
<tr>
<td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
<h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
<!-- To have a header image/logo replace the name below with your img tag -->
<!-- Email clients will render the images when the message is read so any image -->
<!-- must be made available on a public server, so that all recipients can load the image. -->
<a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Re: Human task client on a different user GUI
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/pierx83">Pierpaolo Lombardi</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/625741#625741">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hi,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>actually I use the REST interface just to start a process. The Task management activities are done by using the TaskClient library provided with jbpm. </p><p>About the start process, here my code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-size: 10pt;"><span lang="IT"><p><strong style="color: #7f0055; font-size: 10pt;"><strong style="color: #7f0055; font-size: 10pt;">public</strong></strong><span style="font-size: 10pt;"> </span><strong style="color: #7f0055; font-size: 10pt;"><strong style="color: #7f0055; font-size: 10pt;">void</strong></strong><span style="font-size: 10pt;"> startProcess(String username,String password,String processName, Map<String, Object> parameters) {<span style="font-size: 2;"><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></span><span style="font-size: 10pt; font-family: Courier New;">JBPMRestOperations </span><span style="color: #0000c0; font-size: 10pt;">jbpmRestOperations</span><span style="color: #0000c0; font-size: 10pt; font-family: Courier New;"> = new </span><span style=": ; font-size: 2; font-family: 'Courier New';">JBPMRestOperations ("localhost","8080");</span></span></p><span style=": ; font-size: 2; font-family: 'Courier New';"><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></span><p><span style="font-family: Times New Roman;"><span style="font-size: 10pt;">String responseString = </span><strong style=": ; color: #7f0055; font-size: 2;">this</strong></span><span style="font-family: Times New Roman;"><span style="font-size: 10pt;">.</span><span style="color: #0000c0; font-size: 10pt;">jbpmRestOperations</span></span><span style="font-size: 10pt;"><span style="font-family: Times New Roman;">.requestPostService(processName, parameters);</span><span style="font-size: 2;"><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></span><strong style="color: #7f0055; font-size: 10pt;"><strong style="color: #7f0055; font-size: 10pt;">if</strong></strong><span style="font-size: 10pt;">(responseString.substring(101).startsWith(</span><span style="color: #2a00ff; font-size: 10pt;">"j_security_check"</span><span style="font-size: 10pt;">))</span><span style=": ; font-size: 2; font-family: 'Courier New';"> //REST needs authentication</span></span></p><span style="font-size: 10pt;"><span style="font-size: 2;"><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></span><strong style="color: #7f0055; font-size: 10pt;"><strong style="color: #7f0055; font-size: 10pt;">this</strong></strong><span style="font-size: 10pt;">.</span><span style="color: #0000c0; font-size: 10pt;">jbpmRestOperations</span><span style="font-size: 2;">.authenticate(username, password);</span></span><span style="font-size: 10pt;"><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><span style="font-size: 2;"><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></span><strong style="color: #7f0055; font-size: 10pt;"><strong style="color: #7f0055; font-size: 10pt;">this</strong></strong><span style="font-size: 10pt;">.</span><span style="color: #0000c0; font-size: 10pt;">jbpmRestOperations</span><span style="font-size: 2;">.requestPostService(processName, parameters);</span></span><span style="font-size: 10pt;"><p>}</p></span><span style="font-size: 10pt; font-family: Courier New;"><p>and the JBPMRestOperations class is:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">  </p><p>import java.io.BufferedReader;</p><p>import java.io.InputStream;</p><p>import java.io.InputStreamReader;</p><p>import java.util.ArrayList;</p><p>import java.util.HashMap;</p><p>import java.util.Iterator;</p><p>import java.util.List;</p><p>import java.util.Map;</p><p>import java.util.Set;</p><p>import org.apache.http.HttpResponse;</p><p>import org.apache.http.NameValuePair;</p><p>import org.apache.http.client.entity.UrlEncodedFormEntity;</p><p>import org.apache.http.client.methods.HttpPost;</p><p>import org.apache.http.impl.client.DefaultHttpClient;</p><p>import org.apache.http.message.BasicNameValuePair;</p><p>public class JBPMRestOperations {</p><p>public String KEY_USERNAME = "j_username";</p><p>public String KEY_PASSWORD = "j_password";</p><p>private DefaultHttpClient httpClient; // keep this out of the method in order to reuse the object for calling other services without losing session</p><p>private String address;</p><p>public JBPMRestOperations(String host,String port){</p><p>httpClient = new DefaultHttpClient();</p><p>this.address = host+":"+port;</p><p>}</p><p>public String authenticate(String username, String password) {</p><p>String responseString = "";</p><p>List<NameValuePair> formparams = new ArrayList<NameValuePair>();</p><p>formparams.add(new BasicNameValuePair(KEY_USERNAME, username));</p><p>formparams.add(new BasicNameValuePair(KEY_PASSWORD, password));</p><p><span>HttpPost httpPost = new HttpPost("</span><a class="jive-link-external-small" href="http://" target="_blank">http://</a><span>" + address + "/gwt-console-server/rs/process/j_security_check");</span></p><p>InputStreamReader inputStreamReader = null;</p><p>BufferedReader bufferedReader = null;</p><p>try {</p><p>UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");//UrlEncodedFormEntity(formparams, "multipart/form-data");</p><p>httpPost.setEntity(entity);</p><p>HttpResponse response = httpClient.execute(httpPost);</p><p>InputStream inputStream = response.getEntity().getContent();</p><p>inputStreamReader = new InputStreamReader(inputStream);</p><p>bufferedReader = new BufferedReader(inputStreamReader);</p><p>StringBuilder stringBuilder = new StringBuilder();</p><p>String line = bufferedReader.readLine();</p><p>while (line != null) {</p><p>stringBuilder.append(line);</p><p>line = bufferedReader.readLine();</p><p>}</p><p>responseString = stringBuilder.toString();</p><p>} catch (Exception e) {</p><p>throw new RuntimeException(e);</p><p>} finally {</p><p>if (inputStreamReader != null) {</p><p>try {</p><p>inputStreamReader.close();</p><p>} catch (Exception e) {</p><p>throw new RuntimeException(e);</p><p>}</p><p>}</p><p>if (bufferedReader != null) {</p><p>try {</p><p>bufferedReader.close();</p><p>} catch (Exception e) {</p><p>throw new RuntimeException(e);</p><p>}</p><p>}</p><p>}</p><p>return responseString;</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">  </p><p>public String requestPostService(String processName, Map<String, Object> parameters) {</p><p>String responseString = "";</p><p>List<NameValuePair> formparams = new ArrayList<NameValuePair>();</p><p>if (parameters == null) {</p><p>parameters = new HashMap<String, Object>();</p><p>}</p><p>Set<String> keys = parameters.keySet();</p><p>for (Iterator<String> keysIterator = keys.iterator(); keysIterator.hasNext();) {</p><p>String keyString = keysIterator.next();</p><p>String value = parameters.get(keyString).toString();</p><p>formparams.add(new BasicNameValuePair(keyString, value));</p><p>}</p><p><span>HttpPost httpPost = new HttpPost("</span><a class="jive-link-external-small" href="http://" target="_blank">http://</a><span>" + address + "/gwt-console-server/rs/process/definition/"+processName+"/new_instance_params");</span></p><p>InputStreamReader inputStreamReader = null;</p><p>BufferedReader bufferedReader = null;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">  </p><p>try {</p><p>UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");</p><p>httpPost.setEntity(entity);</p><p>HttpResponse response = httpClient.execute(httpPost);</p><p>InputStream inputStream = response.getEntity().getContent();</p><p>inputStreamReader = new InputStreamReader(inputStream);</p><p>bufferedReader = new BufferedReader(inputStreamReader);</p><p>StringBuilder stringBuilder = new StringBuilder();</p><p>String line = bufferedReader.readLine();</p><p>while (line != null) {</p><p>stringBuilder.append(line);</p><p>line = bufferedReader.readLine();</p><p>}</p><p>responseString = stringBuilder.toString();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">  </p><p>} catch (Exception e) {</p><p>throw new RuntimeException(e);</p><p>} finally {</p><p>if (inputStreamReader != null) {</p><p>try {</p><p>inputStreamReader.close();</p><p>} catch (Exception e) {</p><p>throw new RuntimeException(e);</p><p>}</p><p>}</p><p>if (bufferedReader != null) {</p><p>try {</p><p>bufferedReader.close();</p><p>} catch (Exception e) {</p><p>throw new RuntimeException(e);</p><p>}</p><p>}</p><p>}</p><p>return responseString;</p><p>}</p><p>}</p></span></span></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><div class="mcePaste" id="_mcePaste" style="position: absolute; top: 0px; left: 0px;"></div></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/625741#625741">going to Community</a></p>
<p style="margin: 0;">Start a new discussion in jBPM at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>