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