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: //
|