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