1
22
23 package com.liferay.portlet.messageboards.action;
24
25 import com.liferay.documentlibrary.service.DLLocalServiceUtil;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.model.CompanyConstants;
28 import com.liferay.portal.struts.ActionConstants;
29 import com.liferay.portal.struts.PortletAction;
30 import com.liferay.portal.util.MimeTypesUtil;
31 import com.liferay.portlet.ActionResponseImpl;
32 import com.liferay.portlet.messageboards.model.MBMessage;
33 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
34 import com.liferay.util.servlet.ServletResponseUtil;
35
36 import java.io.InputStream;
37
38 import javax.portlet.ActionRequest;
39 import javax.portlet.ActionResponse;
40 import javax.portlet.PortletConfig;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import javax.servlet.jsp.PageContext;
45
46 import org.apache.struts.action.ActionForm;
47 import org.apache.struts.action.ActionForward;
48 import org.apache.struts.action.ActionMapping;
49
50
56 public class GetMessageAttachmentAction extends PortletAction {
57
58 public ActionForward strutsExecute(
59 ActionMapping mapping, ActionForm form, HttpServletRequest req,
60 HttpServletResponse res)
61 throws Exception {
62
63 try {
64 long messageId = ParamUtil.getLong(req, "messageId");
65 String fileName = ParamUtil.getString(req, "attachment");
66
67 getFile(messageId, fileName, res);
68
69 return null;
70 }
71 catch (Exception e) {
72 req.setAttribute(PageContext.EXCEPTION, e);
73
74 return mapping.findForward(ActionConstants.COMMON_ERROR);
75 }
76 }
77
78 public void processAction(
79 ActionMapping mapping, ActionForm form, PortletConfig config,
80 ActionRequest req, ActionResponse res)
81 throws Exception {
82
83 long messageId = ParamUtil.getLong(req, "messageId");
84 String fileName = ParamUtil.getString(req, "attachment");
85
86 HttpServletResponse httpRes =
87 ((ActionResponseImpl)res).getHttpServletResponse();
88
89 getFile(messageId, fileName, httpRes);
90
91 setForward(req, ActionConstants.COMMON_NULL);
92 }
93
94 protected void getFile(
95 long messageId, String fileName, HttpServletResponse res)
96 throws Exception {
97
98 InputStream is = null;
99
100 try {
101 MBMessage message = MBMessageServiceUtil.getMessage(messageId);
102
103 is = DLLocalServiceUtil.getFileAsStream(
104 message.getCompanyId(), CompanyConstants.SYSTEM,
105 message.getAttachmentsDir() + "/" + fileName);
106
107 String contentType = MimeTypesUtil.getContentType(fileName);
108
109 ServletResponseUtil.sendFile(res, fileName, is, contentType);
110 }
111 finally {
112 ServletResponseUtil.cleanUp(is);
113 }
114 }
115
116 protected boolean isCheckMethodOnProcessAction() {
117 return _CHECK_METHOD_ON_PROCESS_ACTION;
118 }
119
120 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
121
122 }