use of org.mule.DefaultMuleMessage in project Activiti by Activiti.
the class MuleSendActivitiBehavior method execute.
public void execute(ActivityExecution execution) throws Exception {
String endpointUrlValue = this.getStringFromField(this.endpointUrl, execution);
String languageValue = this.getStringFromField(this.language, execution);
String payloadExpressionValue = this.getStringFromField(this.payloadExpression, execution);
String resultVariableValue = this.getStringFromField(this.resultVariable, execution);
String usernameValue = this.getStringFromField(this.username, execution);
String passwordValue = this.getStringFromField(this.password, execution);
ScriptingEngines scriptingEngines = Context.getProcessEngineConfiguration().getScriptingEngines();
Object payload = scriptingEngines.evaluate(payloadExpressionValue, languageValue, execution);
if (endpointUrlValue.startsWith("vm:")) {
LocalMuleClient client = this.getMuleContext().getClient();
MuleMessage message = new DefaultMuleMessage(payload, this.getMuleContext());
MuleMessage resultMessage = client.send(endpointUrlValue, message);
Object result = resultMessage.getPayload();
if (resultVariableValue != null) {
execution.setVariable(resultVariableValue, result);
}
} else {
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
if (usernameValue != null && passwordValue != null) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(usernameValue, passwordValue);
provider.setCredentials(new AuthScope("localhost", -1, "mule-realm"), credentials);
clientBuilder.setDefaultCredentialsProvider(provider);
}
HttpClient client = clientBuilder.build();
HttpPost request = new HttpPost(endpointUrlValue);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(payload);
oos.flush();
oos.close();
request.setEntity(new ByteArrayEntity(baos.toByteArray()));
} catch (Exception e) {
throw new ActivitiException("Error setting message payload", e);
}
byte[] responseBytes = null;
try {
// execute the POST request
HttpResponse response = client.execute(request);
responseBytes = IOUtils.toByteArray(response.getEntity().getContent());
} finally {
// release any connection resources used by the method
request.releaseConnection();
}
if (responseBytes != null) {
try {
ByteArrayInputStream in = new ByteArrayInputStream(responseBytes);
ObjectInputStream is = new ObjectInputStream(in);
Object result = is.readObject();
if (resultVariableValue != null) {
execution.setVariable(resultVariableValue, result);
}
} catch (Exception e) {
throw new ActivitiException("Failed to read response value", e);
}
}
}
this.leave(execution);
}
Aggregations