Source Code Cross Referenced for DownloadDocUtil.java in  » Content-Management-System » contineo » org » contineo » web » document » 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 » Content Management System » contineo » org.contineo.web.document 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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