urlopen.open() to open a DataPower file

The urlopen.open() API to open files in the DataPower® file system.

Syntax

var urlopen = require('urlopen');
urlopen.open(options,callback);

Parameters

options
The set of properties that are grouped as a JSON object or a single URL specification for a target file.
  • The URL of the target file in the DataPower file system.

    For example, urlopen.open('local:///test.xml', callback). In this case, the method defaults to the GET method.

  • A set of properties as a JSON object. Only the following properties are valid.
    target:
    The URL of the target file as a string.
    target: 'local:///test.xml' 
    method:
    The only supported method is GET.
callback
The asynchronous function to call after the urlopen.open() connects, sends the data to the target, and the response header is parsed. The callback is in the function(error,response){} form.
The callback function is the same as the callback parameter of the urlopen.open() API.

Guidelines

The urlopen.open() API opens files that are specified with the target property. Local file access is distinguished by the protocol scheme.
  • You can access files with only the local:, store:, and temporary: protocol schemes.
  • File references are case-sensitive. For example, local:///myArea/myFile.XML is different from local:///myArea/myFile.xml.

Examples

  • Access mylocal_file.xml in local:///mylocal_directory, read the response as a Buffer object, and write the data to the output context.
    var urlopen = require('urlopen');
    urlopen.open("local:///mylocal_directory/mylocal_file.xml", 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
          response.readAsBuffer (function (error, data) {
            session.output.write(data);
          });
        }
      }
    });
  • Attempt to access a nonexistent file returns a 404 File not found status code.
    var urlopen = require('urlopen');
    urlopen.open("local:///nonexisting.txt", function (error, response) {
      if (error) {
        session.output.write("openCallback error: " + error.errorMessage+"\n");
      }
      else {
        if (response.statusCode != 200) {
          // in this case, a non-200 statusCode indicates a problem reading the file
          session.output.write("Unable to open the file, statusCode: " +
                                response.statusCode + ", reasonPhrase: " +
                                response.reasonPhrase);
          response.disconnect();   // Optional: You can call response.disconnect()
                                   // to do an explicit disconnection. Without this
                                   // call, urlopen does an implicit disconnection
        }
        else {
          response.readAsBuffer(function(readError, data) {
            if (readError) {
              session.output.write("read file error: " + readError.toString());
            }
            else {
              session.output.write(data);
            }
          });
        }
      }
    });
  • Use a JSON object to specify the options parameter of the urlopen.open() API.
    var urlopen = require('urlopen');
    var open_options = {
      // the target URL
      target: 'local:///mylocal_directory/mylocal_file.xml',
      // method is optional, default is GET
      method: 'GET'
    };
    
    urlopen.open(open_options, function (error, response) {
      if (error) {
        session.output.write("openCallback error: " + error.errorMessage+"\n");
      }
      else {
        if (response.statusCode != 200) {
          // in this case, a non-200 statusCode indicates a problem reading the file
          session.output.write("Unable to open the file, statusCode: " +
                                response.statusCode + ", reasonPhrase: " +
                                response.reasonPhrase);
          response.disconnect(); // optional, urlopen will do an implicit disconnect()
        }
        else {
          response.readAsBuffer(function(readError, data) {
            if (readError) {
              session.output.write("read file error: " + readError.toString());
            }
            else {
              session.output.write(data);
            }
          });
        }
      }
    });