1/*
2 * Gets a file list from an HTTP(S) server
3 *
4 * @input ConnectionUrl : String of the server URL including protocol
5 * @input Login : String The login for authentication
6 * @input Password : String The password for authentication
7 * @input SortDirection : String 'ASCENDING' or 'DESCENDING'
8 * @input NamePattern : String The pattern for the filenames (use ".*" to get all)
9 * @input ServerPath : String the directory path at the server
10 * @input sampleLogger : Object Sample logger input for Sample logger
11 *
12 * @output FileList : dw.util.Collection The files matching the pattern
13 * @output StatusCode : Number The servers status code
14 * @output StatusMessage : String The servers status message
15 * @output sampleLogger : Object Sample logger debug output for Sample logger
16 * @output sampleError : Object Sample error error output for Sample logger
17 *
18 */
19importPackage( dw.system );
20importPackage( dw.util );
21importPackage( dw.net );
22// Sample Logging ImportScript
23importScript("bc_sample:library/common/libContext.ds");
24importScript("bc_sample:library/utility/exception/libsampleException.ds");
25importScript("bc_sample:library/utility/logging/libLog.ds");
26/*
27*/
28function execute( pdict : PipelineDictionary ) : Number {
29 // Sample Logging var
30 var _context : String = new Context("GetFilesFromHttpServer.ds");
31 var _logger : Log = Log.getLogger(pdict);
32
33 // constants for parsing
34 var pattern_dir : String ="/";
35 var tag_a_href : String = "<a"; //<a href="...
36 var char_left : String = '"'; // .....>file_name<...
37 var char_right : String = '"';
38 // input variables
39 // input variables
40 var connectionURL : String = pdict.ConnectionUrl;
41 var login : String = pdict.Login;
42 var password : String = pdict.Password;
43 var sortDirection : String = pdict.SortDirection;
44 var pattern : RegExp = new RegExp(pdict.NamePattern);
45 var serverPath : String = pdict.ServerPath;
46
47 var httpSvc : HTTPClient = new HTTPClient();
48
49 var url : String = connectionURL + serverPath;
50
51 // get server response for directory
52 httpSvc.open("POST",url); // old httpSvc.open("POST",url,login,password);
53 try{
54 httpSvc.send();
55 }
56 catch( e ){
57 var exception : sampleException = new sampleException(_context, "Could not connect to remote resource {0}. ( {1} )",url,e);
58 _logger.error(_context, "Could not connect to remote resource {0}. ( {1} )" + exception);
59 pdict.sampleError = exception;
60
61 return PIPELET_ERROR;
62 }
63 if(httpSvc.statusCode != 200){
64 _logger.error(_context, "Server responded with Status code {0} ( {1} )!",httpSvc.statusCode,httpSvc.statusMessage);
65 return PIPELET_ERROR;
66 }else{
67 _logger.debug(_context, "Successfully read directory listing of {0}.",url);
68 }
69
70 var response = httpSvc.text;
71
72 _logger.debug(_context, "Content: " + response);
73 var fileName : String = "";
74 var sorted : SortedSet = ((sortDirection == "DESCENDING") ? new SortedSet(descending) : new SortedSet());
75 var line = response;
76
77 // IIS HTML Format
78 var line_pos : Number = 0;
79 var index : String = line;
80 var indexLowerCase : String = index.toLowerCase();
81
82 //++line_count;
83 while (line_pos != -1)
84 {
85 //find contents of <a...>needed_text</a>
86 if ((line_pos = indexLowerCase.indexOf(tag_a_href, line_pos + 6)) != -1)
87 {
88 if ((line_pos = indexLowerCase.indexOf(char_left, line_pos + tag_a_href.length)) != -1)
89 {
90 var idx = indexLowerCase.indexOf(char_right, line_pos + 1);
91 fileName = index.substring(line_pos + 1, idx);
92 // Remove any directory from the filename
93 if (fileName.lastIndexOf(pattern_dir) != -1 && fileName.lastIndexOf(pattern_dir) + 1 < fileName.length) {
94 fileName = fileName.substring(fileName.lastIndexOf(pattern_dir) + 1);
95 }
96
97 _logger.debug(_context, "Found Filename '" + fileName + "'");
98
99 if (idx != -1 && fileName.match(pattern) && fileName.substring(fileName.length - 1) != pattern_dir)
100 {
101 sorted.add(fileName);
102 }
103 }
104 }
105 }
106
107 pdict.StatusCode = httpSvc.statusCode;
108 pdict.StatusMessage = httpSvc.statusMessage;
109
110 // if directory contains files, return them
111 if ( !sorted.empty)
112 {
113 // store 'FileList' in pipeline dictionary
114 pdict.FileList = sorted;
115
116 return PIPELET_NEXT;
117 }
118 return PIPELET_ERROR;
119}
120function descending( var1 : Object, var2) : Number{
121 if( var1 == var2) return 0
122 else if ( var1 < var2) return -1
123 else return 1;
124}