Java tutorial
/* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.activiti.mule; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Map; import org.activiti.engine.ActivitiException; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.Expression; import org.activiti.engine.impl.bpmn.behavior.AbstractBpmnActivityBehavior; import org.activiti.engine.impl.context.Context; import org.activiti.engine.impl.pvm.delegate.ActivityExecution; import org.activiti.engine.impl.scripting.ScriptingEngines; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.mule.DefaultMuleMessage; import org.mule.api.MuleContext; import org.mule.api.MuleMessage; import org.mule.api.client.LocalMuleClient; /** * @author Esteban Robles Luna */ public class MuleSendActivitiBehavior extends AbstractBpmnActivityBehavior { private static final long serialVersionUID = 1L; private MuleContext muleContext; private Expression endpointUrl; private Expression language; private Expression payloadExpression; private Expression resultVariable; private Expression username; private Expression password; 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 { HttpClient client = new HttpClient(); PostMethod request = new PostMethod(endpointUrlValue); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(payload); oos.flush(); oos.close(); request.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray())); } catch (Exception e) { throw new ActivitiException("Error setting message payload", e); } if (usernameValue != null && passwordValue != null) { client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(new AuthScope("localhost", -1, "mule-realm"), new UsernamePasswordCredentials(usernameValue, passwordValue)); request.setDoAuthentication(true); } byte[] response = null; try { // execute the POST request client.executeMethod(request); response = request.getResponseBody(); } finally { // release any connection resources used by the method request.releaseConnection(); } if (response != null) { try { ByteArrayInputStream in = new ByteArrayInputStream(response); 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); } protected MuleContext getMuleContext() { if (this.muleContext == null) { Map<Object, Object> beans = Context.getProcessEngineConfiguration().getBeans(); this.muleContext = (MuleContext) beans.get("muleContext"); } return this.muleContext; } protected String getStringFromField(Expression expression, DelegateExecution execution) { if (expression != null) { Object value = expression.getValue(execution); if (value != null) { return value.toString(); } } return null; } public Expression getEndpointUrl() { return endpointUrl; } public void setEndpointUrl(Expression endpointUrl) { this.endpointUrl = endpointUrl; } public Expression getPayloadExpression() { return payloadExpression; } public void setPayloadExpression(Expression payloadExpression) { this.payloadExpression = payloadExpression; } public Expression getResultVariable() { return resultVariable; } public void setResultVariable(Expression resultVariable) { this.resultVariable = resultVariable; } public Expression getLanguage() { return language; } public void setLanguage(Expression language) { this.language = language; } public Expression getUsername() { return username; } public void setUsername(Expression username) { this.username = username; } public Expression getPassword() { return password; } public void setPassword(Expression password) { this.password = password; } }