001: /*
002: *
003: * Created on March 14, 2004
004: */
005: package org.enhydra.snapperAdmin.business;
006:
007: import java.io.File;
008: import java.io.FileOutputStream;
009: import java.util.Vector;
010:
011: import org.apache.commons.httpclient.Credentials;
012: import org.apache.commons.httpclient.HttpException;
013: import org.apache.commons.httpclient.HttpURL;
014: import org.apache.commons.httpclient.NTCredentials;
015: import org.apache.commons.net.ftp.FTPClient;
016: import org.apache.commons.net.ftp.FTPFile;
017: import org.apache.webdav.lib.WebdavResource;
018: import org.apache.webdav.lib.methods.DepthSupport;
019: import org.enhydra.snapperAdmin.Log;
020: import org.enhydra.snapperAdmin.SnapperAdmin;
021:
022: /**
023: *
024: * Class is client for comunicate with file server(s) over net.
025: * This class is used by application to retrive files
026: *
027: *
028: * @author Milin Radivoj
029: */
030: public class DocumentStore {
031:
032: private static final int LOCAL = 0;
033: private static final int FTP = 1;
034: private static final int UNC = 2;
035: private static final int WebDAV = 3;
036:
037: public static final String LOCAL_STORE = "local";
038: public static final String FTP_STORE = "FTP";
039: public static final String UNC_STORE = "UNC";
040: public static final String WebDAV_STORE = "WebDAV";
041:
042: String user;
043: String docStorePath;
044: String host;
045: String port;
046: String login;
047: String password;
048: String tempFolder;
049: int type = -1;
050: FTPClient ftp;
051: WebdavResource wdr = null;
052:
053: Vector originalFiles, timestamps, owners, creationDate;
054:
055: public DocumentStore(String currentUser, String path, String type,
056: String host, String port, String login, String password,
057: String tempFolder) throws Exception {
058:
059: if (path == null)
060: path = "";
061:
062: this .user = currentUser;
063: this .docStorePath = path;
064: this .host = host;
065: this .port = port;
066: this .login = login;
067: this .password = password;
068: this .tempFolder = tempFolder;
069:
070: if (type.equals("local"))
071: this .type = LOCAL;
072: else if (type.equals("FTP")) {
073: if (host.endsWith("/"))
074: host = host.substring(0, host.length() - 1);
075:
076: if (host.startsWith("ftp://"))
077: host = host.substring("ftp://".length());
078:
079: this .host = host;
080: this .type = FTP;
081: } else if (type.equals("UNC")) {
082: if (host.endsWith("/"))
083: host = host.substring(0, host.length() - 1);
084: this .type = UNC;
085: } else if (type.equals("WebDAV"))
086: this .type = WebDAV;
087: else
088: throw new Exception("Invalid DocumentStore type!");
089: }
090:
091: public void retrieveWebDAVFileNames() throws Exception {
092:
093: originalFiles = new Vector();
094: timestamps = new Vector();
095: creationDate = new Vector();
096: NTCredentials NTcr = null;
097:
098: String root = this .host + docStorePath;
099:
100: if (!root.endsWith("/"))
101: root = root + "/";
102:
103: HttpURL hrl = new HttpURL(root);
104:
105: if (hrl == null)
106: throw new Exception("Coul not create url : " + root);
107:
108: if (login != null && password != null) {
109: hrl.setUserinfo(login, password);
110: NTcr = new NTCredentials(login, password, root, "");
111: } else
112: throw new Exception("Mising user configuration");
113:
114: try {
115: //wdr = new WebdavResource(hrl);
116: wdr = new WebdavResource(hrl, (NTCredentials) NTcr,
117: WebdavResource.DEFAULT, DepthSupport.DEPTH_1);
118: //wdr.setCredentials(NTcr);
119: if (!wdr.exists()) {
120: wdr.close();
121: wdr = null;
122: throw new Exception(" URL : " + root + " not exist !!!");
123: }
124: if (!wdr.isCollection()) {
125: wdr.close();
126: wdr = null;
127: throw new Exception("Path is currently a file !!!");
128: }
129:
130: String[] listOfFiles = wdr.list();
131:
132: String rootPath = wdr.getPath();
133:
134: for (int i = 0; i < listOfFiles.length; i++) {
135:
136: try {
137: wdr.setPath(rootPath + listOfFiles[i]);
138:
139: if (!wdr.isCollection()) {
140: originalFiles.add(root + listOfFiles[i]);
141: timestamps.add(new Long(wdr
142: .getGetLastModified()));
143: creationDate
144: .add(new Long(wdr.getCreationDate()));
145: } else {
146: retrieveSubFolderWebDAVFiles(rootPath
147: + listOfFiles[i], root + listOfFiles[i]);
148: }
149:
150: } catch (HttpException httpe) {
151: SnapperAdmin.logError(" Skipping file :" + root
152: + listOfFiles[i] + " message : "
153: + httpe.getMessage());
154: continue;
155: }
156:
157: }
158: } catch (HttpException e) {
159: SnapperAdmin
160: .logError(" Unexpected exception occured, message : "
161: + e.getMessage()
162: + ", code : "
163: + e.getReasonCode());
164: return;
165: }
166: }
167:
168: private void retrieveSubFolderWebDAVFiles(String currentPath,
169: String root) {
170: if (!root.endsWith("/"))
171: root = root + "/";
172:
173: if (!currentPath.endsWith("/"))
174: currentPath = currentPath + "/";
175:
176: try {
177:
178: wdr.setPath(currentPath);
179: String[] listOfFiles = null;
180: try {
181: listOfFiles = wdr.list();
182: } catch (Exception httpe) {
183: SnapperAdmin
184: .logError(" Problem occured while colecting list of WebDAV files, path :"
185: + root
186: + " message : "
187: + httpe.getMessage());
188: }
189:
190: if (listOfFiles != null)
191: for (int i = 0; i < listOfFiles.length; i++) {
192: try {
193: wdr.setPath(currentPath + listOfFiles[i]);
194: } catch (Exception httpe) {
195: SnapperAdmin
196: .logError(" Problem occured while accessing file in WebDAV path :"
197: + currentPath
198: + listOfFiles[i]
199: + " message : "
200: + httpe.getMessage());
201: }
202: if (!wdr.isCollection()) {
203: originalFiles.add(root + listOfFiles[i]);
204: timestamps.add(new Long(wdr
205: .getGetLastModified()));
206: creationDate
207: .add(new Long(wdr.getCreationDate()));
208: } else {
209: retrieveSubFolderWebDAVFiles(currentPath
210: + listOfFiles[i], root + listOfFiles[i]);
211: }
212: }
213:
214: } catch (Exception httpe) {
215: if (SnapperAdmin.printStackTrace()) {
216: httpe.printStackTrace();
217: }
218: SnapperAdmin
219: .logError(" Problem occured while colecting files in WebDAV path :"
220: + root + " message : " + httpe.getMessage());
221: return;
222: }
223: }
224:
225: public File getWebDAVFile(String filePath) throws Exception {
226: if (wdr == null) {
227:
228: HttpURL hrl = new HttpURL(filePath);
229:
230: if (hrl == null)
231: throw new Exception("Coul not create url : " + filePath);
232:
233: if (login != null && password != null)
234: hrl.setUserinfo(login, password);
235: else
236: throw new Exception("Mising user configuration");
237:
238: wdr = new WebdavResource(hrl);
239: if (!wdr.exists()) {
240: throw new Exception(" URL : " + filePath
241: + " not exist !!!");
242: }
243: }
244:
245: String host = wdr.getHost();
246:
247: if (filePath.indexOf(host) != -1)
248: filePath = filePath.substring(filePath.indexOf(host)
249: + host.length());
250: else
251: return null;
252:
253: String rootTempFolder;
254:
255: if (tempFolder != null)
256: rootTempFolder = tempFolder;
257: else
258: rootTempFolder = System.getProperty("user.home");
259:
260: if (rootTempFolder.endsWith("/")
261: || rootTempFolder.endsWith("\\"))
262: rootTempFolder = rootTempFolder
263: + Thread.currentThread().getName() + File.separator
264: + "WebDAV" + File.separator;
265: else
266: rootTempFolder = rootTempFolder + File.separator
267: + Thread.currentThread().getName() + File.separator
268: + "WebDAV" + File.separator;
269:
270: File tempFile = new File(rootTempFolder + filePath);
271:
272: File destDir = tempFile.getParentFile();
273: if (!destDir.exists() && !destDir.mkdirs())
274: throw new Exception("Couldn't create dir " + destDir);
275: tempFile.createNewFile();
276:
277: wdr.setPath(filePath);
278:
279: if (!wdr.getMethod(tempFile)) {
280: SnapperAdmin
281: .logError(" File could not be saved from WebDAV :"
282: + filePath);
283: }
284:
285: return tempFile;
286: }
287:
288: public void closeWebDAV() {
289: try {
290: if (originalFiles != null) {
291: originalFiles.clear();
292: originalFiles = null;
293: }
294:
295: if (timestamps != null) {
296: timestamps.clear();
297: timestamps = null;
298: }
299:
300: if (creationDate != null) {
301: creationDate.clear();
302: creationDate = null;
303: }
304:
305: wdr.close();
306: } catch (Exception e) {
307: }
308: wdr = null;
309: }
310:
311: public void closeFTP() {
312: try {
313: if (originalFiles != null) {
314: originalFiles.clear();
315: originalFiles = null;
316: }
317:
318: if (timestamps != null) {
319: timestamps.clear();
320: timestamps = null;
321: }
322:
323: if (owners != null) {
324: owners.clear();
325: owners = null;
326: }
327:
328: if (ftp.isConnected())
329: ftp.disconnect();
330: } catch (Exception e) {
331: }
332: ftp = null;
333: }
334:
335: public File[] retrieveUNCFiles(String path) throws Exception {
336: File retVal = null;
337: retVal = new File(this .host + File.separator + path);
338: return retVal.listFiles();
339:
340: }
341:
342: public void retrieveFTPFileNames() throws Exception {
343:
344: originalFiles = new Vector();
345: timestamps = new Vector();
346: owners = new Vector();
347:
348: if (this .type == LOCAL) {
349:
350: } else if (this .type == FTP) {
351:
352: try {
353: FTPFile[] files = ftp.listFiles();
354: for (int i = 0; i < files.length; i++) {
355:
356: if (!files[i].isDirectory()) {
357: originalFiles.add("ftp://" + this .host + "/"
358: + docStorePath + "/"
359: + files[i].getName());
360: timestamps.add(new Long(files[i].getTimestamp()
361: .getTimeInMillis()));
362: owners.add(files[i].getUser());
363:
364: } else {
365: retrieveSubFolderFTPFiles(files[i].getName(),
366: docStorePath);
367: }
368: }
369:
370: } catch (Exception e) {
371: SnapperAdmin
372: .logError(" Problem occured while colecting files in FTP path :"
373: + "ftp://"
374: + this .host
375: + "/"
376: + docStorePath);
377: return;
378: }
379: }
380: }
381:
382: private void retrieveSubFolderFTPFiles(String currentPath,
383: String prefix) {
384:
385: try {
386: FTPFile[] files;
387: ftp.cwd(currentPath);
388: files = ftp.listFiles();
389: for (int i = 0; i < files.length; i++) {
390: if (!files[i].isDirectory()) {
391: originalFiles.add("ftp://" + this .host + "/"
392: + prefix + "/" + currentPath + "/"
393: + files[i].getName());
394: timestamps.add(new Long(files[i].getTimestamp()
395: .getTimeInMillis()));
396: owners.add(files[i].getUser());
397:
398: } else {
399: retrieveSubFolderFTPFiles(files[i].getName(),
400: prefix + "/" + currentPath);
401: }
402: }
403: ftp.cwd("..");
404: } catch (Exception e) {
405: SnapperAdmin
406: .logError(" Problem occured while colecting files in FTP path :"
407: + "ftp://"
408: + this .host
409: + "/"
410: + prefix
411: + "/"
412: + currentPath
413: + " message : "
414: + e.getMessage());
415: return;
416: }
417:
418: }
419:
420: public File getFTPFile(String filePath) throws Exception {
421: String retreiveFilePath = filePath;
422:
423: if (filePath.indexOf(docStorePath) != -1)
424: retreiveFilePath = retreiveFilePath
425: .substring(retreiveFilePath.indexOf(docStorePath)
426: + docStorePath.length());
427: else
428: return null;
429:
430: String rootTempFolder;
431:
432: if (tempFolder != null)
433: rootTempFolder = tempFolder;
434: else
435: rootTempFolder = System.getProperty("user.home");
436:
437: if (rootTempFolder.endsWith("/")
438: || rootTempFolder.endsWith("\\"))
439: rootTempFolder = rootTempFolder
440: + Thread.currentThread().getName() + File.separator
441: + "FTP" + File.separator + docStorePath;
442: else
443: rootTempFolder = rootTempFolder + File.separator
444: + Thread.currentThread().getName() + File.separator
445: + "FTP" + File.separator + docStorePath;
446:
447: File tempFile = new File(rootTempFolder + retreiveFilePath);
448:
449: File destDir = tempFile.getParentFile();
450: if (!destDir.exists() && !destDir.mkdirs())
451: throw new Exception("Couldn't create dir " + destDir);
452: tempFile.createNewFile();
453:
454: FileOutputStream in = null;
455:
456: try {
457: in = new FileOutputStream(tempFile);
458: ftp.retrieveFile(ftp.printWorkingDirectory()
459: + retreiveFilePath, in);
460: } finally {
461: if (in != null) {
462: try {
463: in.close();
464: } catch (Exception e) {
465: }
466:
467: in = null;
468: }
469: }
470:
471: return tempFile;
472: }
473:
474: public boolean connect() {
475: try {
476: ftp = new FTPClient();
477: ftp.connect(this .host);
478: ftp.login(this .login, this .password);
479: ftp.enterLocalActiveMode();
480: ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
481: ftp.cwd(docStorePath);
482: return true;
483: } catch (Exception e) {
484: Log.logException(e);
485: return false;
486: }
487: }
488:
489: public Vector getOriginalFiles() {
490: return originalFiles;
491: }
492:
493: public Vector getTimeStamps() {
494: return timestamps;
495: }
496:
497: public Vector getOwners() {
498: return owners;
499: }
500:
501: public Vector getCreation() {
502: return creationDate;
503: }
504:
505: }
|