001: package org.contineo.web.util;
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: String filename = menu.getMenuRef();
100:
101: // older versions of a document are stored in the same directory as the
102: // current version,
103: // but the filename is the version number without extension, e.g.
104: // "menuid/2.1"
105: String menuref;
106:
107: if (docVerId == null) {
108: menuref = menu.getMenuRef();
109: } else {
110: menuref = docVerId;
111: }
112:
113: // load the file from the file system and output it to the
114: // responseWriter
115: File file = new File(path + "/" + menuref);
116:
117: if (!file.exists()) {
118: throw new FileNotFoundException();
119: }
120:
121: // it seems everything is fine, so we can now start writing to the
122: // response object
123: response.setContentType(mimetype);
124: response.setHeader("Content-Disposition",
125: "attachment; filename=\"" + filename + "\"");
126:
127: InputStream is = new FileInputStream(file);
128: OutputStream os;
129: os = response.getOutputStream();
130:
131: int letter = 0;
132:
133: while ((letter = is.read()) != -1) {
134: os.write(letter);
135: }
136:
137: os.flush();
138: os.close();
139: is.close();
140: }
141:
142: /**
143: * sends the specified document to the response object; the client will
144: * receive it as a download
145: *
146: * @param response the document is written to this object
147: * @param docId Id of the document
148: * @param docVerId name of the version; if null the latest version will
149: * returned
150: */
151: public static void downloadDocument(HttpServletResponse response,
152: String docId, String docVerId)
153: throws FileNotFoundException, IOException {
154: downloadDocument(response, Integer.parseInt(docId), docVerId);
155: }
156: }
|