Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

WSO2 ESB supports standard XPath functions and variables through its underlying XPath engine. The ESB also provides custom XPath functions and variables for accessing message properties.

Table of Contents
maxLevel3
minLevel3

XPath extension functions

There are two custom functions supported by the ESB as follows:

  1. synapse:get-property() function
  2. base64Encode() function

...

The get-property() function allows any XPath expression used in a configuration to look up information from the current message context. Using property mediator, you can retrieve properties previously set, and/or information from the Synapse, Axis2 message contexts or transport 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 synapse, axis2 or transport. If you provide only the property name without the scope, the default Synapse scope will be used.

synapse:get-property( [(axis2 | axis2-client | transport),] <property_name> [,<dateformat>] )

You can use the get-property() function to retrieve Axis2 message context properties or transport headers. For example, synapse:get-property('transport', 'USER_AGENT'). Given below are special properties supported by the get-property() function.

NameReturn Value
ToIncoming URL as a String or empty string («») if a To address is not defined.
FromFrom address as a String or empty string («») if a From address is not defined
ActionSOAP Addressing Action header value as a String or empty string («») if a Action is not defined
FaultToSOAP FautTo header value as a String or empty string («») if a FaultTo address is not defined
ReplyToReplyTo header value as a String or empty string («») if a ReplyTo address is not defined
MessageIDA unique identifier (UUID) for the message as a String or empty string («») if MessageID is not defined. This id is guaranteed to be unique.
FAULTTRUE if the message has a fault or empty string if message doesn't have a fault
MESSAGE_FORMATReturns pox, get, soap11, soap12 depending on the message. If a message type is unknown this returns soap12
OperationNameOperation 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

base64Encode() function

The syntax of the function takes the following format.

  • base64Encode(string)

It returns the base64 encoded value of the string argument.

Synapse XPath variables

There is a set of predefined XPath variables that you can directly use to write XPaths in the Synapse configuration, instead of using the synapse:get-property() function. These XPath variables get properties of various scopes as follows:

body

The SOAP 1.1 or 1.2 body element. For example, expression="$body/getQuote" refers to the first getQuote element in the SOAP body regardless of whether the message is SOAP-11 or SOAP-12.

Header

The SOAP 1.1 or 1.2 header element. For example, expression="$header/wsa:To refers to the addressing To header regardless of whether this message is SOAP-11 or SOAP-12.

$axis2

Prefix for Axis2 MessageContext properties. This is used to get the property value at the axis2 scope. For example, to get the value of Axis2 message context property with name REST_URL_POSTFIX, use the XPath expression $axis2:REST_URL_POSTFIX. We have discussed an example below.

Example of $axis2 usage:

1. Deploy the following proxy service using instructions in Adding a Proxy Service.

Note the property, <property name="stockprop" expression="$axis2:REST_URL_POSTFIX"/> in the configuration which is used to log the REST_URL_POSTFIX value of the request message.

Code Block
languagehtml/xml
<proxy xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <log>
            <property name="stockprop" expression="$axis2:REST_URL_POSTFIX"/>
         </log>
         <send>
            <endpoint>
              <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description></description>
</proxy>

2. Send the StockQuoteRequest using sample stockquote client as follows.

ant stockquote -Daddurl=http://localhost:8280/services/StockQuoteProxy/test/prefix 

3. Note the following message in the ESB log.

Code Block
INFO - LogMediator To: http://localhost:8280/services/StockQuoteProxy/test/prefix, WSAction: urn:getQuote, SOAPAction: urn:getQuote, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:ecd228c5-106a-4448-9c83-3b1e957e2fe5, Direction: request, stockprop = /test/prefix

In this example, the property definition, <property name="stockprop" expression="$axis2:REST_URL_POSTFIX"/> is equivalent to <property name="stockprop" expression="get-property('axis2','REST_URL_POSTFIX')"/>

Similarly, you can use $axis2 prefix with HTTP Transport Properties.

$ctx

Prefix for Synapse MessageContext properties. For example, to get the value of Synapse message context property with name ERROR_MESSAGE, use the XPath expression $ctx:ERROR_MESSAGE. We have discussed an example below.

Example of $ctx usage:

This example sends a request to a sample proxy service, and sets the target endpoint to a non-existent endpoint reference key. It causes a mediation fault, which triggers the fault sequence.

1. Deploy the following proxy service using instructions in Adding a Proxy Service.

Note the property, <property name="stockerrorprop" expression="$ctx:ERROR_MESSAGE"/> in the fault sequence configuration. It is used to log the error message that occurs due to a  mediation fault.

Code Block
languagehtml/xml
<proxy xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
        <send>
            <endpoint key="ep2"/>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
      <faultSequence>
         <log>
            <property name="stockerrorprop" expression="$ctx:ERROR_MESSAGE"/>
            <property name="Cause" expression="get-property('ERROR_MESSAGE')"/>
         </log>
      </faultSequence>
   </target>
   <description></description>
</proxy> 

2. Send a StockQuote request using the sample StockQuote client as follows.

ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuoteProxy

3. Note the following message in the ESB log.

Code Block
INFO - LogMediator To: /services/StockQuoteProxy, WSAction: urn:getQuote, SOAPAction: urn:getQuote, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:54205f7d-359b-4e82-9099-0f8e3bf9d014, Direction: request, stockerrorprop = Couldn't find the endpoint with the key : ep2 

...

$trp

Prefix used to get the transport headers. For example, to get the transport header named Content-Type of the current message, use the XPath expression $trp:Content-Type. HTTP transport headers are not case sensitive. Therefore, $trp:Content-Type and $trp:CONTENT-TYPE are regarded as the same. We have discussed an example below.

...

Note the property, <property name="stockprop" expression="$trp:Content-Type"/> in the configuration, which is used to log the Content-Type HTTP header of the request message.

Code Block
languagehtml/xml
<proxy xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <log>
             <property name="stockprop" expression="$trp:Content-Type"/>
         </log>
         <send>
            <endpoint>
               <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description></description>
</proxy> 

2. Send the following StockQuote request using the sample StockQuote client. For information on the sample client, refer to the Sample Clients sub heading in ESB Samples Setup.

ant stockquote -Daddurl=http://localhost:8280/services/StockQuoteProxy

3. Note the following message in the ESB log.

Code Block
[2013-03-18 12:23:14,101] INFO - LogMediator To: http://localhost:8280/services/StockQuoteProxy, WSAction: urn:getQuote, SOAPAction: urn:getQuote, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:25a3143a-5b18-4cbb-b8e4-27d4dd1895d2, Direction: request, stockprop = text/xml; charset=UTF-8 

In this example, the property definition, <property name="stockprop" expression="$trp:Content-Type"/> is equivalent to <property name="stockprop" expression="get-property('transport','Content-Type')"/>. Similarly, you can use $trp prefix with HTTP Transport Properties.

$ctx

Gets a property at the default scope.

For example, to get the property named 'foo' at the default scope use the following XPath expression:

$ctx:foo

$url

Gets a URL parameter.

For example, to get the URL parameter named 'bar' use the following XPath expression:

$url:foo

 

 

 

 

 

 

 

 

 

Syntax

...

Child pages (Children Display)