Global.helma.Ftp (server)
Sourcecode in /home/hannes/helma/modules/helma/Ftp.js:
22:   helma.Ftp = function(server) {
23:      var OK      =  0;
24:      var SOCKET  =  1;
25:      var TIMEOUT =  2;
26:      var LOGIN   = 10;
27:      var LOGOUT  = 11;
28:      var BINARY  = 20;
29:      var ASCII   = 21;
30:      var ACTIVE  = 22;
31:     var PASSIVE = 23;
32:     var CD      = 30;
33:     var LCD     = 31;
34:     var PWD     = 32;
35:     var DIR     = 33;
36:     var MKDIR   = 34;
37:     var RMDIR   = 35;
38:     var GET     = 40;
39:     var PUT     = 41;
40:     var DELETE  = 42;
41:  
42:     var FTP                    = Packages.com.oroinc.net.ftp.FTP;
43:     var FtpClient              = Packages.com.oroinc.net.ftp.FTPClient;
44:     var BufferedInputStream    = java.io.BufferedInputStream;
45:     var BufferedOutputStream   = java.io.BufferedOutputStream;
46:     var FileInputStream        = java.io.FileInputStream;
47:     var FileOutputStream       = java.io.FileOutputStream;
48:     var ByteArrayInputStream   = java.io.ByteArrayInputStream;
49:     var ByteArrayOutputStream  = java.io.ByteArrayOutputStream;
50:  
51:     var self = this;
52:     var className = "helma.Ftp";
53:  
54:     var ftpclient = new FtpClient();
55:     var localDir;
56:  
57:     var error = function(methName, errMsg) {
58:        var tx = java.lang.Thread.currentThread();
59:        tx.dumpStack();
60:        app.log("Error in " + className + ":" + methName + ": " + errMsg);
61:        return;
62:     };
63:  
64:     var debug = function(methName, msg) {
65:        msg = msg ? " " + msg : "";
66:        app.debug(className + ":" + methName + msg);
67:        return;
68:     };
69:  
70:     var setStatus = function(status) {
71:        if (self.status === OK) {
72:           self.status = status;
73:        }
74:        return;
75:     };
76:  
77:     var getStatus = function() {
78:        return self.status;
79:     };
80:  
81:     this.server = server;
82:     this.status = OK;
83:  
84:     this.toString = function() {
85:        return "[helma.Ftp " + server + "]";
86:     };
87:  
88:     this.setReadTimeout = function(timeout) {
89:        try {
90:           ftpclient.setDefaultTimeout(timeout);
91:           debug("setReadTimeout", timeout);
92:           return true;
93:        } catch(x) {
94:           error("setReadTimeout", x);
95:           setStatus(SOCKET);
96:        }
97:        return false;
98:     };
99:  
100:     this.setTimeout = function(timeout) {
101:        try {
102:           ftpclient.setDataTimeout(timeout);
103:           debug("setTimeout", timeout);
104:           return true;
105:        } catch(x) {
106:           error("setTimeout", x);
107:           setStatus(TIMEOUT);
108:        }
109:        return false;
110:     };
111:  
112:     this.login = function(username, password) {
113:        try {
114:           ftpclient.connect(this.server);
115:           var result = ftpclient.login(username, password);
116:           debug("login", username + "@" + server);
117:           return result;
118:        } catch(x) {
119:           error("login", x);
120:           setStatus(LOGIN);
121:       }
122:       return false;
123:    };
124: 
125:    this.binary = function() {
126:       try {
127:          var result = ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
128:          debug("binary");
129:          return result;
130:       } catch(x) {
131:          error("binary", x);
132:          setStatus(BINARY);
133:       }
134:       return false;
135:    };
136: 
137:    this.ascii = function() {
138:       try {
139:          var result = ftpclient.setFileType(FTP.ASCII_FILE_TYPE);
140:          debug("ascii");
141:          return result;
142:       } catch(x) {
143:          error("ascii", x);
144:          setStatus(ASCII);
145:       }
146:       return false;
147:    };
148: 
149:    this.active = function() {
150:       try {
151:          ftpclient.enterLocalActiveMode();
152:          debug("active");
153:          return true;
154:       } catch(x) {
155:          error("active", x);
156:          setStatus(ACTIVE);
157:       }
158:       return false;
159:    };
160: 
161:    this.passive = function() {
162:       try {
163:          ftpclient.enterLocalPassiveMode();
164:          debug("passive");
165:          return true;
166:       } catch(x) {
167:          error("passive", x);
168:          setStatus(PASSIVE);
169:       }
170:       return false;
171:    };
172: 
173:    this.pwd = function() {
174:       try {
175:          debug("pwd");
176:          return ftpclient.printWorkingDirectory();
177:       } catch(x) {
178:          error("pwd", x);
179:          setStatus(PWD);
180:       }
181:       return;
182:    };
183: 
184:    this.dir = function(path) {
185:       try {
186:          debug("dir", path);
187:          return ftpclient.listNames(path ? path : ".");
188:       } catch(x) {
189:          error("dir", x);
190:          setStatus(DIR);
191:       }
192:       return;
193:    };
194: 
195:    this.mkdir = function(dir) {
196:       try {
197:          var result = ftpclient.makeDirectory(dir);
198:          debug("mkdir", dir);
199:          return result;
200:       } catch(x) {
201:          error("mkdir", x);
202:          setStatus(MKDIR);
203:       }
204:       return false;
205:    };
206: 
207:    this.rmdir = function(dir) {
208:       try {
209:          var result = ftpclient.removeDirectory(dir);
210:          debug("rmdir", dir);
211:          return result;
212:       } catch(x) {
213:          error("rmdir", x);
214:          setStatus(RMDIR);
215:       }
216:       return false;
217:    };
218: 
219:    this.cd = function(path) {
220:       try {
221:          var result = ftpclient.changeWorkingDirectory(path);
222:          debug("cd", path);
223:          return result;
224:       } catch(x) {
225:          error("cd", x);
226:          setStatus(CD);
227:       }
228:       return false;
229:    };
230: 
231:    this.lcd = function(dir) {
232:       try {
233:          localDir = new File(dir);
234:          if (!localDir.exists()) {
235:             localDir.mkdir();
236:             debug("lcd", dir);
237:          }
238:          return true;
239:       } catch(x) {
240:          error("lcd", x);
241:          setStatus(LCD);
242:       }
243:       return false;
244:    };
245: 
246:    this.putFile = function(localFile, remoteFile) {
247:       try {
248:          if (localFile instanceof File) {
249:             var f = localFile;
250:          } else if (typeof localFile == "string") {
251:             if (localDir == null)
252:                var f = new File(localFile);
253:             else
254:                var f = new File(localDir, localFile);
255:          }
256:          var stream = new BufferedInputStream(
257:             new FileInputStream(f.getPath())
258:          );
259:          if (!remoteFile) {
260:             remoteFile = f.getName();
261:          }
262:          var result = ftpclient.storeFile(remoteFile, stream);
263:          stream.close();
264:          debug("putFile", remoteFile);
265:          return result;
266:       } catch(x) {
267:          error("putFile", x);
268:          setStatus(PUT);
269:       }
270:       return false;
271:    };
272: 
273:    this.putString = function(str, remoteFile, charset) {
274:       try {
275:          str = new java.lang.String(str);
276:          var bytes = charset ? str.getBytes(charset) : str.getBytes();
277:          var stream = ByteArrayInputStream(bytes);
278:          var result = ftpclient.storeFile(remoteFile, stream);
279:          debug("putString", remoteFile);
280:          return result;
281:       } catch(x) {
282:          error("putString", x);
283:          setStatus(PUT);
284:       }
285:       return false;
286:    };
287: 
288:    this.getFile = function(remoteFile, localFile) {
289:       try {
290:          if (localDir == null)
291:             var f = new File(localFile);
292:          else
293:             var f = new File(localDir, localFile);
294:          var stream = new BufferedOutputStream(
295:             new FileOutputStream(f.getPath())
296:          );
297:          var result = ftpclient.retrieveFile(remoteFile, stream);
298:          stream.close();
299:          debug("getFile", remoteFile);
300:          return result;
301:       } catch(x) {
302:          error("getFile", x);
303:          setStatus(GET);
304:       }
305:       return false;
306:    };
307: 
308:    this.getString = function(remoteFile) {
309:       try {
310:          var stream = ByteArrayOutputStream();
311:          ftpclient.retrieveFile(remoteFile, stream);
312:          debug("getString", remoteFile);
313:          return stream.toString();
314:       } catch(x) {
315:          error("getString", x);
316:          setStatus(GET);
317:       }
318:       return;
319:    };
320: 
321:    this.deleteFile = function(remoteFile) {
322:       try {
323:          var result = ftpclient.deleteFile(remoteFile);
324:          debug("deleteFile", remoteFile);
325:          return result;
326:       } catch(x) {
327:          error("deleteFile", x);
328:          setStatus(DELETE);
329:       }
330:       return false;
331:    };
332: 
333:    this.logout = function() {
334:       try {
335:          var result = ftpclient.logout();
336:          ftpclient.disconnect();
337:          debug("logout");
338:          return result;
339:       } catch(x) {
340:          error("logout", x);
341:          setStatus(LOGOUT);
342:       }
343:       return false;
344:    };
345: 
346:    for (var i in this)
347:       this.dontEnum(i);
348: 
349:    return this;
350: }