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