DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Mon November 14, 2022 09:38 AM
    Actually I've implemented 2 cases.
    case 1 : 
    by using the following gatewayscript code i can able to download a file but it is downloaded by .file extension but when i renamed it to .pdf then my pdf file is opening.
    the code is :
    var urlopen = require('urlopen');
    var fs = require('fs');
    urlopen.open("local:///api.pdf", function (error, response) {
      if (error) {
        session.output.write("openCallback error: " + error.errorMessage+"\n");
      }
      else {
        if (response.statusCode == 200) {
          // You have a 200, so you can read the file
          fs.readFile("local:///api.pdf", function(error,data) {
            session.output.write(data);
          });
        }
      }
    });

    case 2 :
    by using the following gatewayscript code i can able to open a pdf file when the url is placed in the browser but it is showing "failed to open pdf document".
    a response attachment is added for your understanding.

    var urlopen = require('urlopen');
    var fs = require('fs');
    urlopen.open("local:///api.pdf", function (error, response) {
      if (error) {
        session.output.write("openCallback error: " + error.errorMessage+"\n");
      }
      else {
        if (response.statusCode == 200) {
          // You have a 200, so you can read the file
          fs.readFile("local:///api.pdf", function(error,data) {
            session.output.write(data+'.pdf');
          });
        }
      }
    });

    so now the issue is either i want to load the pdf in browser or i need a file with ".pdf" extension should be downloaded.
    please can anyone will help me regarding this.
    Thanks in Advance....!

    ------------------------------
    Thanks and Regards,
    Vyasavardhan Ramagiri
    ------------------------------


  • 2.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Tue November 15, 2022 03:38 AM
    Hi,

    I always use Fetch - action to get the files from DP local folders. Just remember to change into Advanced tab and select output as 'Binary'.

    ------------------------------
    Hermanni Pernaa
    ------------------------------



  • 3.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Tue November 15, 2022 10:56 AM

    Hi Vyasavardhan,

    I agree with Hermanni that if all you want to do is read the file from the local file system and place it into a DataPower context, fetch with the binary output type is a lot simpler than writing your own code.  I do have some questions about your code though.  The urlopen.open and fs.readFile functions are effectively doing the same thing, they are reading the file from the file system and providing you a variable that will contain the file contents.  It appears you are using urlopen.open simply to determine if the file exists by getting a status code of 200, but assuming you do, you can then do a response.readAsBuffer function within the urlopen.open to use the response object to get the payload.  The urlopen.open has already read the payload, but you must access it with a readAsxxxx function.  In fact, https://www.ibm.com/docs/en/datapower-gateway/10.0.1?topic=apis-urlopen-module#urlopen.opentoopenlocalfiles has an example of exactly this. By not doing a readAsBuffer function on the response, you're probably seeing log messages indicating that a payload was implicitly discarded and forcing DataPower to do a lot of unnecessary cleanup for you.

    You could also simply use the fs.exists() function to determine if the file exists, and if so do the  fs.readFile within its async function.  In fact https://www.ibm.com/docs/en/datapower-gateway/10.0.1?topic=apis-fs-module#fs.read has that exact example.

    Finally, your first example has the correct session.output.write(data).  The session.output will take the data in the argument and will write it to the output context specified on the GatewayScript action that references your .js.  The second example with session.output.write(data+'.pdf'); is taking your pdf data and concatenating a string to it which would make the data written to your output context to have non pdf data.  

    From a DataPower perspective, what are you trying to accomplish?  Either using the fetch action, the urlopen.open with a subsequent response.readAsBuffer, or the fs.readFile function, the latter two writing the returned file content to a DataPower context,  are you wanting a browser, doing a HTTP GET to your DataPower service, to return your pdf as a payload that the browser would automatically show in the browser?  I'm not sure without some research about what response headers you'd need to provide, but if you're providing the payload correctly, then you'll need response headers, for example you would need at a minimum a pdf content type (application/pdf) in the transaction's  response headers. After deciding upon the mechanism of getting the file content into a DataPower context, you can set the response payload content type using a GatewayScript action, transformation action (xslt), or even a set variable action.  In GatewayScript

    var hm = require('header-metadata');
    hm.response.set('Content-Type', 'application/pdf');

    Best Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 4.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Wed November 16, 2022 12:19 AM
    Edited by Stefen Salvatore Wed November 16, 2022 12:20 AM
    Thanks Steve Linn,

    It worked fine when i added header metadata and content type. But when I used the same code in API Connect it again showing "Failed to Load PDF Document" please let me know if any changes required when i used the same in V10.
    the code i used in apic is :

    var hm = require('header-metadata');
    hm.response.set('Content-Type', 'application/pdf');
    var urlopen = require('urlopen');
    urlopen.open("local:///api.pdf", function (error, response) {
    if (error) {
    session.output.write("openCallback error: " + error.errorMessage+"\n");
    }
    else {
    session.output.write(response);

    }
    });

    Thanks in Advance.

    ------------------------------
    Vyasavardhan Ramagiri
    ------------------------------



  • 5.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Wed November 16, 2022 09:23 AM

    Hi Vyasavardhan,
    Glad to hear your DataPower service is now working!  As for API Connect, you say you're using v10?  I believe the session.output.write will be equivalent to a context.message.body.write(response), but as for the content-type, try not using the header-metadata module but instead try context.message.header.set('Content-Type', 'application/pdf').

    Regards,

    Steve Linn



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 6.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Mon November 28, 2022 05:04 AM
    Hi Steve,
             I'm here for another help from you i.e as above we've loaded the pdf from Datapower local directory that's fine. But what if i want to load the file which is stored in 'ftp'? can you please help me in Gatewayscript regarding this.


    Thanks in Advance.

    ------------------------------
    Vyasavardhan Ramagiri
    ------------------------------



  • 7.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Mon November 28, 2022 09:28 AM

    Hi Vyasavardhan,
    GatewayScript via urlopen.open only supports http(s), (dp)mq, and recently graphql protocols but not ftp, so you would need in your GatewayScript to use transform.xslt to execute an xslt that would use the dp:url-open extension function which does support the ftp protocol.  You an pass in parameters to your xslt  (the url for example) and the output of the xslt is returned to the GatewayScript code.

    Best Regards,

    Steve Linn



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 8.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Mon November 28, 2022 09:30 AM
    There are many ways this can be done, so you may need to clarify.

    1. The file is stored on DataPower (like you have it now), and the client wants to use (S)FTP to fetch it?
    2. The file is stored on a back end server and you need DataPower to fetch it on behalf of the client.  There are two scenarios here:
      1. The client wants to fetch it with HTTP(S)
      2. The client wants to fetch it with (S)FTP

    In scenarios #1 above, you can simply setup an SFTP server handler on DataPower using a virtual ephemeral file system and put your files in one of the virtual directories configured in the handler. 

    For scenario #2 above, you're setting up a handler with a transparent file system with a connection from DataPower to the actual FTP back end server.  This can become slightly more complicated if you decide the client should not have direct access to the actual FTP server, and their only access to the files is through DataPower, but that just means using a different account (datapower account if you will) when retrieving the file from the actual FTP server.


    ​​​​​

    ------------------------------
    Joseph Morgan
    ------------------------------



  • 9.  RE: How can i open a pdf file which is stored in local diretory of datapower through get method?

    Posted Thu December 01, 2022 02:47 AM

    Hi Joseph,

    My need is ' assume that a pdf is already there in (s)ftp server now i need to load it through my api connect via get method '
    So i need to do it with gatewayscript code. As steeve said it won't be possible to call ftp using gatewayscript. Then i need to do it with xslt.

    If possible anyone please guide me with the syntax of url open in xslt for ftp open.

    Thanks in Advance!!



    ------------------------------
    Vyasavardhan Ramagiri
    ------------------------------