KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > FileDownloadServlet


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.net;
26
27 import org.radeox.util.logging.Logger;
28 import org.snipsnap.app.Application;
29 import org.snipsnap.config.Configuration;
30 import org.snipsnap.snip.Snip;
31 import org.snipsnap.snip.attachment.Attachment;
32 import org.snipsnap.snip.attachment.storage.AttachmentStorage;
33 import org.snipsnap.container.Components;
34
35 import javax.servlet.RequestDispatcher JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.http.HttpServlet JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.io.BufferedInputStream JavaDoc;
41 import java.io.BufferedOutputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * Servlet for downloading attachments.
48  * @author Matthias L. Jugel
49  * @version $Id: FileDownloadServlet.java 1606 2004-05-17 10:56:18Z leo $
50  */

51 public class FileDownloadServlet extends HttpServlet JavaDoc {
52
53   protected long getLastModified(HttpServletRequest JavaDoc request) {
54     Snip snip = (Snip) request.getAttribute(SNIP);
55     String JavaDoc fileName = (String JavaDoc) request.getAttribute(FILENAME);
56
57     if (snip != null) {
58       Attachment attachment = snip.getAttachments().getAttachment(fileName);
59       // make sure the attachment exists
60
if (attachment != null) {
61         return attachment.getDate().getTime();
62       }
63     }
64     return super.getLastModified(request);
65   }
66
67   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68       throws ServletException JavaDoc, IOException JavaDoc {
69     doPost(request, response);
70   }
71
72   public final static String JavaDoc FILENAME = "filename";
73   public final static String JavaDoc SNIP = "snip";
74
75   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
76       throws ServletException JavaDoc, IOException JavaDoc {
77
78     AttachmentStorage attachmentStorage = (AttachmentStorage) Components.getComponent(AttachmentStorage.class);
79
80     Snip snip = (Snip) request.getAttribute(SNIP);
81     String JavaDoc fileName = (String JavaDoc) request.getAttribute(FILENAME);
82
83     if (snip != null) {
84       Attachment attachment = snip.getAttachments().getAttachment(fileName);
85
86       // make sure the attachment exists
87
if (attachment != null) {
88         response.setContentType(attachment.getContentType());
89         response.setContentLength((int) attachment.getSize());
90         response.setDateHeader("Last-Modified", attachment.getDate().getTime());
91         BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(response.getOutputStream());
92         BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(attachmentStorage.getInputStream(attachment));
93         byte buf[] = new byte[4096];
94         int length = -1;
95         while ((length = in.read(buf)) != -1) {
96           out.write(buf, 0, length);
97         }
98         out.flush();
99         in.close();
100         out.close();
101         return;
102       } else {
103         // legacy: found a default image download
104
Logger.log(Logger.DEBUG, "old style image: " + fileName);
105         String JavaDoc oldStyleFile = "/images/image-" + snip.getName() + "-" + fileName;
106         if (getServletContext().getResource(oldStyleFile) != null) {
107           RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher(oldStyleFile);
108           if (dispatcher != null) {
109             dispatcher.forward(request, response);
110             return;
111           }
112         }
113       }
114     }
115
116     // file does not exist, tell caller
117
throw new ServletException JavaDoc("file does not exist: " + fileName);
118   }
119 }
120
Popular Tags