Layer7 API Management

  • 1.  How to sort response of "List via SFTP"

    Posted Aug 26, 2016 02:08 PM

    How to sort the xml response generated by "list via SFTP" assertion? I want to extract the filename of the most recent file available in the directory listing.



  • 2.  Re: How to sort response of "List via SFTP"
    Best Answer

    Broadcom Employee
    Posted Aug 26, 2016 06:22 PM

    Hussain,

     

    As the response in the list from SFTP is XML, an XSL transformation can be done against the output and return just the last file name. The following snippet of the XSL transformation assertion will take the response message, exclude the entries that are directories, sort, and then grab the last @name file attribute value.

     

    Sample response:

     

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <files>
        <file file="true" lastModified="1408144529000" name="tcpdump-4.0.0-3.20090921gitdf3cb4.1.el6.x86_64.rpm" permissions="644" size="345180"/>
        <file file="true" lastModified="1467145064000" name="all_ellga02-ssg840-1.zip" permissions="600" size="8546430"/>
        <file file="true" lastModified="1467143402000" name="all_ellga02-ssg820-1.zip" permissions="600" size="33441245"/>
    </files>

     

    XSL Assertion XML snippet:

    <?xml version="1.0" encoding="UTF-8"?>
    <wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
        <wsp:All wsp:Usage="Required">
            <L7p:XslTransformation>
                <L7p:Direction intValue="2"/>
                <L7p:ResourceInfo staticResourceInfo="included">
                    <L7p:Document stringValueReference="inline"><![CDATA[<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
     
        <xsl:template match="@*|*|processing-instruction()|comment()">
          <xsl:copy>
            <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/>
          </xsl:copy>
        </xsl:template>
     
        <xsl:template match="files">
          <xsl:apply-templates select="file">
                 <xsl:sort data-type="number" select="@lastModified"/>
           </xsl:apply-templates>
        </xsl:template>
       
        <xsl:template match="file">
        <xsl:if test="position() = last()">
          <xsl:value-of select="@name"/>
        </xsl:if>
        </xsl:template>
        
        <xsl:template match="file[@file='false'] "/>
     
     
    </xsl:stylesheet>]]></L7p:Document>
                </L7p:ResourceInfo>
                <L7p:Target target="RESPONSE"/>
                <L7p:TransformName stringValue=""/>
                <L7p:XsltVersion stringValue="1.0"/>
            </L7p:XslTransformation>
        </wsp:All>
    </wsp:Policy>

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 3.  Re: How to sort response of "List via SFTP"

    Posted Aug 29, 2016 12:18 AM

    Thank you very much Stephan, it worked.

     

    I have been able to identify the most recent file and read it. The file is in comma separated file.

     

    I will appreciate your help if you provide me pointers for hiw to convert the csv response to an xml output using a particular template on CA API gateway version 9.1.

     

    Thanks again

    Hussain



  • 4.  Re: How to sort response of "List via SFTP"

    Broadcom Employee
    Posted Aug 29, 2016 12:52 AM

    Hussain,

     

    You could use the split variable assertion to convert the response into a multi part context variable which then could be used in Run Assertion For Each Item assertion branch to build out a context variable. The Run Assertion will really only be required if there are multiple lines. If it is a fixed number of entries on one line then you can use the set context variable to build the template with the array values of the split variable.

     

    Additional understanding points

    Example in the FTP file: cat,tree,one,12345

    Split variable will create an array in a variable: Variable name: foo Array values foo[0]=cat / foo[1]=tree / foo[2]=one, ....

    In the set context variable create the template:

    <output>

    <animal>${foo[0]}</animal>

    <location>${foo[1]}</location>

    <numberFiremen>${foo[2]}</numberFiremen>

    </output>

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 5.  Re: How to sort response of "List via SFTP"

    Posted Aug 29, 2016 12:17 PM

    Thanks for quick response.

     

    Will try the Split Variable and Run Assertion.

     

    Do you know why would I be getting Code 9434 while listing directory of sftp server.

    “SSH Routing Error: Failed to retrieve ssh session: Pool exhausted”

     

    BTW it was working fine earlier on with no change in credentials etc.

     

    Thank you very much