KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > communication > ShowAttachmentAction


1 /*
2  * Created on 30.03.2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.contineo.actions.communication;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import javax.servlet.http.HttpSession JavaDoc;
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 /**
29  * @author Michael Scholz
30  *
31  * To change the template for this generated type comment go to
32  * Window>Preferences>Java>Code Generation>Code and Comments
33  */

34 public class ShowAttachmentAction extends Action {
35
36     /**
37      * @uml.property name="logger"
38      * @uml.associationEnd
39      */

40     private Logger logger;
41
42     
43     /**
44      *
45      */

46     public ShowAttachmentAction() {
47         logger = LoggingManager.getLogger(this.getClass());
48     }
49
50     public ActionForward execute(ActionMapping mapping,
51                     ActionForm form, HttpServletRequest JavaDoc request,
52                     HttpServletResponse JavaDoc response) {
53         ActionForward actionForward = new ActionForward();
54         HttpSession JavaDoc session = request.getSession();
55         if (SessionManagement.isValid(session)) {
56             try {
57                 String JavaDoc messageid = request.getParameter("messageid");
58                 String JavaDoc partid = request.getParameter("partid");
59                 String JavaDoc username = (String JavaDoc)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 JavaDoc userdir = conf.getValue("userdir") + "/" + username + "/mails/" + String.valueOf(att.getMessageId());
65                 File JavaDoc file = new File JavaDoc(userdir + "/" + att.getFilename());
66                 InputStream JavaDoc is = new FileInputStream JavaDoc(file);
67                 BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is);
68                 PrintWriter JavaDoc 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                 // do not forward, because we already sent something to the client
78
actionForward = null;
79             } catch (Exception JavaDoc 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