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.wiki.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.kernel.util.StringPool;
22  import com.liferay.portal.model.CompanyConstants;
23  import com.liferay.portal.struts.ActionConstants;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portlet.wiki.model.WikiPage;
27  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
28  import com.liferay.util.servlet.ServletResponseUtil;
29  
30  import java.io.InputStream;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="GetPageAttachmentAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Jorge Ferrer
47   */
48  public class GetPageAttachmentAction extends PortletAction {
49  
50      public ActionForward strutsExecute(
51              ActionMapping mapping, ActionForm form, HttpServletRequest request,
52              HttpServletResponse response)
53          throws Exception {
54  
55          try {
56              long nodeId = ParamUtil.getLong(request, "nodeId");
57              String title = ParamUtil.getString(request, "title");
58              String fileName = ParamUtil.getString(request, "fileName");
59  
60              getFile(nodeId, title, fileName, request, response);
61  
62              return null;
63          }
64          catch (Exception e) {
65              PortalUtil.sendError(e, request, response);
66  
67              return null;
68          }
69      }
70  
71      public void processAction(
72              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
73              ActionRequest actionRequest, ActionResponse actionResponse)
74          throws Exception {
75  
76          try {
77              long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
78              String title = ParamUtil.getString(actionRequest, "title");
79              String fileName = ParamUtil.getString(actionRequest, "fileName");
80  
81              HttpServletRequest request = PortalUtil.getHttpServletRequest(
82                  actionRequest);
83              HttpServletResponse response = PortalUtil.getHttpServletResponse(
84                  actionResponse);
85  
86              getFile(nodeId, title, fileName, request, response);
87  
88              setForward(actionRequest, ActionConstants.COMMON_NULL);
89          }
90          catch (Exception e) {
91              PortalUtil.sendError(e, actionRequest, actionResponse);
92          }
93      }
94  
95      protected void getFile(
96              long nodeId, String title, String fileName,
97              HttpServletRequest request, HttpServletResponse response)
98          throws Exception {
99  
100         int pos = fileName.indexOf(StringPool.SLASH);
101 
102         if (pos != -1) {
103             title = fileName.substring(0, pos);
104             fileName = fileName.substring(pos + 1);
105         }
106 
107         WikiPage page = WikiPageServiceUtil.getPage(nodeId, title);
108 
109         String path = page.getAttachmentsDir() + "/" + fileName;
110 
111         InputStream is = DLLocalServiceUtil.getFileAsStream(
112             page.getCompanyId(), CompanyConstants.SYSTEM, path);
113         int contentLength = (int)DLServiceUtil.getFileSize(
114             page.getCompanyId(), CompanyConstants.SYSTEM, path);
115         String contentType = MimeTypesUtil.getContentType(fileName);
116 
117         ServletResponseUtil.sendFile(
118             response, fileName, is, contentLength, contentType);
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 }