001
014
015 package com.liferay.portlet.messageboards.action;
016
017 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018 import com.liferay.portal.kernel.util.MimeTypesUtil;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.model.CompanyConstants;
021 import com.liferay.portal.struts.ActionConstants;
022 import com.liferay.portal.struts.PortletAction;
023 import com.liferay.portal.util.PortalUtil;
024 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
025 import com.liferay.portlet.messageboards.model.MBMessage;
026 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
027
028 import java.io.InputStream;
029
030 import javax.portlet.ActionRequest;
031 import javax.portlet.ActionResponse;
032 import javax.portlet.PortletConfig;
033
034 import javax.servlet.http.HttpServletRequest;
035 import javax.servlet.http.HttpServletResponse;
036
037 import org.apache.struts.action.ActionForm;
038 import org.apache.struts.action.ActionForward;
039 import org.apache.struts.action.ActionMapping;
040
041
044 public class GetMessageAttachmentAction extends PortletAction {
045
046 @Override
047 public void processAction(
048 ActionMapping actionMapping, ActionForm actionForm,
049 PortletConfig portletConfig, ActionRequest actionRequest,
050 ActionResponse actionResponse)
051 throws Exception {
052
053 try {
054 long messageId = ParamUtil.getLong(actionRequest, "messageId");
055 String fileName = ParamUtil.getString(actionRequest, "attachment");
056
057 HttpServletRequest request = PortalUtil.getHttpServletRequest(
058 actionRequest);
059 HttpServletResponse response = PortalUtil.getHttpServletResponse(
060 actionResponse);
061
062 getFile(messageId, fileName, request, response);
063
064 setForward(actionRequest, ActionConstants.COMMON_NULL);
065 }
066 catch (Exception e) {
067 PortalUtil.sendError(e, actionRequest, actionResponse);
068 }
069 }
070
071 @Override
072 public ActionForward strutsExecute(
073 ActionMapping actionMapping, ActionForm actionForm,
074 HttpServletRequest request, HttpServletResponse response)
075 throws Exception {
076
077 try {
078 long messageId = ParamUtil.getLong(request, "messageId");
079 String fileName = ParamUtil.getString(request, "attachment");
080
081 getFile(messageId, fileName, request, response);
082
083 return null;
084 }
085 catch (Exception e) {
086 PortalUtil.sendError(e, request, response);
087
088 return null;
089 }
090 }
091
092 protected void getFile(
093 long messageId, String fileName, HttpServletRequest request,
094 HttpServletResponse response)
095 throws Exception {
096
097 MBMessage message = MBMessageServiceUtil.getMessage(messageId);
098
099 String path = message.getAttachmentsDir() + "/" + fileName;
100
101 InputStream is = DLStoreUtil.getFileAsStream(
102 message.getCompanyId(), CompanyConstants.SYSTEM, path);
103 long contentLength = DLStoreUtil.getFileSize(
104 message.getCompanyId(), CompanyConstants.SYSTEM, path);
105 String contentType = MimeTypesUtil.getContentType(fileName);
106
107 ServletResponseUtil.sendFile(
108 request, response, fileName, is, contentLength, contentType);
109 }
110
111 @Override
112 protected boolean isCheckMethodOnProcessAction() {
113 return _CHECK_METHOD_ON_PROCESS_ACTION;
114 }
115
116 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
117
118 }