Accessing Properties with XPath
The ESB profile of WSO2 Enterprise Integrator(WSO2 EI) supports standard XPath functions and variables through its underlying XPath engine. The ESB profile also provides custom XPath functions and variables for accessing message properties.
- 1 XPath Extension Functions
- 1.1 base64Encode() function
- 1.2 base64Decode() function
- 1.3 get-property() function
- 1.3.1 Synapse scope
- 1.3.2 axis2 scope
- 1.3.3 axis2-client
- 1.3.4 transport scope
- 1.3.5 registry scope
- 1.3.6 system scope
- 1.3.7 operation scope
- 1.4 url-encode() function
- 2 Synapse XPath Variables
XPath Extension Functions
In addition to standard XPath functions, the ESB profile of WSO2 Enterprise Integrator supports the following custom functions for working with XPath expressions:
base64Encode() function
The base64Encode function returns the base64-encoded value of the specified string.
Syntax:
base64Encode(string value)base64Encode(string value, string charset)
base64Decode() function
The base64Decode function returns the original value of the specified base64-encoded value.
Syntax:
base64Decode(string encodedValue)base64Decode(string encodedValue, string charset)
get-property() function
The
get-property()
function allows any XPath expression used in a configuration to look up information from the current message context. Using the Property mediator, you can retrieve properties from the message context and header.
The syntax of the function takes the following format.
get-property(String propertyName)get-property(String scope, String propertyName)
The function accepts scope as an optional parameter. It retrieves a message property at the given scope, which can be one of the following.
If you provide only the property name without the scope, the default synapse scope will be used.
When the result of an XPath evaluation results in a single XML node, the evaluator will return the text content of this node by default (equivalent of doing /root/body/node/text()). If you want to retrieve the node itself, you can configure the Enrich mediator as shown in the following example.
<inSequence>
<log level="custom">
<property name="WHERE" value="before doing stuff"/>
</log>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="ENRICH_PROPERTY"/>
</enrich>
<property name="PROPERTY_PROPERTY"
expression="$body/child::node()"
scope="default"/>
<log level="custom">
<property name="WHERE" value="before doing stuff"/>
<property name="ENRICH_PROPERTY" expression="get-property('ENRICH_PROPERTY')"/>
<property name="PROPERTY_PROPERTY" expression="get-property('PROPERTY_PROPERTY')"/>
</log>
<enrich>
<source type="property" clone="true" property="ENRICH_PROPERTY"/>
<target type="body" action="sibling"/>
</enrich>
<log level="full"/>
</inSequence>Synapse scope
When the scope of a property mediator is synapse, its value is available throughout both the in sequence and the out sequence. In addition to the user-defined properties, you can retrieve the following special properties from the synapse scope.
Name | Return Value |
To | Incoming URL as a String, or empty string («») if a To address is not defined. |
From | From address as a String, or empty string («») if a From address is not defined. |
Action | SOAP Addressing Action header value as a String, or empty string («») if an Action is not defined. |
FaultTo | SOAP FaultTo header value as a String, or empty string («») if a FaultTo address is not defined. |
ReplyTo | ReplyTo header value as a String, or empty string («») if a ReplyTo address is not defined. |
MessageID | A unique identifier (UUID) for the message as a String, or empty string («») if a MessageID is not defined. This ID is guaranteed to be unique. |
FAULT | TRUE if the message has a fault, or empty string if the message does not have a fault. |
MESSAGE_FORMAT | Returns pox, get, soap11, or soap12 depending on the message. If a message type is unknown this returns soap12 |
OperationName | Operation name corresponding to the message. A proxy service with a WSDL can have different operations. If the WSDL is not defined, ESB defines fixed operations. |
To access a property with the synapses cope inside the mediate() method of a mediator, you can include the following configuration in a custom mediator created using the Class mediator:
public boolean mediate(org.apache.synapse.MessageContext mc) {
// Available in both in-sequence and out-sequenc
String propValue = (String) mc.getProperty("PropName");
System.out.println("SCOPE_SYNAPSE : " + propValue);
return true;
}axis2 scope
When the scope of a property mediator is axis2, its value is available only throughout the sequence for which the property is defined (e.g., if you add the property to an in sequence, its value will be available only throughout the in sequence). You can retrieve message context properties within the axis2 scope using the following syntax.
Syntax:
get-property('axis2', String propertyName)
To access a property with the axis2 scope inside the mediate() method of a mediator, you can include the following configuration in a custom mediator created using the Class mediator:
public boolean mediate(org.apache.synapse.MessageContext mc) {
org.apache.axis2.context.MessageContext axis2MsgContext;
axis2MsgContext = ((Axis2MessageContext) mc).getAxis2MessageContext();
// Available only in the sequence the property is defined.
String propValue = (String) axis2MsgContext.getProperty("PropName");
System.out.println("SCOPE_AXIS2 : " + propValue);
return true;
} axis2-client
This is similar to the synapse
scope. The difference is that it can be accessed inside the mediate() method of a mediator by including one of the following configurations in a custom mediator, created using the Class mediator :
public boolean mediate(org.apache.synapse.MessageContext mc) {
org.apache.axis2.context.MessageContext axis2MsgContext;
axis2MsgContext = ((Axis2MessageContext) mc).getAxis2MessageContext();
String propValue = (String) axis2MsgContext.getProperty("PropName");
System.out.println("SCOPE_AXIS2_CLIENT - 1 : " + propValue); or
propValue = (String) axis2MsgContext.getOptions().getProperty("PropName");
System.out.println("SCOPE_AXIS2_CLIENT - 2: " + propValue);
return true;
} transport scope
When the scope of a property mediator is transport, it will be added to the transport header of the outgoing message from the ESB profile. You can retrieve message context properties within the transport scope using the following syntax.
Syntax:
get-property('transport', String propertyName)