1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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, 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              HttpServletResponse response = PortalUtil.getHttpServletResponse(
79                  actionResponse);
80  
81              getFile(messageId, fileName, response);
82  
83              setForward(actionRequest, ActionConstants.COMMON_NULL);
84          }
85          catch (Exception e) {
86              PortalUtil.sendError(e, actionRequest, actionResponse);
87          }
88      }
89  
90      protected void getFile(
91              long messageId, String fileName, HttpServletResponse response)
92          throws Exception {
93  
94          MBMessage message = MBMessageServiceUtil.getMessage(messageId);
95  
96          String path = message.getAttachmentsDir() + "/" + fileName;
97  
98          InputStream is = DLLocalServiceUtil.getFileAsStream(
99              message.getCompanyId(), CompanyConstants.SYSTEM, path);
100         int contentLength = (int)DLServiceUtil.getFileSize(
101             message.getCompanyId(), CompanyConstants.SYSTEM, path);
102         String contentType = MimeTypesUtil.getContentType(fileName);
103 
104         ServletResponseUtil.sendFile(
105             response, fileName, is, contentLength, contentType);
106     }
107 
108     protected boolean isCheckMethodOnProcessAction() {
109         return _CHECK_METHOD_ON_PROCESS_ACTION;
110     }
111 
112     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
113 
114 }