Header Mediator
The Header Mediator allows you to manipulate SOAP and HTTP headers.
The Header mediator is a conditionally content aware mediator.
Syntax
<header name=”string” (value=”string|{property}” | expression=”xpath”) [scope=default|transport] [action=set|remove]/>
The optional action
attribute specifies whether the mediator should set or remove the header. If no value is specified, the header is set by default.
Configuration
The parameters available to configure the Header mediator are as follows.
Paramater Name | Description |
---|---|
Name | The name of the header element. You can specify the namespace used in the header element by clicking the Namespaces link next to the text field. |
Action | Select Set if you want to set the header as a new header. Select Remove if you want to remove the header from the incoming message. |
Value/Expression | A static value or an XPath expression that will be executed on the message to set the header value. |
Inline XML Header | This parameter allows you to directly input any XML syntax related to the Header mediator (specifically for SOAP headers). For example, to achieve the following configuration, you should enter the <header> <urn:lastTradeTimestamp xmlns:urn=" http://synapse.apache.org/ ">Mon May 13 13:52:17 IST 2013</urn:lastTradeTimestamp> </header> |
Scope | Select Synapse if you want to manipulate SOAP headers. Select Transport if you want to manipulate HTTP headers. |
Namespaces | You can click this link to add namespaces if you are providing an expression. The Namespace Editor panel would appear. You can enter any number of namespace prefixes and URL that you have used in the XPath expression in this panel. |
Examples
This section covers the following scenarios in which the Header mediator can be used.
Example 1 - SOAP headers
In the following example, the value for P1 code
should be included in the SOAP header of the message sent from the client to the ESB profile. To do this, the header mediator is added to the in sequence of the proxy configuration as shown below.
To get a response with Hello World
in the SOAP header, the header mediator is also added to the out sequence.
<inSequence> <header> <p1:Code xmlns:p1="http://www.XYZ.com/XSD">XYZ</p1:Code> </header> <send> <endpoint> <address uri="http://localhost:8899/services/SimpleStockQuoteService?wsdl"/> </endpoint> </send> </inSequence> <outSequence> <header> <p2:Header xmlns:p2="http://www.ABC.com/XSD"> <p2:Hello>World</p2:Hello> </p2:Header> </header> <send/> </outSequence>
Example 2 - HTTP headers
The following example makes the ESB profile add the HTTP header Accept
with the value image/jpeg
to the HTTP request made to the endpoint.
<inSequence> <header name="Accept" value="image/jpeg" scope="transport"/> <send> <endpoint name="people"> <address uri="http://localhost:9763/people/eric+cooke" format="get"/> </endpoint> </send> </inSequence> <outSequence> <send/> </outSequence>
If you have enabled wire logs, you will view the following output.
<< GET /people/eric+cooke HTTP/1.1 << Accept: image/jpeg << Host: localhost:9763 << Connection: Keep-Alive
Example 3 - Handling headers with complex XML
A header can contain XML structured values by embedding XML content within the <header>
element as shown below.
<header> <m:complexHeader xmlns:m="http://org.synapse.example"> <property key="k1" value="v1" /> <property key="k2" value="v2" /> </m:complexHeader> </header>
Example 4 - Adding a dynamic SOAP header
The following configuration takes the value of an element named symbol
in the message body (the namespace
http://services.samples/xsd
), and adds it as a SOAP header named header1
.
<header xmlns:m="http://org.synapse.example" xmlns:sym="http://services.samples/xsd" name="m:header1" scope="default" expression="//sym:symbol"/>
Example 5 - Setting the endpoint URL dynamically
In this example, the Header mediator allows the endpoint URL to which the message is sent to be set dynamically. It specifies the default address to which the message is sent dynamically by deriving the To header of the message via an XPath expression. Then the Send mediator sends the message to a Default Endpoint . A Default Endpoint sends the message to the default address of the message (i.e. address specified in the To header). Therefore, in this scenario, selecting the Default Endpoint results in the message being sent to relevant URL calculated via the fn:concat('http://localhost:9764/services/Axis2SampleService_',get-property('epr'))
expression.
<header name="To" expression="fn:concat('http://localhost:9764/services/Axis2SampleService_',get-property('epr'))"/> <send> <endpoint> <default/> </endpoint> </send>