use of org.apache.synapse.mediators.template.TemplateContext in project wso2-synapse by wso2.
the class MessageHelper method cloneMessageContext.
/**
* This method will simulate cloning the message context and creating an exact copy of the
* passed message. One should use this method with care; that is because, inside the new MC,
* most of the attributes of the MC like opCtx and so on are still kept as references inside
* the axis2 MessageContext for performance improvements. (Note: U dont have to worrie
* about the SOAPEnvelope, it is a cloned copy and not a reference from any other MC)
* @param synCtx - this will be cloned
* @param cloneSoapEnvelope whether to clone the soap envelope
* @return cloned Synapse MessageContext
* @throws AxisFault if there is a failure in creating the new Synapse MC or in a failure in
* clonning the underlying axis2 MessageContext
*
* @see MessageHelper#cloneAxis2MessageContext
*/
public static MessageContext cloneMessageContext(MessageContext synCtx, boolean cloneSoapEnvelope) throws AxisFault {
// creates the new MessageContext and clone the internal axis2 MessageContext
// inside the synapse message context and place that in the new one
MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
axis2MC.setAxis2MessageContext(cloneAxis2MessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext(), cloneSoapEnvelope));
newCtx.setConfiguration(synCtx.getConfiguration());
newCtx.setEnvironment(synCtx.getEnvironment());
newCtx.setContextEntries(synCtx.getContextEntries());
// set the parent correlation details to the cloned MC -
// for the use of aggregation like tasks
newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
// copying the core parameters of the synapse MC
newCtx.setTo(synCtx.getTo());
newCtx.setReplyTo(synCtx.getReplyTo());
newCtx.setSoapAction(synCtx.getSoapAction());
newCtx.setWSAAction(synCtx.getWSAAction());
newCtx.setResponse(synCtx.isResponse());
// copy all the synapse level properties to the newCtx
for (Object o : synCtx.getPropertyKeySet()) {
// If there are non String keyed properties neglect them rather than trow exception
if (o instanceof String) {
String strkey = (String) o;
Object obj = synCtx.getProperty(strkey);
if (obj instanceof String) {
// No need to do anything since Strings are immutable
} else if (obj instanceof ArrayList) {
if (log.isDebugEnabled()) {
log.debug("Deep clone Started for ArrayList property: " + strkey + ".");
}
// Call this method to deep clone ArrayList
obj = cloneArrayList((ArrayList) obj);
if (log.isDebugEnabled()) {
log.debug("Deep clone Ended for ArrayList property: " + strkey + ".");
}
} else if (obj instanceof Stack && strkey.equals(SynapseConstants.SYNAPSE__FUNCTION__STACK)) {
if (log.isDebugEnabled()) {
log.debug("Deep clone for Template function stack");
}
obj = getClonedTemplateStack((Stack<TemplateContext>) obj);
} else if (obj instanceof OMElement) {
if (log.isDebugEnabled()) {
log.debug("Deep clone for OMElement");
}
obj = (OMElement) ((OMElement) obj).cloneOMElement();
} else if (obj instanceof ResponseState) {
// do nothing and let the same reference to go to the cloned context
} else {
/**
* Need to add conditions according to type if found in
* future
*/
if (log.isDebugEnabled()) {
log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
}
}
newCtx.setProperty(strkey, obj);
}
}
// Make deep copy of fault stack so that parent will not be lost it's fault stack
Stack<FaultHandler> faultStack = synCtx.getFaultStack();
if (!faultStack.isEmpty()) {
List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
newFaultStack.addAll(faultStack);
for (FaultHandler faultHandler : newFaultStack) {
if (faultHandler != null) {
newCtx.pushFaultHandler(faultHandler);
}
}
}
Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
if (functionStack != null) {
newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
}
if (log.isDebugEnabled()) {
log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
}
// Copy ContinuationStateStack from original MC to the new MC
if (synCtx.isContinuationEnabled()) {
Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
newCtx.setContinuationEnabled(true);
for (ContinuationState continuationState : continuationStates) {
if (continuationState != null) {
newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
}
}
}
newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
return newCtx;
}
use of org.apache.synapse.mediators.template.TemplateContext in project wso2-synapse by wso2.
the class MessageHelper method getClonedTemplateStack.
/**
* Get a clone of a Template Function stack
*
* @param oriTemplateStack original template function stack to be cloned
* @return clone of a Template Function stack
*/
public static Stack<TemplateContext> getClonedTemplateStack(Stack<TemplateContext> oriTemplateStack) {
Stack<TemplateContext> clonedTemplateStack = new Stack<TemplateContext>();
for (TemplateContext oriTemplateCtx : oriTemplateStack) {
TemplateContext clonedTemplateCtx = new TemplateContext(oriTemplateCtx.getName(), oriTemplateCtx.getParameters());
Map oriValueMap = oriTemplateCtx.getMappedValues();
Map clonedValueMap = new HashMap();
for (Object key : oriValueMap.keySet()) {
Object value = oriValueMap.get(key);
if (value instanceof ArrayList) {
value = cloneArrayList((ArrayList<Object>) value);
}
clonedValueMap.put(key, value);
}
clonedTemplateCtx.setMappedValues(clonedValueMap);
clonedTemplateStack.push(clonedTemplateCtx);
}
return clonedTemplateStack;
}
use of org.apache.synapse.mediators.template.TemplateContext in project wso2-synapse by wso2.
the class MessageHelper method cloneMessageContextForAggregateMediator.
public static MessageContext cloneMessageContextForAggregateMediator(MessageContext synCtx) throws AxisFault {
// creates the new MessageContext and clone the internal axis2 MessageContext
// inside the synapse message context and place that in the new one
MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
axis2MC.setAxis2MessageContext(cloneAxis2MessageContextForAggregate(((Axis2MessageContext) synCtx).getAxis2MessageContext()));
newCtx.setConfiguration(synCtx.getConfiguration());
newCtx.setEnvironment(synCtx.getEnvironment());
newCtx.setContextEntries(synCtx.getContextEntries());
// set the parent correlation details to the cloned MC -
// for the use of aggregation like tasks
newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
// copying the core parameters of the synapse MC
newCtx.setTo(synCtx.getTo());
newCtx.setReplyTo(synCtx.getReplyTo());
newCtx.setSoapAction(synCtx.getSoapAction());
newCtx.setWSAAction(synCtx.getWSAAction());
newCtx.setResponse(synCtx.isResponse());
// copy all the synapse level properties to the newCtx
for (Object o : synCtx.getPropertyKeySet()) {
// throw exception
if (o instanceof String) {
/**
* Clone the properties and add to new context
* If not cloned can give errors in target configuration
*/
String strkey = (String) o;
Object obj = synCtx.getProperty(strkey);
if (obj instanceof String) {
// No need to do anything since Strings are immutable
} else if (obj instanceof ArrayList) {
if (log.isDebugEnabled()) {
log.warn("Deep clone Started for ArrayList property: " + strkey + ".");
}
// Call this method to deep clone ArrayList
obj = cloneArrayList((ArrayList) obj);
if (log.isDebugEnabled()) {
log.warn("Deep clone Ended for ArrayList property: " + strkey + ".");
}
} else {
/**
* Need to add conditions according to type if found in
* future
*/
if (log.isDebugEnabled()) {
log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
}
}
newCtx.setProperty(strkey, obj);
}
}
// Make deep copy of fault stack so that parent will not be lost it's fault stack
Stack<FaultHandler> faultStack = synCtx.getFaultStack();
if (!faultStack.isEmpty()) {
List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
newFaultStack.addAll(faultStack);
for (FaultHandler faultHandler : newFaultStack) {
if (faultHandler != null) {
newCtx.pushFaultHandler(faultHandler);
}
}
}
Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
if (functionStack != null) {
newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
}
if (log.isDebugEnabled()) {
log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
}
// Copy ContinuationStateStack from original MC to the new MC
if (synCtx.isContinuationEnabled()) {
Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
newCtx.setContinuationEnabled(true);
for (ContinuationState continuationState : continuationStates) {
if (continuationState != null) {
newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
}
}
}
newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
return newCtx;
}
use of org.apache.synapse.mediators.template.TemplateContext in project wso2-synapse by wso2.
the class GetPropertyFunction method evaluate.
/**
* Returns the string value of the property using arg one as key and arg two as scope
*
* @param scopeObject scope will decide from where property will be picked up from
* i.e. axis2, transport, default/synapse
* @param keyObject the key of the property
* @param navigator object model which can be used for navigation around
* @param dateformat The dateformat that need to convert
* @return The String value of property using arg one as key and arg two as scope
*/
public Object evaluate(Object scopeObject, Object keyObject, Object dateformat, Navigator navigator) {
boolean traceOn = synCtx.getTracingState() == SynapseConstants.TRACING_ON;
boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
String scope = StringFunction.evaluate(scopeObject, navigator);
String key = StringFunction.evaluate(keyObject, navigator);
if (key == null || "".equals(key)) {
if (traceOrDebugOn) {
traceOrDebug(traceOn, "property-name should be provided when executing synapse:get-property" + "(scope,prop-name) or synapse:get-property(prop-name) Xpath function");
}
return NULL_STRING;
}
if (SynapseConstants.SYSTEM_DATE.equals(key)) {
if (dateformat != null) {
Format formatter = new SimpleDateFormat(dateformat.toString());
return formatter.format(new java.util.Date());
} else {
Format formatter = new SimpleDateFormat();
return formatter.format(new java.util.Date());
}
}
// return the current system time as a string , don't care scope
if (SynapseConstants.SYSTEM_TIME.equals(key)) {
return Long.toString(System.currentTimeMillis());
}
if (XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
if (SynapseConstants.HEADER_TO.equals(key)) {
EndpointReference toEPR = synCtx.getTo();
if (toEPR != null) {
return toEPR.getAddress();
} else {
return NULL_STRING;
}
} else if (SynapseConstants.HEADER_FROM.equals(key)) {
EndpointReference fromEPR = synCtx.getFrom();
if (fromEPR != null) {
return fromEPR.getAddress();
} else {
return NULL_STRING;
}
} else if (SynapseConstants.HEADER_ACTION.equals(key)) {
String wsaAction = synCtx.getWSAAction();
if (wsaAction != null) {
return wsaAction;
} else {
return NULL_STRING;
}
} else if (SynapseConstants.HEADER_FAULT.equals(key)) {
EndpointReference faultEPR = synCtx.getFaultTo();
if (faultEPR != null) {
return faultEPR.getAddress();
} else {
return NULL_STRING;
}
} else if (SynapseConstants.HEADER_REPLY_TO.equals(key)) {
EndpointReference replyToEPR = synCtx.getReplyTo();
if (replyToEPR != null) {
return replyToEPR.getAddress();
} else {
return NULL_STRING;
}
} else if (SynapseConstants.HEADER_RELATES_TO.equals(key)) {
RelatesTo relatesTo = synCtx.getRelatesTo();
if (relatesTo != null) {
return relatesTo.getValue();
} else {
return NULL_STRING;
}
} else if (SynapseConstants.HEADER_MESSAGE_ID.equals(key)) {
String messageID = synCtx.getMessageID();
if (messageID != null) {
return messageID;
} else {
return NULL_STRING;
}
} else if (SynapseConstants.PROPERTY_FAULT.equals(key)) {
if (synCtx.getEnvelope().hasFault()) {
return SynapseConstants.TRUE;
} else if (synCtx instanceof Axis2MessageContext) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
if (axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE) != null && SynapseConstants.TRUE.equals(axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE))) {
return SynapseConstants.TRUE;
}
} else {
return NULL_STRING;
}
} else if (SynapseConstants.PROPERTY_MESSAGE_FORMAT.equals(key)) {
if (synCtx.isDoingPOX())
return SynapseConstants.FORMAT_POX;
else if (synCtx.isDoingGET())
return SynapseConstants.FORMAT_GET;
else if (synCtx.isSOAP11())
return SynapseConstants.FORMAT_SOAP11;
else
return SynapseConstants.FORMAT_SOAP12;
} else if (SynapseConstants.PROPERTY_OPERATION_NAME.equals(key) || SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
if (synCtx instanceof Axis2MessageContext) {
AxisOperation axisOperation = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getAxisOperation();
if (axisOperation != null) {
if (SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
return axisOperation.getName().getNamespaceURI();
} else {
return axisOperation.getName().getLocalPart();
}
}
}
} else {
Object result = synCtx.getProperty(key);
if (result != null) {
return result;
} else {
return synCtx.getLocalEntry(key);
}
}
} else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
return axis2MessageContext.getProperty(key);
} else if (XMLConfigConstants.SCOPE_FUNC.equals(scope)) {
Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
TemplateContext topCtxt = functionStack.peek();
if (topCtxt != null) {
return topCtxt.getParameterValue(key);
}
} else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
Object headers = axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
return headersMap.get(key);
}
} else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
return axis2smc.getAxis2MessageContext().getOperationContext().getProperty(key);
} else if (XMLConfigConstants.SCOPE_REGISTRY.equals(scope)) {
String[] regParam = key.split("@");
String regPath = null;
String propName = null;
if (regParam.length == 2) {
regPath = regParam[0];
propName = regParam[1];
} else if (regParam.length == 1) {
regPath = regParam[0];
}
Entry propEntry = synCtx.getConfiguration().getEntryDefinition(regPath);
if (propEntry == null) {
propEntry = new Entry();
propEntry.setType(Entry.REMOTE_ENTRY);
propEntry.setKey(key);
}
Registry registry = synCtx.getConfiguration().getRegistry();
if (registry != null) {
registry.getResource(propEntry, new Properties());
if (propName != null) {
Properties reqProperties = propEntry.getEntryProperties();
if (reqProperties != null) {
if (reqProperties.get(propName) != null) {
return reqProperties.getProperty(propName);
}
}
} else if (propEntry.getValue() != null) {
if (propEntry.getValue() instanceof OMText) {
return ((OMText) propEntry.getValue()).getText();
}
return propEntry.getValue().toString();
}
}
} else if (XMLConfigConstants.SCOPE_SYSTEM.equals(scope)) {
String propVal = System.getProperty(key);
if (propVal != null) {
return propVal;
} else {
if (traceOrDebugOn) {
traceOrDebug(traceOn, "System property " + key + " not found");
}
return NULL_STRING;
}
} else {
if (traceOrDebugOn) {
traceOrDebug(traceOn, "Invalid scope : '" + scope + "' has been set for the " + "synapse:get-property(scope,prop-name) XPath function");
}
}
return NULL_STRING;
}
use of org.apache.synapse.mediators.template.TemplateContext in project wso2-synapse by wso2.
the class SynapseXPathVariableContext method getVariableValue.
/**
* Gets the variable values resolved from the context. This includes the
* <dl>
* <dt><tt>body</tt></dt>
* <dd>The SOAP 1.1 or 1.2 body element.</dd>
* <dt><tt>header</tt></dt>
* <dd>The SOAP 1.1 or 1.2 header element.</dd>
* </dl>
* and the following variable prefixes
* <dl>
* <dt><tt>ctx</tt></dt>
* <dd>Prefix for Synapse MessageContext properties</dd>
* <dt><tt>axis2</tt></dt>
* <dd>Prefix for Axis2 MessageContext properties</dd>
* <dt><tt>trp</tt></dt>
* <dd>Prefix for the transport headers</dd>
* </dl>
* If the variable is unknown, this method attempts to resolve it using
* the parent variable context.
*
* @param namespaceURI namespaces for the variable resolution
* @param prefix string prefix for the variable resolution
* @param localName string local name for the variable resolution
* @return Resolved variable value
* @throws UnresolvableException if the variable specified does not found
*/
public Object getVariableValue(String namespaceURI, String prefix, String localName) throws UnresolvableException {
if (namespaceURI == null) {
if (env != null) {
if (SynapseXPathConstants.SOAP_BODY_VARIABLE.equals(localName)) {
return env.getBody();
} else if (SynapseXPathConstants.SOAP_HEADER_VARIABLE.equals(localName)) {
return env.getHeader();
} else if (SynapseXPathConstants.SOAP_ENVELOPE_VARIABLE.equals(localName)) {
return env;
}
}
if (prefix != null && !"".equals(prefix) && synCtx != null) {
if (SynapseXPathConstants.MESSAGE_CONTEXT_VARIABLE_PREFIX.equals(prefix)) {
return synCtx.getProperty(localName);
} else if (SynapseXPathConstants.AXIS2_CONTEXT_VARIABLE_PREFIX.equals(prefix)) {
return ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(localName);
} else if (SynapseXPathConstants.FUNC_CONTEXT_VARIABLE_PREFIX.equals(prefix)) {
Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
TemplateContext topCtxt = functionStack.peek();
if (topCtxt != null) {
Object result = topCtxt.getParameterValue(localName);
if (result != null && result instanceof SynapseXPath && env != null) {
SynapseXPath expression = (SynapseXPath) topCtxt.getParameterValue(localName);
try {
return expression.evaluate(env);
} catch (JaxenException e) {
return null;
}
} else {
return result;
}
}
} else if (SynapseXPathConstants.TRANSPORT_VARIABLE_PREFIX.equals(prefix)) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
Object headers = axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
return headersMap.get(localName);
} else {
return null;
}
} else if (SynapseXPathConstants.URL_VARIABLE_PREFIX.equals(prefix)) {
EndpointReference toEPR = synCtx.getTo();
if (toEPR != null) {
String completeURL = toEPR.getAddress();
AxisBindingOperation axisBindingOperation = (AxisBindingOperation) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(Constants.AXIS_BINDING_OPERATION);
String queryParameterSeparator = null;
if (axisBindingOperation != null) {
queryParameterSeparator = (String) axisBindingOperation.getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
}
if (queryParameterSeparator == null) {
queryParameterSeparator = WSDL20DefaultValueHolder.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR_DEFAULT;
}
int i = completeURL.indexOf("?");
if (i > -1) {
String queryString = completeURL.substring(i + 1);
if (queryString != null && !queryString.equals("")) {
String[] params = queryString.split(queryParameterSeparator);
if (params == null || params.length == 0) {
return "";
}
for (String param : params) {
String[] temp = param.split("=");
if (temp != null && temp.length >= 1) {
if (temp[0].equalsIgnoreCase(localName)) {
try {
return temp.length > 1 ? URIEncoderDecoder.decode(temp[1]) : "";
} catch (UnsupportedEncodingException e) {
String msg = "Couldn't decode the URL parameter " + "value " + temp[1] + " with name " + localName;
log.error(msg, e);
throw new UnresolvableException(msg + e.getMessage());
}
}
}
}
}
}
}
return "";
} else if (SynapseXPathConstants.OPERATION_SCOPE_VARIABLE_PREFIX.equals(prefix)) {
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
return axis2smc.getAxis2MessageContext().getOperationContext().getProperty(localName);
} else if (SynapseXPathConstants.SYSTEM_SCOPE_VARIABLE_PREFIX.equals(prefix)) {
String propVal = System.getProperty(localName);
if (propVal != null) {
return propVal;
} else {
return "";
}
} else {
Object o = synCtx.getProperty(prefix);
if (o instanceof Map) {
Object valueObject = ((Map) o).get(localName);
if (valueObject != null) {
return valueObject.toString();
}
}
}
}
}
// try resolving using available custom extensions
Object obj = XpathExtensionUtil.resolveVariableContext(synCtx, namespaceURI, prefix, localName);
if (obj != null) {
return obj;
}
return parent.getVariableValue(namespaceURI, prefix, localName);
}
Aggregations