GetFilesFromFTPServer.ds

Note: This code is provided as a sample. It isn't a supported part of the SiteGenesis application.

/**
 * Connected to FTP server.
 *
 * @input Hostname   : String    The host name
 * @input Username   : String    The user name
 * @input Password   : String    The password
 * @input Directory  : String    The directory
 * @input sampleLogger  : Object Sample logger input for Sample logger
 * @output sampleLogger : Object Sample logger debug output for Sample logger
 * @output sampleError  : Object Sample error error output for Sample logger
 */
importPackage(dw.system);
importPackage(dw.net);



// Sample Logging ImportScript
importScript("bc_sample:library/common/libContext.ds");
importScript("bc_sample:library/utility/exception/libsampleException.ds");
importScript("bc_sample:library/utility/logging/libLog.ds");



function execute(pdict: PipelineDictionary): Number {
    // Sample Logging var
    var _context: String = new Context("GetFilesFromFTPServer.ds");
    var _logger: Log = Log.getLogger(pdict);

    var scriptName: String = "bc_sample:pipelets/impex/GetFilesFromFTPServer.ds";

    if (Logger.isDebugEnabled()) {

        Logger.debug(scriptName + ": BEGIN");
    }

    //*** get script parameters ***//
    var host: String = pdict.Hostname;
    var userName: String = pdict.Username;
    var password: String = pdict.Password;

    var ftpClient: FTPClient = new FTPClient();
    var connectedFlag: Boolean = false;

    try {

        connectedFlag = ftpClient.connect(host, userName, password);
    } catch (e) {
        // Sample Logging wrap error into custom exception
        var exception: sampleException = new sampleException(_context, scriptName + ': Error connecting to FTP server: ' + +host + '\n' + e.message, e);

        // Sample Logging call into error handler
        _logger.error(_context, scriptName + ': Error connecting to FTP server: ' + +host + '\n' + e.message + exception);
        pdict.sampleError = exception;

        return PIPELET_ERROR;
    }

    if (connectedFlag) {

        if (Logger.isDebugEnabled()) {

            _logger.debug(_context, scriptName + ': Connected to host: ' + host);
        }

        try {

            /*var files = ftpClient.list();
   for each (var file : FTPFileInfo in files) {
   
    Logger.debug("---> " + file.name);
   }*/

            ftpClient.disconnect();

            if (Logger.isDebugEnabled()) {

                _logger.debug(_context, scriptName + ': Disconnected from host: ' + host);
            }
        } catch (e) {

            _logger.error(_context, scriptName + ': FTP opreration failed: ' + e.message);

            return PIPELET_ERROR;
        }
    } else {

        if (Logger.isDebugEnabled()) {

            _logger.debug(_context, scriptName + ': Could not connect to host: ' + host);
        }

        return PIPELET_ERROR;
    }

    //*** set script output parameters ***//


    if (Logger.isDebugEnabled()) {

        _logger.debug(_context, scriptName + ": END");
    }

    return PIPELET_NEXT;
}