1 7 package org.contineo.actions.communication; 8 9 import java.io.BufferedInputStream ; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.InputStream ; 13 import java.io.PrintWriter ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.http.HttpSession ; 17 import org.apache.log4j.Level; 18 import org.apache.log4j.Logger; 19 import org.apache.struts.action.Action; 20 import org.apache.struts.action.ActionForm; 21 import org.apache.struts.action.ActionForward; 22 import org.apache.struts.action.ActionMapping; 23 import org.contineo.communication.Attachment; 24 import org.contineo.communication.dao.AttachmentDAO; 25 import org.contineo.core.LoggingManager; 26 import org.contineo.core.SessionManagement; 27 import org.contineo.core.config.SettingConfigurator; 28 34 public class ShowAttachmentAction extends Action { 35 36 40 private Logger logger; 41 42 43 46 public ShowAttachmentAction() { 47 logger = LoggingManager.getLogger(this.getClass()); 48 } 49 50 public ActionForward execute(ActionMapping mapping, 51 ActionForm form, HttpServletRequest request, 52 HttpServletResponse response) { 53 ActionForward actionForward = new ActionForward(); 54 HttpSession session = request.getSession(); 55 if (SessionManagement.isValid(session)) { 56 try { 57 String messageid = request.getParameter("messageid"); 58 String partid = request.getParameter("partid"); 59 String username = (String )session.getAttribute("authuser"); 60 AttachmentDAO attDao = new AttachmentDAO(); 61 Attachment att = attDao.findByPrimaryKey(Integer.parseInt(messageid), Integer.parseInt(partid)); 62 response.setContentType(att.getMimeType()); 63 SettingConfigurator conf = new SettingConfigurator(); 64 String userdir = conf.getValue("userdir") + "/" + username + "/mails/" + String.valueOf(att.getMessageId()); 65 File file = new File (userdir + "/" + att.getFilename()); 66 InputStream is = new FileInputStream (file); 67 BufferedInputStream bis = new BufferedInputStream (is); 68 PrintWriter pw = response.getWriter(); 69 int letter = 0; 70 while ((letter = bis.read()) != -1) { 71 pw.write(letter); 72 } 73 pw.flush(); 74 pw.close(); 75 bis.close(); 76 77 actionForward = null; 79 } catch (Exception e) { 80 if (logger.isEnabledFor(Level.ERROR)) 81 logger.error(e.getMessage()); 82 } 83 actionForward = mapping.findForward("error"); 84 } else 85 actionForward = mapping.findForward("invalid"); 86 return actionForward; 87 } 88 } 89 | Popular Tags |