GetFilesFromHttpServer.ds

Review the GetFilesFromHTTPServer.ds referenced script files when scheduling inventory imports.

Note: This code is provided as a sample. It isn't a supported part of the SiteGenesis application.
/*
 * Gets a file list from an HTTP(S) server
 *
 * @input ConnectionUrl  : String    of the server URL including protocol
 * @input Login    : String    The login for authentication
 * @input Password   : String    The password for authentication
 * @input SortDirection  : String    'ASCENDING' or 'DESCENDING'
 * @input NamePattern   : String    The pattern for the filenames (use ".*" to get all)
 * @input ServerPath   : String    the directory path at the server
 * @input sampleLogger   : Object Sample logger input for Sample logger
 *
 * @output FileList   : dw.util.Collection  The files matching the pattern
 * @output StatusCode   : Number    The servers status code
 * @output StatusMessage  : String    The servers status message
 * @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.util );
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("GetFilesFromHttpServer.ds");
 var _logger : Log = Log.getLogger(pdict);
 
 // constants for parsing
 var pattern_dir  : String ="/";
 var tag_a_href   : String = "<a";   //<a href="...
 var char_left   : String = '"';    // .....>file_name<...
 var char_right   : String = '"';
 // input variables 
 // input variables 
 var connectionURL   : String = pdict.ConnectionUrl;
 var login     : String = pdict.Login;
 var password    : String = pdict.Password;
 var sortDirection   : String = pdict.SortDirection;
 var pattern    : RegExp = new RegExp(pdict.NamePattern);
 var serverPath    : String = pdict.ServerPath;
 
 var httpSvc : HTTPClient = new HTTPClient();
 
 var url : String = connectionURL + serverPath;
 
 // get server response for directory
 httpSvc.open("POST",url); // old httpSvc.open("POST",url,login,password);
 try{
  httpSvc.send();
 }
 catch( e ){
  var exception : sampleException = new sampleException(_context, "Could not connect to remote resource {0}. ( {1} )",url,e);
  _logger.error(_context, "Could not connect to remote resource {0}. ( {1} )" + exception);
  pdict.sampleError = exception; 
  
  return PIPELET_ERROR;
 }
 if(httpSvc.statusCode != 200){
  _logger.error(_context, "Server responded with Status code {0} ( {1} )!",httpSvc.statusCode,httpSvc.statusMessage);
  return PIPELET_ERROR;
 }else{
  _logger.debug(_context, "Successfully read directory listing of {0}.",url);
 }
 
 var response = httpSvc.text;
 
 _logger.debug(_context, "Content: " + response);
 var fileName : String = "";
 var sorted : SortedSet = ((sortDirection == "DESCENDING") ? new SortedSet(descending) :  new SortedSet());
 var line = response;
 
 // IIS HTML Format
 var line_pos : Number = 0;
 var index : String = line;
 var indexLowerCase : String = index.toLowerCase();
 
 //++line_count;
 while (line_pos != -1)
 {
  //find contents of <a...>needed_text</a>
  if ((line_pos = indexLowerCase.indexOf(tag_a_href, line_pos + 6)) != -1)
  {
   if ((line_pos = indexLowerCase.indexOf(char_left, line_pos + tag_a_href.length)) != -1)
   {
    var idx = indexLowerCase.indexOf(char_right, line_pos + 1);
    fileName = index.substring(line_pos + 1, idx);
    // Remove any directory from the filename
    if (fileName.lastIndexOf(pattern_dir) != -1 && fileName.lastIndexOf(pattern_dir) + 1 < fileName.length) {
     fileName = fileName.substring(fileName.lastIndexOf(pattern_dir) + 1);
    }
 
    _logger.debug(_context, "Found Filename '" + fileName + "'");
 
             if (idx != -1 && fileName.match(pattern) && fileName.substring(fileName.length - 1) != pattern_dir)
    {
     sorted.add(fileName);
    }
   }
  }
 }
 
 pdict.StatusCode = httpSvc.statusCode;
 pdict.StatusMessage = httpSvc.statusMessage;
 
 // if directory contains files, return them
 if ( !sorted.empty)
 {
     // store 'FileList' in pipeline dictionary
     pdict.FileList = sorted;
 
     return PIPELET_NEXT;
 }
 return PIPELET_ERROR;
}
function descending( var1 : Object, var2) : Number{
 if( var1 == var2) return 0
  else if ( var1 < var2) return -1
   else return 1;
}