[jboss-user] [jBPM] - Re: Human task client on a different user GUI

Pierpaolo Lombardi do-not-reply at jboss.com
Fri Sep 9 08:27:23 EDT 2011


Pierpaolo Lombardi [http://community.jboss.org/people/pierx83] created the discussion

"Re: Human task client on a different user GUI"

To view the discussion, visit: http://community.jboss.org/message/625741#625741

--------------------------------------------------------------
Hi,

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. 
About the start process, here my code:

**public** **void** startProcess(String username,String password,String processName, Map<String, Object> parameters) {
JBPMRestOperations jbpmRestOperations = new JBPMRestOperations ("localhost","8080");

String responseString = *this*.jbpmRestOperations.requestPostService(processName, parameters);
**if**(responseString.substring(101).startsWith("j_security_check")) //REST needs authentication

**this**.jbpmRestOperations.authenticate(username, password);

**this**.jbpmRestOperations.requestPostService(processName, parameters);}
and the JBPMRestOperations class is:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class JBPMRestOperations {
public String KEY_USERNAME = "j_username";
public String KEY_PASSWORD = "j_password";
private DefaultHttpClient httpClient; // keep this out of the method in order to reuse the object for calling other services without losing session
private String address;
public JBPMRestOperations(String host,String port){
httpClient = new DefaultHttpClient();
this.address = host+":"+port;
}
public String authenticate(String username, String password) {
String responseString = "";
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair(KEY_USERNAME, username));
formparams.add(new BasicNameValuePair(KEY_PASSWORD, password));
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(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 requestPostService(String processName, Map<String, Object> parameters) {
String responseString = "";
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
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));
}
HttpPost httpPost = new HttpPost("http://" + address + "/gwt-console-server/rs/process/definition/"+processName+"/new_instance_params");
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;

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



--------------------------------------------------------------

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

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110909/a412dbed/attachment-0001.html 


More information about the jboss-user mailing list