RFH2 Header transform with Datapower

Below is the RFH2 Transform xsl to convert a message to Websphere MQ format and append userid, ipaddress, filename and filepath for dynamic routing of messages.

<?xml version=”1.0″ encoding=”UTF-8″?>

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221; version=”1.0″
extension-element-prefixes=”dp”
exclude-result-prefixes=”dp func ag”>
<xsl:template match=”/”>
<xsl:variable name=”userid” select=”dp:auth-info(‘basic-auth-name’)”/>
<xsl:variable name=”ipaddress” select=”dp:request-header(‘X-Client-IP’)”/>
<xsl:variable name=”filename” select=”ag:substring-after(dp:variable(‘var://service/URI’),’/’)”/>
<xsl:variable name=”filepath” select=”substring(dp:variable(‘var://service/URI’),1,string-length(dp:variable(‘var://service/URI’)) – string-length($filename))”/>
<xsl:variable name=”MQMD”>
<MQMD>
<Format>MQHRF2</Format>
<Persistence type=”MQLONG”>0</Persistence>
</MQMD>
</xsl:variable>
<xsl:variable name=”RFH2″>
<MQRFH2>
<StrucId>RFH</StrucId>
<Version>2</Version>
<Format>MQSTR</Format>
<NameValueData>
<NameValue>
<usr>
<userid><xsl:value-of select=”$userid”/></userid>
<filePath><xsl:value-of select=”$filepath”/></filePath>
<fileName><xsl:value-of select=”$filename”/></fileName>
<ipaddres><xsl:value-of select=”$ipaddress”/></ipaddres>
</usr>
</NameValue>
</NameValueData>
</MQRFH2>
</xsl:variable>
<xsl:variable name=”mqmdStr”>
<dp:serialize select=”$MQMD” omit-xml-decl=”yes”/>
</xsl:variable>
<xsl:variable name=”rfh2Str”>
<dp:serialize select=”$RFH2″ omit-xml-decl=”yes”/>
</xsl:variable>
<dp:set-request-header name=”‘MQMD'” value=”$mqmdStr”/>
<dp:set-request-header name=”‘MQRFH2′” value=”$rfh2Str”/>
</xsl:template>
<func:function name=”ag:substring-after”>
<xsl:param name=”string”/>
<xsl:param name=”delimiter”/>
<xsl:choose>
<xsl:when test=”contains($string,$delimiter)”>
<func:result select=”ag:substring-after(substring-after($string,$delimiter),$delimiter)”/>
</xsl:when>
<xsl:otherwise>
<func:result select=”$string”/>
</xsl:otherwise>
</xsl:choose>
</func:function>
</xsl:stylesheet>

<?xml version=”1.0″ encoding=”UTF-8″?><xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221; version=”1.0″    xmlns:dp=”http://www.datapower.com/extensions”    xmlns:func=”http://exslt.org/functions”    xmlns:ag=”http://demo.ibm.com/functions”    extension-element-prefixes=”dp”    exclude-result-prefixes=”dp func ag”>        <xsl:template match=”/”>                <xsl:variable name=”userid” select=”dp:auth-info(‘basic-auth-name’)”/>        <xsl:variable name=”ipaddress” select=”dp:request-header(‘X-Client-IP’)”/>        <xsl:variable name=”filename” select=”ag:substring-after(dp:variable(‘var://service/URI’),’/’)”/>        <xsl:variable name=”filepath” select=”substring(dp:variable(‘var://service/URI’),1,string-length(dp:variable(‘var://service/URI’)) – string-length($filename))”/>                <xsl:variable name=”MQMD”>            <MQMD>                <Format>MQHRF2</Format>                <Persistence type=”MQLONG”>0</Persistence>            </MQMD>        </xsl:variable>                        <xsl:variable name=”RFH2″>            <MQRFH2>                <StrucId>RFH</StrucId>                <Version>2</Version>                <Format>MQSTR</Format>                <NameValueData>                    <NameValue>                        <usr>                            <userid><xsl:value-of select=”$userid”/></userid>                            <filePath><xsl:value-of select=”$filepath”/></filePath>                            <fileName><xsl:value-of select=”$filename”/></fileName>                            <ipaddres><xsl:value-of select=”$ipaddress”/></ipaddres>                                                    </usr>                    </NameValue>                </NameValueData>            </MQRFH2>        </xsl:variable>                <xsl:variable name=”mqmdStr”>            <dp:serialize select=”$MQMD” omit-xml-decl=”yes”/>        </xsl:variable>                <xsl:variable name=”rfh2Str”>            <dp:serialize select=”$RFH2″ omit-xml-decl=”yes”/>        </xsl:variable>                <dp:set-request-header name=”‘MQMD'” value=”$mqmdStr”/>        <dp:set-request-header name=”‘MQRFH2′” value=”$rfh2Str”/>            </xsl:template>        <func:function name=”ag:substring-after”>        <xsl:param name=”string”/>        <xsl:param name=”delimiter”/>        <xsl:choose>            <xsl:when test=”contains($string,$delimiter)”>                <func:result select=”ag:substring-after(substring-after($string,$delimiter),$delimiter)”/>            </xsl:when>            <xsl:otherwise>                <func:result select=”$string”/>            </xsl:otherwise>        </xsl:choose>    </func:function>    </xsl:stylesheet>

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s