Source Code Cross Referenced for DownloadImpl.java in  » Search-Engine » snapper » org » enhydra » snapperAdmin » business » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » snapper » org.enhydra.snapperAdmin.business 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.