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