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.wiki.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.kernel.util.StringPool;
27  import com.liferay.portal.model.CompanyConstants;
28  import com.liferay.portal.struts.ActionConstants;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.wiki.model.WikiPage;
32  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
33  import com.liferay.util.servlet.ServletResponseUtil;
34  
35  import java.io.InputStream;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="GetPageAttachmentAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Jorge Ferrer
52   *
53   */
54  public class GetPageAttachmentAction extends PortletAction {
55  
56      public ActionForward strutsExecute(
57              ActionMapping mapping, ActionForm form, HttpServletRequest request,
58              HttpServletResponse response)
59          throws Exception {
60  
61          try {
62              long nodeId = ParamUtil.getLong(request, "nodeId");
63              String title = ParamUtil.getString(request, "title");
64              String fileName = ParamUtil.getString(request, "fileName");
65  
66              getFile(nodeId, title, fileName, request, 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 nodeId = ParamUtil.getLong(actionRequest, "nodeId");
84              String title = ParamUtil.getString(actionRequest, "title");
85              String fileName = ParamUtil.getString(actionRequest, "fileName");
86  
87              HttpServletRequest request = PortalUtil.getHttpServletRequest(
88                  actionRequest);
89              HttpServletResponse response = PortalUtil.getHttpServletResponse(
90                  actionResponse);
91  
92              getFile(nodeId, title, fileName, request, response);
93  
94              setForward(actionRequest, ActionConstants.COMMON_NULL);
95          }
96          catch (Exception e) {
97              PortalUtil.sendError(e, actionRequest, actionResponse);
98          }
99      }
100 
101     protected void getFile(
102             long nodeId, String title, String fileName,
103             HttpServletRequest request, HttpServletResponse response)
104         throws Exception {
105 
106         int pos = fileName.indexOf(StringPool.SLASH);
107 
108         if (pos != -1) {
109             title = fileName.substring(0, pos);
110             fileName = fileName.substring(pos + 1);
111         }
112 
113         InputStream is = null;
114 
115         try {
116             WikiPage page = WikiPageServiceUtil.getPage(nodeId, title);
117 
118             String path = page.getAttachmentsDir() + "/" + fileName;
119 
120             is = DLLocalServiceUtil.getFileAsStream(
121                 page.getCompanyId(), CompanyConstants.SYSTEM, path);
122             int contentLength = (int)DLServiceUtil.getFileSize(
123                 page.getCompanyId(), CompanyConstants.SYSTEM, path);
124             String contentType = MimeTypesUtil.getContentType(fileName);
125 
126             ServletResponseUtil.sendFile(
127                 response, fileName, is, contentLength, contentType);
128         }
129         finally {
130             ServletResponseUtil.cleanUp(is);
131         }
132     }
133 
134     protected boolean isCheckMethodOnProcessAction() {
135         return _CHECK_METHOD_ON_PROCESS_ACTION;
136     }
137 
138     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
139 
140 }