1 package org.enhydra.snapper.business; 2 3 9 10 import org.apache.commons.net.ftp.FTPClient; 11 import org.enhydra.dods.DODS; 12 import org.enhydra.snapper.Log; 13 import org.enhydra.snapper.spec.*; 14 15 import com.lutris.appserver.server.sql.DBTransaction; 16 17 import java.io.File ; 18 import java.io.FileOutputStream ; 19 20 24 25 public class DownloadImpl implements Download 26 { 27 28 public File downloadFtpFile(String id, String type){ 29 try{ 30 FTPClient ftp=new FTPClient(); 31 String temp = id.replaceFirst("ftp://",""); 32 temp = temp.replaceAll("\\\\","/"); 33 34 35 String host = temp.substring(0,temp.indexOf("/")); 36 String filePath = temp.substring(temp.indexOf("/")); 37 38 String user = new String (); 39 String pass = new String (); 40 41 try{ 42 DBTransaction dbTransaction = DODS.getDatabaseManager().createTransaction(); 43 44 SiteList sl = SiteListFactory.getSiteList("org.enhydra.snapper.business.SiteListImpl"); 45 46 Site[] list = sl.getList(dbTransaction); 47 48 49 for(int i=0;i<list.length;i++) 50 { 51 Site currentSite = list[i]; 52 53 String idd = currentSite.getID(); 54 String name = currentSite.getName(); 55 56 Path pt; 57 Path[] pathArray; 58 59 pt = PathFactory.getPath("org.enhydra.snapper.business.PathImpl"); 60 61 pathArray = pt.getListForID(dbTransaction, idd); 62 63 64 for(int j=0;j<pathArray.length;j++) 65 { 66 String hostDB = pathArray[j].getHost(); 67 String rootDB = pathArray[j].getRoot(); 68 String r = filePath.replaceFirst("/",""); 69 70 if(hostDB.equals(host)&&r.startsWith(rootDB)) 71 { 72 73 user = pathArray[j].getUser(); 74 pass = pathArray[j].getPass(); 75 76 break; 77 } 78 79 } 80 } 81 dbTransaction.release(); 82 83 }catch (Exception e){ 84 Log.logException(e); 85 return null; 86 } 87 ftp = new FTPClient(); 88 ftp.connect(host); 89 ftp.login(user, pass); 90 ftp.enterLocalActiveMode(); 91 File retVal = new File ("temp."+type); 92 FileOutputStream in = new FileOutputStream (retVal); 93 ftp.retrieveFile(ftp.printWorkingDirectory() + filePath, in); 94 in.close(); 95 in=null; 96 ftp.disconnect(); 97 return retVal; 98 } catch (Exception e) { 99 Log.logException(e); 100 return null; 101 } 102 103 104 105 } 106 107 108 } | Popular Tags |