<!--
     zip-copy.xsl copies all files from a given .zip archive to "local:///".

     Directory structure is preserved, and even empty directories in
     .zip archive will be created under "local:///".

     Deletion of directories under "local:///" before copying files can be
     requested; this will remove all files there before copying in new files.

     zip-copy.xsl is to be used in a "Transform Binary" action, and can be
     used with coproc2nonxml endpoint.

     Format (with coproc2):
     coproc2 zip-copy.xsl archive.zip http://dp6-l3:2224/domain[?delete=y[es]]


     SSL Proxy Profile "xmlmgmt-client-ssl" is needed, see:
https://www.ibm.com/developerworks/mydeveloperworks/blogs/HermannSW/entry/accessing_xml_management_interface_from_within_a_stylesheet38

     This stylesheet is based on "unzip-l.xsl" posting:
https://www.ibm.com/developerworks/mydeveloperworks/blogs/HermannSW/entry/unzip_l_xsl_xslt_implementation_of_unzip_l_command8

     This stylesheet is attached to thread "uploading the directory on XI50":
     https://www.ibm.com/developerworks/forums/thread.jspa?threadID=432753

     This is the associated Blog posting:    
https://www.ibm.com/developerworks/mydeveloperworks/blogs/HermannSW/entry/zip_copy_xsl
-->
<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"
>
  <!--  ==>  <object><message>***binary data***</message></object>  -->
  <dp:input-mapping href="store:///pkcs7-convert-input.ffd" type="ffd"/>
  <xsl:output omit-xml-declaration="yes"/>

  <!-- "xmlmgmt" user is used to access XML management interface -->
  <xsl:variable name="pwd64" select="dp:encode('xmlmgmt:terces','base-64')"/>
  <xsl:variable name="basicAuth" select="concat('Basic ',$pwd64)"/>

  <!-- these HTTP header are used for accessing XML management interface -->
  <xsl:variable name="httpHeaders">
    <header name="Content-Type">application/soap+xml</header>
    <header name="Authorization"><xsl:value-of select="$basicAuth"/></header>
  </xsl:variable>

  <!-- determine domain and delete setting from URI -->
  <xsl:variable name="uri" select="dp:variable('var://service/URI')"/>
  <xsl:variable name="domain" 
    select="substring(substring-before(concat($uri,'?'),'?'),2)"/>
  <xsl:variable name="delete" 
    select="starts-with(substring-after($uri,'?'),'delete=y')"/>


  <xsl:template match="/">

    <!-- create context "swa" with "any content" (not used) -->
    <dp:set-variable name="'var://context/swa'" value="''" />

    <!-- attach zip-archive to context "swa" ("myZip" Content-ID) -->
    <dp:url-open 
      target="attachment://swa/cid:myZip" 
      data-type="base64" 
      response="ignore"
      content-type="application/zip"
    >
      <xsl:value-of select="dp:binary-encode(/object/message/node())" />
    </dp:url-open>


    <!-- get directories and files -->
    <xsl:variable name="filename"
      select="dp:variable('var://context/swa/attachment-manifest')/
              manifest/attachments/attachment/files/file/filename"/>


    <!-- iterate over directories first -->
    <xsl:for-each select="$filename[contains(concat(.,'/'),'//')]">

      <!-- RemoveDir if requested, deletes all files (and sub-directories) -->
      <xsl:if test="$delete">
        <xsl:variable name="result">
          <dp:url-open 
             target="https://127.0.0.1:5550/service/mgmt/current"
             http-headers="$httpHeaders/*"
             ssl-proxy="xmlmgmt-client-ssl"
          >
            <env:Envelope 
              xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
            >
              <env:Body>
                <dp:request 
                  xmlns:dp="http://www.datapower.com/schemas/management"
                  domain="{$domain}"
                >
                  <dp:do-action>
                    <RemoveDir>
                      <Dir>
                        <xsl:value-of select="concat('local:///', .)"/>
                      </Dir>
                    </RemoveDir>
                  </dp:do-action>
                </dp:request>
              </env:Body>
            </env:Envelope>
          </dp:url-open>
        </xsl:variable>
      </xsl:if>

      <!-- CreateDir -->
      <xsl:variable name="result">
        <dp:url-open 
           target="https://127.0.0.1:5550/service/mgmt/current"
           http-headers="$httpHeaders/*"
           ssl-proxy="xmlmgmt-client-ssl"
        >
          <env:Envelope 
            xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
          >
            <env:Body>
              <dp:request 
                xmlns:dp="http://www.datapower.com/schemas/management"
                domain="{$domain}"
              >
                <dp:do-action>
                  <CreateDir>
                    <Dir>
                      <xsl:value-of select="concat('local:///', .)"/>
                    </Dir>
                  </CreateDir>
                </dp:do-action>
              </dp:request>
            </env:Body>
          </env:Envelope>
        </dp:url-open>
      </xsl:variable>
    </xsl:for-each>

    <!-- now handle non-directory files -->
    <xsl:for-each select="$filename[not(contains(concat(.,'/'),'//'))]">

      <!-- extract file from .zip archive -->
      <xsl:variable name="file">
        <dp:url-open 
          target="{concat(
                     'attachment://swa/cid:myZip?Archive=zip&amp;Filename=',
                     .
                  )}" 
          response="binaryNode"
        />
      </xsl:variable>

      <!-- base64 encode (binary) file content -->
      <xsl:variable name="file64" 
        select="dp:binary-encode($file/result/binary/node())"/>

      <!-- set-file stores extracted .zip archive file -->
      <xsl:variable name="result">
        <dp:url-open 
           target="https://127.0.0.1:5550/service/mgmt/current"
           http-headers="$httpHeaders/*"
           ssl-proxy="xmlmgmt-client-ssl"
        >
          <env:Envelope 
            xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
          >
            <env:Body>
              <dp:request 
                xmlns:dp="http://www.datapower.com/schemas/management"
                domain="{$domain}"
              >
                <dp:set-file name="{concat('local:///', .)}">
                  <xsl:value-of select="$file64"/>
                </dp:set-file>
              </dp:request>
            </env:Body>
          </env:Envelope>
        </dp:url-open>
      </xsl:variable>
    </xsl:for-each>

    <done/>
  </xsl:template>

</xsl:stylesheet>