001: package org.enhydra.snapperPreviewer.presentation;
002:
003: import java.io.File;
004:
005: import org.enhydra.xml.xmlc.XMLObject;
006: import java.util.Vector;
007:
008: import org.enhydra.snapper.SnapperManager;
009: import org.enhydra.snapperPreviewer.presentation.FileDownloadBO;
010: import org.enhydra.snapperPreviewer.spec.*;
011: import org.enhydra.snapperPreviewer.Previewer;
012:
013: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
014: import com.lutris.appserver.server.httpPresentation.ServerPageRedirectException;
015: import com.lutris.appserver.server.sql.DBTransaction;
016:
017: public class DM_DownloadPO extends BasePO {
018:
019: protected XMLObject getDOM() throws Exception {
020:
021: boolean isContainer = false;
022: FileContainerManager manager = null;
023: try {
024: String path = comms.request.getParameter("url");
025: String type = comms.request.getParameter("type");
026: String siteName = comms.request.getParameter("id");
027:
028: if (siteName == null) {
029: //Previewer.logInfo(" Missing site name parameter , granted download ");
030: try {
031: downloadFile(Previewer.getXslDirectory() + "/"
032: + "fileError.jpg", "image/jpeg");
033: } catch (Exception e) {
034: Previewer.logError(e.getMessage());
035: }
036: return null;
037: }
038:
039: if (path == null) {
040: Previewer
041: .logInfo(" Missing url parameter , granted download ");
042: try {
043: downloadFile(Previewer.getXslDirectory() + "/"
044: + "fileError.jpg", "image/jpeg");
045: } catch (Exception e) {
046: Previewer.logError(e.getMessage());
047: }
048: return null;
049: }
050:
051: if (siteName.equals("conv")) {
052:
053: if (path.startsWith(Previewer.pathOfConvertedFiles())
054: || path.startsWith(SnapperManager.getInstance()
055: .getTempDir()
056: + File.separator + "conversion")) {
057: try {
058: downloadFile(path, type);
059: } catch (Exception e) {
060: Previewer.logError(e.getMessage());
061: }
062:
063: }
064:
065: return null;
066:
067: }
068:
069: if (!checkIsGrantedDownload(siteName)) {
070: if (Previewer.isRelitive()) {
071:
072: if (siteName == null) {
073: try {
074: downloadFile(Previewer.getXslDirectory()
075: + "/" + "fileError.jpg",
076: "image/jpeg");
077: } catch (Exception e) {
078: Previewer.logError(e.getMessage());
079: }
080: return null;
081: }
082:
083: try {
084:
085: if (path.indexOf("|") != -1)
086: path = path.substring(0, path.indexOf("|"));
087:
088: SiteList sl = SiteListFactory
089: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
090:
091: Site site = sl.findSiteByName(siteName);
092:
093: Path[] pathList = site.getPathList();
094:
095: if (pathList != null && pathList.length == 1) {
096: String mapRoot = pathList[0]
097: .getMappingRoot();
098:
099: mapRoot = mapRoot + "/" + path;
100:
101: throw new ClientPageRedirectException(
102: mapRoot);
103:
104: }
105:
106: } catch (Exception e) {
107: Previewer.logError(e.getMessage());
108: }
109:
110: } else {
111: try {
112: downloadFile(Previewer.getXslDirectory() + "/"
113: + "fileError.jpg", "image/jpeg");
114: } catch (Exception e) {
115: Previewer.logError(e.getMessage());
116: }
117: return null;
118: }
119:
120: }
121:
122: String fileToShow = null;
123:
124: if (path.indexOf("|") != -1) {
125: manager = FileContainerManagerFactory
126: .getFileContainerManager("org.enhydra.snapperPreviewer.business.FileContainerManagerImpl");
127: try {
128: File containerFile = manager.getContainerFile(path,
129: siteName);
130: fileToShow = containerFile.getAbsolutePath();
131: } catch (Exception e) {
132: fileToShow = null;
133: }
134: isContainer = true;
135: } else {
136: fileToShow = path;
137: }
138:
139: if (fileToShow != null) {
140: int indexOfExtension = fileToShow.lastIndexOf(".");
141:
142: String typeOfFile = type;
143:
144: if (indexOfExtension != -1) {
145: typeOfFile = fileToShow.substring(
146: indexOfExtension + 1, fileToShow.length());
147: } else {
148: Previewer
149: .logInfo(" Missing extension of file name ");
150: try {
151: downloadFile(Previewer.getXslDirectory() + "/"
152: + "fileError.jpg", "image/jpeg");
153: } catch (Exception e) {
154: Previewer.logError(e.getMessage());
155: }
156: if (isContainer && manager != null)
157: manager.deleteAllTempFiles();
158: return null;
159: }
160:
161: if (fileToShow.startsWith("ftp")) {
162: File tempFile = downloadFtpFile(fileToShow,
163: typeOfFile, siteName);
164:
165: if (tempFile != null) {
166: downloadFile(tempFile.getAbsolutePath(),
167: typeOfFile);
168: tempFile.delete();
169: return null;
170: } else {
171: try {
172: downloadFile(Previewer.getXslDirectory()
173: + "/" + "fileError.jpg",
174: "image/jpg");
175: } catch (Exception e) {
176: Previewer.logError(e.getMessage());
177: }
178: if (isContainer && manager != null)
179: manager.deleteAllTempFiles();
180: return null;
181: }
182: } else if (fileToShow.startsWith("http")) {
183: File tempFile = downloadWebDavFile(fileToShow,
184: typeOfFile, siteName);
185:
186: if (tempFile != null) {
187: downloadFile(tempFile.getAbsolutePath(),
188: typeOfFile);
189: tempFile.delete();
190: return null;
191: } else {
192: try {
193: downloadFile(Previewer.getXslDirectory()
194: + "/" + "fileError.jpg",
195: "image/jpg");
196: } catch (Exception e) {
197: Previewer.logError(e.getMessage());
198: }
199: if (isContainer && manager != null)
200: manager.deleteAllTempFiles();
201: return null;
202: }
203: } else {
204: downloadFile(fileToShow, typeOfFile);
205: }
206: } else {
207: try {
208: downloadFile(Previewer.getXslDirectory() + "/"
209: + "fileError.jpg", "image/jpeg");
210: } catch (Exception e) {
211: Previewer.logError(e.getMessage());
212: }
213: }
214:
215: } catch (Exception e) {
216: Previewer.logError(e.getMessage());
217:
218: try {
219: downloadFile(Previewer.getXslDirectory() + "/"
220: + "fileError.jpg", "image/jpeg");
221: } catch (Exception ex) {
222: Previewer.logError(ex.getMessage());
223: }
224:
225: }
226: if (isContainer && manager != null)
227: manager.deleteAllTempFiles();
228: return null;
229: }
230:
231: protected void downloadFile(String filePath, String type)
232: throws Exception {
233: FileDownloadBO theBO = new FileDownloadBO(new File(filePath),
234: comms);
235:
236: try {
237: theBO.setContentType(manageType(type));
238: theBO.download();
239: } catch (Exception e) {
240: Previewer.logError(e.getMessage());
241: }
242: }
243:
244: protected String manageType(String type) {
245: String result;
246: if (type.equalsIgnoreCase("html"))
247: result = FileDownloadBO.HTML_CONTENT_TYPE;
248: else if (type.equalsIgnoreCase("pdf"))
249: result = FileDownloadBO.PDF_CONTENT_TYPE;
250: else if (type.equalsIgnoreCase("xls"))
251: result = FileDownloadBO.EXCEL_CONTENT_TYPE;
252: else if (type.equalsIgnoreCase("doc"))
253: result = FileDownloadBO.WORD_CONTENT_TYPE;
254: else if (type.equalsIgnoreCase("rtf"))
255: result = FileDownloadBO.RTF_CONTENT_TYPE;
256: else if (type.equalsIgnoreCase("sxw"))
257: result = FileDownloadBO.SXW_CONTENT_TYPE;
258: else if (type.equalsIgnoreCase("stw"))
259: result = FileDownloadBO.STW_CONTENT_TYPE;
260: else if (type.equalsIgnoreCase("sxg"))
261: result = FileDownloadBO.SXG_CONTENT_TYPE;
262: else if (type.equalsIgnoreCase("sxc"))
263: result = FileDownloadBO.SXC_CONTENT_TYPE;
264: else if (type.equalsIgnoreCase("stc"))
265: result = FileDownloadBO.STC_CONTENT_TYPE;
266: else if (type.equalsIgnoreCase("sxi"))
267: result = FileDownloadBO.SXI_CONTENT_TYPE;
268: else if (type.equalsIgnoreCase("sti"))
269: result = FileDownloadBO.STI_CONTENT_TYPE;
270: else if (type.equalsIgnoreCase("sxd"))
271: result = FileDownloadBO.SXD_CONTENT_TYPE;
272: else if (type.equalsIgnoreCase("std"))
273: result = FileDownloadBO.STD_CONTENT_TYPE;
274: else if (type.equalsIgnoreCase("sxm"))
275: result = FileDownloadBO.SXM_CONTENT_TYPE;
276: else if (type.equalsIgnoreCase("msg"))
277: result = FileDownloadBO.EML_CONTENT_TYPE;
278: else if (type.equalsIgnoreCase("ppt"))
279: result = FileDownloadBO.PPT_CONTENT_TYPE;
280: else if (type.equalsIgnoreCase("pps"))
281: result = FileDownloadBO.PPS_CONTENT_TYPE;
282: else if (type.equalsIgnoreCase("zip"))
283: result = FileDownloadBO.ZIP_CONTENT_TYPE;
284: else if (type.equalsIgnoreCase("eml"))
285: result = FileDownloadBO.EML_CONTENT_TYPE;
286: else if (type.equalsIgnoreCase("txt"))
287: result = "text/plain";
288: else if (type.equalsIgnoreCase("ico"))
289: result = "image/x-icon";
290: else if (type.equalsIgnoreCase("xlc"))
291: result = FileDownloadBO.EXCEL_CONTENT_TYPE;
292: else if (type.equalsIgnoreCase("xlt"))
293: result = FileDownloadBO.EXCEL_CONTENT_TYPE;
294: else if (type.equalsIgnoreCase("xla"))
295: result = FileDownloadBO.EXCEL_CONTENT_TYPE;
296: else if (type.equalsIgnoreCase("text"))
297: result = "text/plain";
298: else if (type.equalsIgnoreCase("bib"))
299: result = "text/plain";
300: else if (type.equalsIgnoreCase("err"))
301: result = "text/plain";
302: else if (type.equalsIgnoreCase("faq"))
303: result = "text/plain";
304: else if (type.equalsIgnoreCase("log"))
305: result = "text/plain";
306: else if (type.equalsIgnoreCase("csv"))
307: result = "text/plain";
308: else if (type.equalsIgnoreCase("sql"))
309: result = "text/plain";
310: else if (type.equalsIgnoreCase("xml"))
311: result = "application/xml";
312: else if (type.equalsIgnoreCase("xsl"))
313: result = "application/xml";
314: else if (type.equalsIgnoreCase("xslt"))
315: result = "application/xml";
316: else if (type.equalsIgnoreCase("js"))
317: result = "text/plain";
318: else if (type.equalsIgnoreCase("jsp"))
319: result = "text/plain";
320: else if (type.equalsIgnoreCase("jspx"))
321: result = "text/plain";
322: else if (type.equalsIgnoreCase("css"))
323: result = "text/plain";
324: else if (type.equalsIgnoreCase("php"))
325: result = "text/plain";
326: else if (type.equalsIgnoreCase("php3"))
327: result = "text/plain";
328: else if (type.equalsIgnoreCase("php4"))
329: result = "text/plain";
330: else if (type.equalsIgnoreCase("php5"))
331: result = "text/plain";
332: else if (type.equalsIgnoreCase("asp"))
333: result = "text/plain";
334: else if (type.equalsIgnoreCase("aspx"))
335: result = "text/plain";
336: else if (type.equalsIgnoreCase("cfm"))
337: result = "text/plain";
338: else if (type.equalsIgnoreCase("htx"))
339: result = "text/plain";
340: else if (type.equalsIgnoreCase("java"))
341: result = "text/plain";
342: else if (type.equalsIgnoreCase("properties"))
343: result = "text/plain";
344: else if (type.equalsIgnoreCase("c"))
345: result = "text/plain";
346: else if (type.equalsIgnoreCase("cpp"))
347: result = "text/plain";
348: else if (type.equalsIgnoreCase("h"))
349: result = "text/plain";
350: else if (type.equalsIgnoreCase("cgi"))
351: result = "text/plain";
352: else if (type.equalsIgnoreCase("dtd"))
353: result = "text/plain";
354: else if (type.equalsIgnoreCase("pl"))
355: result = "text/plain";
356: else if (type.equalsIgnoreCase("rar"))
357: result = FileDownloadBO.ZIP_CONTENT_TYPE;
358: else if (type.equalsIgnoreCase("gz"))
359: result = FileDownloadBO.ZIP_CONTENT_TYPE;
360: else if (type.equalsIgnoreCase("bz2"))
361: result = FileDownloadBO.ZIP_CONTENT_TYPE;
362: else if (type.equalsIgnoreCase("pst"))
363: result = FileDownloadBO.EML_CONTENT_TYPE;
364: else if (type.equalsIgnoreCase("tiff"))
365: result = "image/tiff";
366: else if (type.equalsIgnoreCase("tif"))
367: result = "image/tiff";
368: else if (type.equalsIgnoreCase("g3f"))
369: result = "image/g3fax";
370: else if (type.equalsIgnoreCase("g3"))
371: result = "image/g3fax";
372: else
373: result = "";
374: return result;
375:
376: }
377:
378: public File downloadFtpFile(String id, String type, String siteName) {
379: try {
380: Download dw = DownloadFactory
381: .getDownload("org.enhydra.snapperPreviewer.business.DownloadImpl");
382: File retVal = dw.downloadFtpFile(id, type, siteName);
383: if (retVal != null) {
384: return retVal;
385: } else {
386: Previewer.logError(" Error geting ftp file ");
387: return null;
388: }
389: } catch (Exception e) {
390: Previewer.logError(" Error geting ftp file ");
391: Previewer.logError(e.getMessage());
392: return null;
393: }
394: }
395:
396: public File downloadWebDavFile(String id, String type,
397: String siteName) {
398: try {
399: Download dw = DownloadFactory
400: .getDownload("org.enhydra.snapperPreviewer.business.DownloadImpl");
401: File tempFile = dw.downloadWebDavFile(id, type, siteName);
402:
403: if (tempFile != null) {
404: return tempFile;
405: } else {
406: Previewer.logError(" Error geting WebDAV file ");
407: return null;
408: }
409: } catch (Exception e) {
410: Previewer.logError(" Error geting WebDAV file ");
411: Previewer.logError(e.getMessage());
412: return null;
413: }
414: }
415:
416: public boolean checkIsGrantedDownload(String siteName) {
417: if (siteName == null)
418: return false;
419: if (siteName.equals(""))
420: return true;
421: try {
422: SiteList sl = SiteListFactory
423: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
424:
425: Site site = sl.findSiteByName(siteName);
426:
427: if (site != null)
428: return site.getDOWNLOAD();
429: else
430: return false;
431:
432: } catch (Exception e) {
433:
434: return false;
435: }
436:
437: }
438:
439: }
|