1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.action;
21  
22  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
23  import com.liferay.documentlibrary.service.DLServiceUtil;
24  import com.liferay.portal.kernel.util.MimeTypesUtil;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.model.CompanyConstants;
27  import com.liferay.portal.struts.ActionConstants;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.messageboards.model.MBMessage;
31  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
32  import com.liferay.util.servlet.ServletResponseUtil;
33  
34  import java.io.InputStream;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="GetMessageAttachmentAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class GetMessageAttachmentAction extends PortletAction {
54  
55      public ActionForward strutsExecute(
56              ActionMapping mapping, ActionForm form, HttpServletRequest request,
57              HttpServletResponse response)
58          throws Exception {
59  
60          try {
61              long messageId = ParamUtil.getLong(request, "messageId");
62              String fileName = ParamUtil.getString(request, "attachment");
63  
64              getFile(messageId, fileName, response);
65  
66              return null;
67          }
68          catch (Exception e) {
69              PortalUtil.sendError(e, request, response);
70  
71              return null;
72          }
73      }
74  
75      public void processAction(
76              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
77              ActionRequest actionRequest, ActionResponse actionResponse)
78          throws Exception {
79  
80          try {
81              long messageId = ParamUtil.getLong(actionRequest, "messageId");
82              String fileName = ParamUtil.getString(actionRequest, "attachment");
83  
84              HttpServletResponse response = PortalUtil.getHttpServletResponse(
85                  actionResponse);
86  
87              getFile(messageId, fileName, response);
88  
89              setForward(actionRequest, ActionConstants.COMMON_NULL);
90          }
91          catch (Exception e) {
92              PortalUtil.sendError(e, actionRequest, actionResponse);
93          }
94      }
95  
96      protected void getFile(
97              long messageId, String fileName, HttpServletResponse response)
98          throws Exception {
99  
100         InputStream is = null;
101 
102         try {
103             MBMessage message = MBMessageServiceUtil.getMessage(messageId);
104 
105             String path = message.getAttachmentsDir() + "/" + fileName;
106 
107             is = DLLocalServiceUtil.getFileAsStream(
108                 message.getCompanyId(), CompanyConstants.SYSTEM, path);
109             int contentLength = (int)DLServiceUtil.getFileSize(
110                 message.getCompanyId(), CompanyConstants.SYSTEM, path);
111             String contentType = MimeTypesUtil.getContentType(fileName);
112 
113             ServletResponseUtil.sendFile(
114                 response, fileName, is, contentLength, contentType);
115         }
116         finally {
117             ServletResponseUtil.cleanUp(is);
118         }
119     }
120 
121     protected boolean isCheckMethodOnProcessAction() {
122         return _CHECK_METHOD_ON_PROCESS_ACTION;
123     }
124 
125     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
126 
127 }