1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.action;
16  
17  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
18  import com.liferay.documentlibrary.service.DLServiceUtil;
19  import com.liferay.portal.kernel.util.MimeTypesUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.model.CompanyConstants;
22  import com.liferay.portal.struts.ActionConstants;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portlet.messageboards.model.MBMessage;
26  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
27  import com.liferay.util.servlet.ServletResponseUtil;
28  
29  import java.io.InputStream;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="GetMessageAttachmentAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class GetMessageAttachmentAction extends PortletAction {
48  
49      public ActionForward strutsExecute(
50              ActionMapping mapping, ActionForm form, HttpServletRequest request,
51              HttpServletResponse response)
52          throws Exception {
53  
54          try {
55              long messageId = ParamUtil.getLong(request, "messageId");
56              String fileName = ParamUtil.getString(request, "attachment");
57  
58              getFile(messageId, fileName, request, response);
59  
60              return null;
61          }
62          catch (Exception e) {
63              PortalUtil.sendError(e, request, response);
64  
65              return null;
66          }
67      }
68  
69      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71              ActionRequest actionRequest, ActionResponse actionResponse)
72          throws Exception {
73  
74          try {
75              long messageId = ParamUtil.getLong(actionRequest, "messageId");
76              String fileName = ParamUtil.getString(actionRequest, "attachment");
77  
78              HttpServletRequest request = PortalUtil.getHttpServletRequest(
79                  actionRequest);
80              HttpServletResponse response = PortalUtil.getHttpServletResponse(
81                  actionResponse);
82  
83              getFile(messageId, fileName, request, response);
84  
85              setForward(actionRequest, ActionConstants.COMMON_NULL);
86          }
87          catch (Exception e) {
88              PortalUtil.sendError(e, actionRequest, actionResponse);
89          }
90      }
91  
92      protected void getFile(
93              long messageId, String fileName, HttpServletRequest request,
94              HttpServletResponse response)
95          throws Exception {
96  
97          MBMessage message = MBMessageServiceUtil.getMessage(messageId);
98  
99          String path = message.getAttachmentsDir() + "/" + fileName;
100 
101         InputStream is = DLLocalServiceUtil.getFileAsStream(
102             message.getCompanyId(), CompanyConstants.SYSTEM, path);
103         int contentLength = (int)DLServiceUtil.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     protected boolean isCheckMethodOnProcessAction() {
112         return _CHECK_METHOD_ON_PROCESS_ACTION;
113     }
114 
115     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
116 
117 }