001: package org.enhydra.snapperPreviewer.business;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005:
006: import org.apache.commons.httpclient.HttpURL;
007: import org.apache.commons.net.ftp.FTPClient;
008: import org.apache.webdav.lib.WebdavResource;
009: import org.enhydra.snapperPreviewer.spec.Download;
010: import org.enhydra.snapperPreviewer.spec.Path;
011: import org.enhydra.snapperPreviewer.spec.Site;
012: import org.enhydra.snapperPreviewer.spec.SiteList;
013: import org.enhydra.snapperPreviewer.spec.SiteListFactory;
014:
015: /**
016: * @author Milin Radivoj
017: *
018: */
019:
020: public class DownloadImpl implements Download {
021:
022: public File downloadFtpFile(String url, String type, String siteName)
023: throws Exception {
024:
025: if (url == null)
026: throw new Exception(
027: " URL of the file is null, could not download FTP file ");
028:
029: String user = null;
030: String pass = null;
031: String host = null;
032: String path = null;
033:
034: SiteList sl = SiteListFactory
035: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
036: Site[] list = sl.getList();
037:
038: if (list != null) {
039: for (int i = 0; i < list.length; i++) {
040: Site currentSite = list[i];
041: String name = currentSite.getNAME();
042:
043: Path[] pathArray = list[i].getPathList();
044: if (pathArray != null) {
045: for (int j = 0; j < pathArray.length; j++) {
046: if (pathArray[j] == null)
047: continue;
048:
049: host = pathArray[j].getHost();
050: path = pathArray[j].getRoot();
051:
052: if (url.indexOf(host + path) != -1) {
053: user = pathArray[j].getUser();
054: pass = pathArray[j].getPass();
055: break;
056: }
057: }
058: }
059: }
060: }
061:
062: if (user == null || pass == null || host == null)
063: throw new Exception(
064: " Parameters for conecting to FTP mising, could not download FTP file ");
065:
066: if (path == null)
067: path = "";
068:
069: String retreiveFilePath = url;
070:
071: if (url.indexOf(host + path) != -1)
072: retreiveFilePath = retreiveFilePath
073: .substring(retreiveFilePath.indexOf(host)
074: + host.length());
075: else
076: throw new Exception(
077: " Could not download FTP file, mapping failed : "
078: + url);
079:
080: if (host.endsWith("/"))
081: host = host.substring(0, host.length() - 1);
082:
083: if (host.startsWith("ftp://"))
084: host = host.substring("ftp://".length());
085:
086: String nameOfTempFile = (new Long(System.currentTimeMillis()))
087: .toString()
088: + "." + type;
089: if (url.lastIndexOf("/") != -1)
090: nameOfTempFile = url.substring(url.lastIndexOf("/") + 1);
091:
092: FTPClient ftp = new FTPClient();
093: ftp.connect(host);
094: ftp.login(user, pass);
095: ftp.enterLocalActiveMode();
096: ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
097:
098: String tempFilePath = System.getProperty("user.home");
099:
100: if (!tempFilePath.endsWith(File.separator))
101: tempFilePath = tempFilePath + File.separator;
102:
103: tempFilePath = tempFilePath + ".snapperTemp" + File.separator
104: + "download" + File.separator + nameOfTempFile;
105:
106: File tempFile = new File(tempFilePath);
107: File destDir = tempFile.getParentFile();
108:
109: if (!destDir.exists() && !destDir.mkdirs()) {
110: ftp.disconnect();
111: throw new Exception("Couldn't create dir "
112: + retreiveFilePath
113: + ", could not download FTP file");
114: }
115: tempFile.createNewFile();
116:
117: FileOutputStream in = null;
118: try {
119: in = new FileOutputStream(tempFile);
120: ftp.retrieveFile(ftp.printWorkingDirectory() + "/"
121: + retreiveFilePath, in);
122: ftp.disconnect();
123: } finally {
124: if (in != null) {
125: try {
126: in.close();
127: } catch (Exception e) {
128: }
129: in = null;
130: }
131: }
132: return tempFile;
133:
134: }
135:
136: public File downloadWebDavFile(String url, String type,
137: String siteName) throws Exception {
138:
139: if (url == null)
140: throw new Exception(
141: " URL of the file is null, could not download WebDAV file ");
142:
143: HttpURL hrl = new HttpURL(url);
144:
145: if (hrl == null)
146: throw new Exception(
147: "URL of the file is null, could not download WebDAV file ");
148:
149: SiteList sl = SiteListFactory
150: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
151: Site[] list = sl.getList();
152:
153: String user = null;
154: String pass = null;
155:
156: if (list != null) {
157: for (int i = 0; i < list.length; i++) {
158: Site currentSite = list[i];
159: String name = currentSite.getNAME();
160:
161: Path[] pathArray = list[i].getPathList();
162: if (pathArray != null) {
163: for (int j = 0; j < pathArray.length; j++) {
164: if (pathArray[j] == null)
165: continue;
166:
167: String hostDB = pathArray[j].getHost();
168: String rootDB = pathArray[j].getRoot();
169:
170: hostDB = hostDB + rootDB;
171:
172: if (url.indexOf(hostDB) != -1) {
173: user = pathArray[j].getUser();
174: pass = pathArray[j].getPass();
175: break;
176: }
177: }
178: }
179: }
180: }
181:
182: if (user != null && pass != null)
183: hrl.setUserinfo(user, pass);
184: else
185: throw new Exception(
186: " Autentification parameters mising, could not download WebDAV file ");
187:
188: WebdavResource wdr = new WebdavResource(hrl);
189:
190: if (!wdr.exists()) {
191: wdr.close();
192: throw new Exception(" URL : " + url
193: + " not exist , could not download WebDAV file");
194: }
195:
196: String tempFilePath = System.getProperty("user.home");
197:
198: if (!tempFilePath.endsWith(File.separator))
199: tempFilePath = tempFilePath + File.separator;
200:
201: String nameOfTempFile = (new Long(System.currentTimeMillis()))
202: .toString()
203: + "." + type;
204: if (url.lastIndexOf("/") != -1)
205: nameOfTempFile = url.substring(url.lastIndexOf("/") + 1);
206:
207: tempFilePath = tempFilePath + ".snapperTemp" + File.separator
208: + "download" + File.separator + nameOfTempFile;
209:
210: File tempFile = new File(tempFilePath);
211:
212: File destDir = tempFile.getParentFile();
213:
214: if (!destDir.exists() && !destDir.mkdirs()) {
215: wdr.close();
216: throw new Exception("Couldn't create dir " + destDir
217: + ", could not download WebDAV file");
218: }
219:
220: tempFile.createNewFile();
221:
222: if (!wdr.getMethod(tempFile)) {
223: wdr.close();
224: throw new Exception(" Could not save file :" + url
225: + ", could not download WebDAV file");
226: }
227:
228: wdr.close();
229:
230: return tempFile;
231:
232: }
233: }
|