Source Code Cross Referenced for ViewAttachmentServlet.java in  » Wiki-Engine » VeryQuickWiki » vqwiki » servlets » 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 » Wiki Engine » VeryQuickWiki » vqwiki.servlets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Delivers an attachment to the browser. Sends as a binary stream along with a content-type
003:         * determined by the mime.types file in the classpath.
004:         *
005:         * @author garethc
006:         * 25/10/2002 14:18:32
007:         */package vqwiki.servlets;
008:
009:        import org.apache.log4j.Logger;
010:        import vqwiki.Environment;
011:
012:        import javax.servlet.ServletException;
013:        import javax.servlet.ServletOutputStream;
014:        import javax.servlet.http.HttpServletRequest;
015:        import javax.servlet.http.HttpServletResponse;
016:        import java.io.*;
017:        import java.util.HashMap;
018:        import java.util.StringTokenizer;
019:
020:        public class ViewAttachmentServlet extends VQWikiServlet {
021:
022:            private static final Logger logger = Logger
023:                    .getLogger(ViewAttachmentServlet.class);
024:
025:            /**
026:             *
027:             */
028:            protected void doGet(HttpServletRequest request,
029:                    HttpServletResponse response) throws ServletException,
030:                    IOException {
031:                Environment en = Environment.getInstance();
032:                String attachmentName = request.getParameter("attachment");
033:                String virtualWiki = (String) request
034:                        .getAttribute("virtual-wiki");
035:                File uploadPath = en.uploadPath(virtualWiki, attachmentName);
036:                response.reset();
037:                // attachments can be "inline" or "attachment"
038:                response.setHeader("Content-Disposition", en
039:                        .getStringSetting(Environment.PROPERTY_ATTACHMENT_TYPE)
040:                        + ";filename=" + attachmentName + ";");
041:                if (attachmentName.indexOf('.') >= 0) {
042:                    if (attachmentName.indexOf('.') < attachmentName.length() - 1) {
043:                        String extension = attachmentName
044:                                .substring(attachmentName.lastIndexOf('.') + 1);
045:                        logger.debug("Extension: " + extension);
046:                        try {
047:                            String mimetype = (String) getMimeByExtension()
048:                                    .get(extension.toLowerCase());
049:                            logger.debug("MIME: " + mimetype);
050:                            if (mimetype != null) {
051:                                logger.debug("Setting content type to: "
052:                                        + mimetype);
053:                                response.setContentType(mimetype);
054:                            }
055:                        } catch (Exception e) {
056:                            error(request, response, new WikiServletException(e
057:                                    .getMessage()));
058:                            return;
059:                        }
060:                    }
061:                }
062:                FileInputStream in = new FileInputStream(uploadPath);
063:                ServletOutputStream out = response.getOutputStream();
064:                int bytesRead = 0;
065:                byte byteArray[] = new byte[4096];
066:                // Read in bytes through file stream, and write out through servlet stream
067:                while ((bytesRead = in.read(byteArray)) != -1) {
068:                    out.write(byteArray, 0, bytesRead);
069:                }
070:                in.close();
071:                out.flush();
072:                out.close();
073:            }
074:
075:            /**
076:             *
077:             */
078:            protected HashMap getMimeByExtension() throws Exception {
079:                HashMap map = new HashMap();
080:                InputStream resourceAsStream = Environment.getInstance()
081:                        .getResourceAsStream("/mime.types");
082:                if (resourceAsStream == null) {
083:                    logger.warn("couldn't find the MIME types file mime.types");
084:                    return map;
085:                }
086:                BufferedReader in = new BufferedReader(new InputStreamReader(
087:                        resourceAsStream));
088:                while (true) {
089:                    String line = in.readLine();
090:                    if (line == null)
091:                        break;
092:                    if (!line.startsWith("#") && !line.trim().equals("")) {
093:                        StringTokenizer tokens = new StringTokenizer(line);
094:                        if (tokens.hasMoreTokens()) {
095:                            String type = tokens.nextToken();
096:                            while (tokens.hasMoreTokens()) {
097:                                map.put(tokens.nextToken(), type);
098:                            }
099:                        }
100:                    }
101:                }
102:                return map;
103:            }
104:        }
105:
106:        // $Log$
107:        // Revision 1.9  2006/04/23 06:36:56  wrh2
108:        // Coding style updates (VQW-73).
109:        //
110:        // Revision 1.8  2004/04/27 04:23:30  coljac
111:        // Fixed getting extension from file attachment.(Bug 926148)
112:        //
113:        // Revision 1.7  2003/10/05 05:07:32  garethc
114:        // fixes and admin file encoding option + merge with contributions
115:        //
116:        // Revision 1.6  2003/04/15 23:11:04  garethc
117:        // lucene fixes
118:        //
119:        // Revision 1.5  2003/04/15 08:33:12  mrgadget4711
120:        // ADD: Search using Lucene
121:        // ADD: RSS feed
122:        //
123:        // Revision 1.4  2003/04/09 20:44:29  garethc
124:        // package org
125:        //
126:        // Revision 1.3  2003/03/31 20:35:27  garethc
127:        // wikiname.ignore
128:        //
129:        // Revision 1.2  2003/03/11 20:21:16  garethc
130:        // fixes and 2.5 enhancements
131:        //
132:        // Revision 1.1  2003/02/02 19:41:25  garethc
133:        // servlets
134:        //
135:        // Revision 1.5  2003/01/07 03:11:53  garethc
136:        // beginning of big cleanup, taglibs etc
137:        //
138:        // Revision 1.4  2002/12/02 19:26:51  garethc
139:        // fixes
140:        //
141:        // Revision 1.3  2002/11/22 02:53:51  garethc
142:        // 2.3.5
143:        //
144:        // Revision 1.2  2002/11/07 21:47:45  garethc
145:        // part way through 2 part lex
146:        //
147:        // Revision 1.1  2002/11/01 03:12:43  garethc
148:        // starting work on new two pass lexer
149:        //
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.