KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > documan > util > DownloadDocUtil


1 /* License: GNU General Public License (GPL) version 2 from June 1991; but not any newer version!
2  */

3 package org.contineo.actions.documan.util;
4
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 import org.contineo.admin.Menu;
15 import org.contineo.admin.UserDoc;
16 import org.contineo.admin.dao.MenuDAO;
17 import org.contineo.admin.dao.UserDocDAO;
18 import org.contineo.core.config.MimeTypeConfigurator;
19 import org.contineo.core.config.SettingConfigurator;
20 import org.contineo.documan.Document;
21 import org.contineo.documan.dao.DocumentDAO;
22
23 /**
24  * some helper utilities to download a document
25  * but also to add the document to the recent files of the user
26  * @author Sebastian Stein
27  */

28 public class DownloadDocUtil {
29     /**
30      * adds the given document to the recent files entry of the user
31      * @param userName the username of the user accessing the file
32      * @param menuId id of the menu the user accessed
33      */

34     public static void addToRecentFiles(String JavaDoc userName, int menuId) {
35             UserDoc userdoc = new UserDoc();
36             userdoc.setMenuId(menuId);
37             userdoc.setUserName(userName);
38             UserDocDAO uddao = new UserDocDAO();
39             uddao.store(userdoc);
40     }
41     
42     /**
43      * extracts the mimetype of the document
44      */

45     public static String JavaDoc getMimeType(Menu docId) {
46         if (docId == null)
47             return null;
48         String JavaDoc extension = docId.getMenuRef().substring(docId.getMenuRef().lastIndexOf(".") + 1);
49         MimeTypeConfigurator mtc = new MimeTypeConfigurator();
50         String JavaDoc mimetype = mtc.getMimeApp(extension);
51         if (mimetype == null || mimetype.equals(""))
52             mimetype = "application/octet-stream";
53         return mimetype;
54     }
55     
56     /**
57      * sends the specified document to the response object; the client will receive it as a download
58      * @param response the document is written to this object
59      * @param docId Id of the document
60      * @param docVerId name of the version; if null the latest version will returned
61      */

62     public static void downloadDocument(HttpServletResponse JavaDoc response, int docId, String JavaDoc docVerId)
63                        throws FileNotFoundException JavaDoc, IOException JavaDoc {
64         // get menu and document
65
MenuDAO mdao = new MenuDAO();
66         Menu menu = mdao.findByPrimaryKey(docId);
67         DocumentDAO ddao = new DocumentDAO();
68         Document doc = ddao.findByMenuId(docId);
69         if (menu == null || doc == null) {
70             throw new FileNotFoundException JavaDoc();
71         }
72         
73         // get the mimetype
74
String JavaDoc mimetype = DownloadDocUtil.getMimeType(menu);
75         
76         // get path correct file name
77
SettingConfigurator settings = new SettingConfigurator();
78         String JavaDoc path = settings.getValue("docdir") + menu.getMenuPath() + "/" + menu.getMenuId();
79         
80         // older versions of a document are stored in the same directory as the current version,
81
// but the filename is the version number without extension, e.g. "menuid/2.1"
82
String JavaDoc menuref;
83         if (docVerId == null) {
84             menuref = menu.getMenuRef();
85         } else {
86             menuref = docVerId;
87         }
88         
89         // load the file from the file system and output it to the responseWriter
90
File JavaDoc file = new File JavaDoc(path + "/" + menuref);
91         if (!file.exists()) {
92             throw new FileNotFoundException JavaDoc();
93         }
94         
95         // it seems everything is fine, so we can now start writing to the response object
96
response.setContentType(mimetype);
97         response.setHeader("Content-disposition","attachment;filename=" + doc.getDocName().replaceAll(" ", "_") + "." + doc.getDocType());
98         InputStream JavaDoc is = new FileInputStream JavaDoc(file);
99         OutputStream JavaDoc os;
100         os = response.getOutputStream();
101         int letter = 0;
102         while ((letter = is.read()) != -1) {
103             os.write(letter);
104         }
105         os.flush();
106         os.close();
107         is.close();
108     }
109     
110     /**
111      * sends the specified document to the response object; the client will receive it as a download
112      * @param response the document is written to this object
113      * @param docId Id of the document
114      * @param docVerId name of the version; if null the latest version will returned
115      */

116     public static void downloadDocument(HttpServletResponse JavaDoc response, String JavaDoc docId, String JavaDoc docVerId)
117                        throws FileNotFoundException JavaDoc, IOException JavaDoc {
118         downloadDocument(response, Integer.parseInt(docId), docVerId);
119     }
120 }
Popular Tags