001: package org.contineo.web.document;
002:
003: import org.contineo.core.document.Document;
004: import org.contineo.core.document.dao.DocumentDAO;
005: import org.contineo.core.security.Menu;
006: import org.contineo.core.security.UserDoc;
007: import org.contineo.core.security.dao.MenuDAO;
008: import org.contineo.core.security.dao.UserDocDAO;
009:
010: import org.contineo.util.Context;
011: import org.contineo.util.config.MimeTypeConfig;
012: import org.contineo.util.config.SettingsConfig;
013:
014: import java.io.File;
015: import java.io.FileInputStream;
016: import java.io.FileNotFoundException;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.io.OutputStream;
020:
021: import javax.servlet.http.HttpServletResponse;
022:
023: /**
024: * some helper utilities to download a document but also to add the document to
025: * the recent files of the user
026: *
027: * @author Sebastian Stein
028: */
029: public class DownloadDocUtil {
030: /**
031: * adds the given document to the recent files entry of the user
032: *
033: * @param userName the username of the user accessing the file
034: * @param menuId id of the menu the user accessed
035: */
036: public static void addToRecentFiles(String userName, int menuId) {
037: UserDoc userdoc = new UserDoc();
038: userdoc.setMenuId(menuId);
039: userdoc.setUserName(userName);
040:
041: UserDocDAO uddao = (UserDocDAO) Context.getInstance().getBean(
042: UserDocDAO.class);
043: uddao.store(userdoc);
044: }
045:
046: /**
047: * extracts the mimetype of the document
048: */
049: public static String getMimeType(Menu docId) {
050: if (docId == null) {
051: return null;
052: }
053:
054: String extension = docId.getMenuRef().substring(
055: docId.getMenuRef().lastIndexOf(".") + 1);
056: MimeTypeConfig mtc = (MimeTypeConfig) Context.getInstance()
057: .getBean(MimeTypeConfig.class);
058: String mimetype = mtc.getMimeApp(extension);
059:
060: if ((mimetype == null) || mimetype.equals("")) {
061: mimetype = "application/octet-stream";
062: }
063:
064: return mimetype;
065: }
066:
067: /**
068: * sends the specified document to the response object; the client will
069: * receive it as a download
070: *
071: * @param response the document is written to this object
072: * @param docId Id of the document
073: * @param docVerId name of the version; if null the latest version will
074: * returned
075: */
076: public static void downloadDocument(HttpServletResponse response,
077: int docId, String docVerId) throws FileNotFoundException,
078: IOException {
079: // get menu and document
080: MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
081: MenuDAO.class);
082: Menu menu = mdao.findByPrimaryKey(docId);
083: DocumentDAO ddao = (DocumentDAO) Context.getInstance().getBean(
084: DocumentDAO.class);
085: Document doc = ddao.findByMenuId(docId);
086:
087: if ((menu == null) || (doc == null)) {
088: throw new FileNotFoundException();
089: }
090:
091: // get the mimetype
092: String mimetype = DownloadDocUtil.getMimeType(menu);
093:
094: // get path correct file name
095: SettingsConfig settings = (SettingsConfig) Context
096: .getInstance().getBean(SettingsConfig.class);
097: String path = settings.getValue("docdir") + menu.getMenuPath()
098: + "/" + menu.getMenuId();
099:
100: // older versions of a document are stored in the same directory as the
101: // current version,
102: // but the filename is the version number without extension, e.g.
103: // "menuid/2.1"
104: String menuref;
105:
106: if (docVerId == null) {
107: menuref = menu.getMenuRef();
108: } else {
109: menuref = docVerId;
110: }
111:
112: // load the file from the file system and output it to the
113: // responseWriter
114: File file = new File(path + "/" + menuref);
115:
116: if (!file.exists()) {
117: throw new FileNotFoundException(file.getPath());
118: }
119:
120: // it seems everything is fine, so we can now start writing to the
121: // response object
122: response.setContentType(mimetype);
123: response.setHeader("Content-Disposition",
124: "attachment; filename=\"" + menuref + "\"");
125:
126: // Headers required by Internet Explorer
127: response.setHeader("Pragma", "public");
128: response.setHeader("Cache-Control",
129: "must-revalidate, post-check=0,pre-check=0");
130: response.setHeader("Expires", "0");
131:
132: InputStream is = new FileInputStream(file);
133: OutputStream os;
134: os = response.getOutputStream();
135:
136: int letter = 0;
137:
138: try {
139: while ((letter = is.read()) != -1) {
140: os.write(letter);
141: }
142: } finally {
143: os.flush();
144: os.close();
145: is.close();
146: }
147: }
148:
149: /**
150: * sends the specified document to the response object; the client will
151: * receive it as a download
152: *
153: * @param response the document is written to this object
154: * @param docId Id of the document
155: * @param docVerId name of the version; if null the latest version will
156: * returned
157: */
158: public static void downloadDocument(HttpServletResponse response,
159: String docId, String docVerId)
160: throws FileNotFoundException, IOException {
161: downloadDocument(response, Integer.parseInt(docId), docVerId);
162: }
163: }
|