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