Sample redirect stylesheet

You can use this sample stylesheet to handle redirection and content-type rewriting. You refer to the stylesheet when you create rules to define an HTTP basic authentication policy or an HTML forms-based authentication policy.

You provide a custom stylesheet when you define rule mpgw-form_rule_6 (see Rules for HTML forms-based authentication, Table 6), and when you define rule worklight-basicauth_rule_2 (see Rules for HTTP basic authentication, Table 4).

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dp="http://www.datapower.com/extensions"
    xmlns:re="http://exslt.org/regular-expressions"
    extension-element-prefixes="dp re"
    exclude-result-prefixes="dp">

    <xsl:template match="/">
      <xsl:choose>
        <xsl:when test="dp:responding()">
          <xsl:variable name="code">
            <xsl:choose>
              <xsl:when test="dp:http-response-header('x-dp-response-code') != ''">
                <xsl:value-of select="substring(dp:http-response-header('x-dp-response-code'), 1, 3)"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="substring(dp:variable('var://service/error-headers'), 10, 3)" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:choose>
            <xsl:when test="$code = '302'">
              <xsl:variable name="dphost" select="dp:http-request-header('Host')"/>
              <xsl:variable name="host" select="$dphost"/>
              <xsl:variable name="location" select="dp:http-response-header('Location')"/>
              <xsl:variable name="location_host">
                <xsl:for-each select="re:match($location, '(\w+):\/\/([^/]+)')">
                  <xsl:if test="position()=3">
                    <xsl:value-of select="." />
                  </xsl:if>
                </xsl:for-each>
              </xsl:variable>
              <xsl:variable name="location_final">
                <xsl:value-of select="re:replace($location, $location_host, 'g', $host)" />
              </xsl:variable>
              <dp:set-http-response-header name="'Location'" value="$location_final" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:variable name="orig-content" select="dp:variable('var://service/original-response-content-type')"/>
              <xsl:if test="$orig-content != ''">
                <dp:set-http-response-header name="'Content-Type'" value='$orig-content'/>
              </xsl:if>
            </xsl:otherwise>
          </xsl:choose>

          <!-- the following prevent DataPower from overriding the
               response code coming back from WorkLight Server
          -->
          <dp:set-response-header name="'x-dp-response-code'" value="'-1'"/>

        </xsl:when>
        <xsl:otherwise/>
      </xsl:choose>
    </xsl:template>
</xsl:stylesheet>