org.activiti.camel.ExchangeUtils Maven / Gradle / Ivy
/* 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.camel;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.Exchange;
/**
* This class contains one method - prepareVariables - that is used to copy variables from Camel into Activiti.
*
* @author Ryan Johnston (@rjfsu), Tijs Rademakers
*/
public class ExchangeUtils {
public static final String CAMELBODY = "camelBody";
protected static final String IGNORE_MESSAGE_PROPERTY = "CamelMessageHistory";
/**
* Copies variables from Camel into Activiti.
*
* This method will copy the Camel body to the "camelBody" variable. It will copy the Camel body to individual variables within Activiti if it is of type
* Map<String, Object> or it will copy the Object as it comes.
*
* - If the copyVariablesFromProperties parameter is set on the endpoint, the properties are copied instead
* - If the copyCamelBodyToBodyAsString parameter is set on the endpoint, the camelBody is converted to java.lang.String and added as a camelBody variable,
* unless it is a Map<String, Object>
* - If the copyVariablesFromHeader parameter is set on the endpoint, each Camel Header will be copied to an individual variable within Activiti.
*
*
* @param exchange The Camel Exchange object
* @param activitiEndpoint The ActivitiEndpoint implementation
* @return A Map<String, Object> containing all of the variables to be used in Activiti
*/
public static Map prepareVariables(Exchange exchange, ActivitiEndpoint activitiEndpoint) {
boolean shouldReadFromProperties = activitiEndpoint.isCopyVariablesFromProperties();
Map camelVarMap = null;
if (shouldReadFromProperties) {
camelVarMap = exchange.getProperties();
// filter camel property that can't be serializable for camel version after 2.12.x+
Map newCamelVarMap = new HashMap();
for (String s : camelVarMap.keySet()) {
if (IGNORE_MESSAGE_PROPERTY.equalsIgnoreCase(s) == false) {
newCamelVarMap.put(s, camelVarMap.get(s));
}
}
camelVarMap = newCamelVarMap;
} else {
camelVarMap = new HashMap();
Object camelBody = null;
if (exchange.hasOut())
camelBody = exchange.getOut().getBody();
else
camelBody = exchange.getIn().getBody();
if(camelBody instanceof Map,?>) {
Map,?> camelBodyMap = (Map,?>)camelBody;
for (@SuppressWarnings("rawtypes") Map.Entry e : camelBodyMap.entrySet()) {
if (e.getKey() instanceof String) {
camelVarMap.put((String) e.getKey(), e.getValue());
}
}
} else {
if(activitiEndpoint.isCopyCamelBodyToBodyAsString() && !(camelBody instanceof String)) {
camelBody = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, camelBody);
}
camelVarMap.put(CAMELBODY, camelBody);
}
if(activitiEndpoint.isCopyVariablesFromHeader()) {
for(Map.Entry header : exchange.getIn().getHeaders().entrySet()) {
camelVarMap.put(header.getKey(), header.getValue());
}
}
}
return camelVarMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy