1 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 ; 36 import javax.servlet.ServletException ; 37 import javax.servlet.http.HttpServlet ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import java.io.BufferedInputStream ; 41 import java.io.BufferedOutputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.IOException ; 45 46 51 public class FileDownloadServlet extends HttpServlet { 52 53 protected long getLastModified(HttpServletRequest request) { 54 Snip snip = (Snip) request.getAttribute(SNIP); 55 String fileName = (String ) request.getAttribute(FILENAME); 56 57 if (snip != null) { 58 Attachment attachment = snip.getAttachments().getAttachment(fileName); 59 if (attachment != null) { 61 return attachment.getDate().getTime(); 62 } 63 } 64 return super.getLastModified(request); 65 } 66 67 public void doGet(HttpServletRequest request, HttpServletResponse response) 68 throws ServletException , IOException { 69 doPost(request, response); 70 } 71 72 public final static String FILENAME = "filename"; 73 public final static String SNIP = "snip"; 74 75 public void doPost(HttpServletRequest request, HttpServletResponse response) 76 throws ServletException , IOException { 77 78 AttachmentStorage attachmentStorage = (AttachmentStorage) Components.getComponent(AttachmentStorage.class); 79 80 Snip snip = (Snip) request.getAttribute(SNIP); 81 String fileName = (String ) request.getAttribute(FILENAME); 82 83 if (snip != null) { 84 Attachment attachment = snip.getAttachments().getAttachment(fileName); 85 86 if (attachment != null) { 88 response.setContentType(attachment.getContentType()); 89 response.setContentLength((int) attachment.getSize()); 90 response.setDateHeader("Last-Modified", attachment.getDate().getTime()); 91 BufferedOutputStream out = new BufferedOutputStream (response.getOutputStream()); 92 BufferedInputStream in = new BufferedInputStream (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 Logger.log(Logger.DEBUG, "old style image: " + fileName); 105 String oldStyleFile = "/images/image-" + snip.getName() + "-" + fileName; 106 if (getServletContext().getResource(oldStyleFile) != null) { 107 RequestDispatcher dispatcher = request.getRequestDispatcher(oldStyleFile); 108 if (dispatcher != null) { 109 dispatcher.forward(request, response); 110 return; 111 } 112 } 113 } 114 } 115 116 throw new ServletException ("file does not exist: " + fileName); 118 } 119 } 120 | Popular Tags |