user8316430
user8316430

Reputation: 11

Content-Disposition details extract from IBM Datapower gateway

I would like to extract the filename from Content-Disposition which comes in AS2 header and assign it to variable in IBM Datapower gateway. Do we have dp: service variable to extract it.

Upvotes: 0

Views: 1061

Answers (2)

Anders
Anders

Reputation: 3412

AugustZ's answer is good for a HTTP header, BUT when it comes to the Content-Disposition (filename) for AS2 you first have to enable "Preserve Filename" in the AS2 Partner settings in DataPower, otherwise DataPower strips the information.

Content-Disposition filename for AS2 is not a HTTP header, but a MIME-header and that is why it has to be moved into a HTTP header to be able to get it.

The filename will also be quoted so the following XSLT will deal with that:

<!-- use xpath to get the value of Content-Disposition header. For example,
         Content-Disposition: attachment; filename="fname.txt"
         Content-Disposition: attachment; filename="fname.txt"; otherparam="xyz" 
         Content-Disposition: attachment; filename=fname.txt -->
<xsl:variable name="cdisp" select="dp:request-header('Content-Disposition')"/>

<!-- Match the value of filename, quoted or non-quoted. 
     For example, "fname.txt" or fname.txt -->
<xsl:variable name="quoted-filename" select="regexp:match($cdisp, 'filename=(.+?)\s*(;|$)', 'i')[2]"/>

<!-- strip all double quotes if the filename is double-quoted. $filename is the resulting filename -->
<xsl:variable name="filename" select="translate($quoted-filename, '&quot;', '')"/>

Upvotes: 0

AugustZ
AugustZ

Reputation: 26

I don't think there is a specific variable at DataPower for exactly just the filename of Content-Disposition header of a AS2 message. However, Content-Dispositon header should be part of the HTTP headers set you can query on request or response through the header-manifest variable. Once you have that just query the value of the relevant header, in your case 'Content-Disposition'. You can do this with XSLT or GatewayScript. As a overloaded sample, not exactly reduced to your usecase, the below XSLT goes through the manifest and then queries the value for each header name retrieved from the header-manifest by using dp:http-request-header(...) to obtain the individual values. Once you have the Content-Disposition value, you just need to substring the "filename=...;" portion. I hope this is interesting enough for learning purposes and to catch the idea?

<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:dp="http://www.datapower.com/extensions" 
     extension-element-prefixes="dp"> 
<xsl:output omit-xml-declaration="yes" /> 

<xsl:template match="/"> 
    <xsl:apply-templates select="dp:variable('var://service/header-manifest')/headers/header" /> 
</xsl:template> 

<xsl:template match="header"/> 

<xsl:template match="header[. != 'xsl']"> 
    <property> 
        <xsl:attribute name="name"><xsl:value-of select="."/></xsl:attribute> 
        <xsl:value-of select="dp:http-request-header(.)"/> 
    </property><xsl:text>&#10;</xsl:text> 

    <xsl:variable name="input" select="dp:http-request-header(.)"/> 
    <xsl:message dp:priority="warning"><xsl:copy-of select="." />: <xsl:copy-of select="$input" /></xsl:message> 
</xsl:template> 
</xsl:stylesheet>

Upvotes: 0

Related Questions