Skip to Content

Intro: Once you are working on more complex logic to be implemented in CPI, using header and property value is very common practice. Accessing these values in groovy script is known to every one. This blog will go through different ways to access Header and properties values in following areas:

 

  • Xpath
  • Message Mapping
  • XSLT mapping

This will simpify the logic to be implemented.

 

  1. XPath:

 

Accessing header and property value in Xpath is pretty simple. Just use ${{headerName}} or ${{propertyName}} with the Xpath.

 

Consider below example:

 

XML :

Heaver Value

In order to collect all selling code having code type greater than 50, we can write the following XPATH:

/SELLING_CODE/PRODUCT/CODE[CODE_TYPE > $CodeType]

 

Same way property can be accessed in Xpath, if in case both header and property have same variable name then header will take preference.

 

  1. Message Mapping

 

To access header and property value in Message Mapping flow, you need to create two different script with extension *.gsh and place it in script folder of project.

 

getProperty.gsh

 

import com.sap.it.api.mapping.*;

import com.sap.it.api.mapping.MappingContext;

def String getProperty(String property_name, MappingContext context) {

    def propValue= context.getPropery(property_name);

    return propValue;

}

 

getHeader.gsh

 

import com.sap.it.api.mapping.*;

import com.sap.it.api.mapping.MappingContext;

def String getheader(String header_name, MappingContext context) {

    def headervalue= context.getHeader(header_name);

    return headervalue;

}

 

Add these scripts as custom function in Message Mapping.

 

Once these script are added as custom function, they can be used within mapping as shown below. Add the variable name of header or property you want to retrieve in the constant function.

 

 

 

  1. XSLT mapping

 

All parameters defined in the XLST mapping are automatically bound to Camel headers. In case a property or header with that name already exists, its value gets auto assigned to the parameter.

 

 

First the namespace where the extension functions (setheader, setProperty) is registered needs to be defined.

 

<xsl:stylesheet version=”2.0″
xmlns:xsl=http://www.w3.org/1999/XSL/Transform
xmlns:hci=http://sap.com/it/>

 

The exchange must be included in the XSLT as a parameter.

 

<xsl:param name=”exchange”/>

 

Setting a header or property:

When this is done, the extension functions can be called from within the XSLT sheet. The provided functions have three parameters (the first parameter must be the exchange, the other two are header name and header value or exchange property name and value (all strings)).

 

Here is an example:

<xsl:template match=”/”>

<xsl:value-of select=”hci:setHeader($exchange, ‘myname’, ‘myvalue’)”/>

<xsl:value-of select=”hci:setProperty($exchange, ‘myname’, ‘myvalue’)”/>

</xsl:template>

 

Getting the value of a header or property:

In order to get the value of a header/property that already exists, simply define a parameter with the same name.

 

Example: assume that a header/property with the name a1 already exists and has a value 123, simple define a parameter with the same name:

 

<xsl:param name= “a1”>

 

Now 123 is automatically assigned as a value to a1.

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Fasiullah S

    Thanks Shashank. very helpful.

    One correction  – In getPropert.gsh, “def propValue= context.getPropery(property_name);” getProperty is misspelled as getPropery (“t” missing).

    (0) 

Leave a Reply