1/**
2 * Connected to FTP server.
3 *
4 * @input Hostname : String The host name
5 * @input Username : String The user name
6 * @input Password : String The password
7 * @input Directory : String The directory
8 * @input sampleLogger : Object Sample logger input for Sample logger
9 * @output sampleLogger : Object Sample logger debug output for Sample logger
10 * @output sampleError : Object Sample error error output for Sample logger
11 */
12importPackage(dw.system);
13importPackage(dw.net);
14
15
16
17// Sample Logging ImportScript
18importScript("bc_sample:library/common/libContext.ds");
19importScript("bc_sample:library/utility/exception/libsampleException.ds");
20importScript("bc_sample:library/utility/logging/libLog.ds");
21
22
23
24function execute(pdict: PipelineDictionary): Number {
25 // Sample Logging var
26 var _context: String = new Context("GetFilesFromFTPServer.ds");
27 var _logger: Log = Log.getLogger(pdict);
28
29 var scriptName: String = "bc_sample:pipelets/impex/GetFilesFromFTPServer.ds";
30
31 if (Logger.isDebugEnabled()) {
32
33 Logger.debug(scriptName + ": BEGIN");
34 }
35
36 //*** get script parameters ***//
37 var host: String = pdict.Hostname;
38 var userName: String = pdict.Username;
39 var password: String = pdict.Password;
40
41 var ftpClient: FTPClient = new FTPClient();
42 var connectedFlag: Boolean = false;
43
44 try {
45
46 connectedFlag = ftpClient.connect(host, userName, password);
47 } catch (e) {
48 // Sample Logging wrap error into custom exception
49 var exception: sampleException = new sampleException(_context, scriptName + ': Error connecting to FTP server: ' + +host + '\n' + e.message, e);
50
51 // Sample Logging call into error handler
52 _logger.error(_context, scriptName + ': Error connecting to FTP server: ' + +host + '\n' + e.message + exception);
53 pdict.sampleError = exception;
54
55 return PIPELET_ERROR;
56 }
57
58 if (connectedFlag) {
59
60 if (Logger.isDebugEnabled()) {
61
62 _logger.debug(_context, scriptName + ': Connected to host: ' + host);
63 }
64
65 try {
66
67 /*var files = ftpClient.list();
68 for each (var file : FTPFileInfo in files) {
69
70 Logger.debug("---> " + file.name);
71 }*/
72
73 ftpClient.disconnect();
74
75 if (Logger.isDebugEnabled()) {
76
77 _logger.debug(_context, scriptName + ': Disconnected from host: ' + host);
78 }
79 } catch (e) {
80
81 _logger.error(_context, scriptName + ': FTP opreration failed: ' + e.message);
82
83 return PIPELET_ERROR;
84 }
85 } else {
86
87 if (Logger.isDebugEnabled()) {
88
89 _logger.debug(_context, scriptName + ': Could not connect to host: ' + host);
90 }
91
92 return PIPELET_ERROR;
93 }
94
95 //*** set script output parameters ***//
96
97
98 if (Logger.isDebugEnabled()) {
99
100 _logger.debug(_context, scriptName + ": END");
101 }
102
103 return PIPELET_NEXT;
104}