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