1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.wiki.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.kernel.util.StringPool;
30  import com.liferay.portal.model.CompanyConstants;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.wiki.model.WikiPage;
35  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  
38  import java.io.InputStream;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="GetPageAttachmentAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Jorge Ferrer
55   *
56   */
57  public class GetPageAttachmentAction extends PortletAction {
58  
59      public ActionForward strutsExecute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          try {
65              long nodeId = ParamUtil.getLong(request, "nodeId");
66              String title = ParamUtil.getString(request, "title");
67              String fileName = ParamUtil.getString(request, "fileName");
68  
69              getFile(nodeId, title, fileName, request, response);
70  
71              return null;
72          }
73          catch (Exception e) {
74              PortalUtil.sendError(e, request, response);
75  
76              return null;
77          }
78      }
79  
80      public void processAction(
81              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
82              ActionRequest actionRequest, ActionResponse actionResponse)
83          throws Exception {
84  
85          try {
86              long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
87              String title = ParamUtil.getString(actionRequest, "title");
88              String fileName = ParamUtil.getString(actionRequest, "fileName");
89  
90              HttpServletRequest request = PortalUtil.getHttpServletRequest(
91                  actionRequest);
92              HttpServletResponse response = PortalUtil.getHttpServletResponse(
93                  actionResponse);
94  
95              getFile(nodeId, title, fileName, request, response);
96  
97              setForward(actionRequest, ActionConstants.COMMON_NULL);
98          }
99          catch (Exception e) {
100             PortalUtil.sendError(e, actionRequest, actionResponse);
101         }
102     }
103 
104     protected void getFile(
105             long nodeId, String title, String fileName,
106             HttpServletRequest request, HttpServletResponse response)
107         throws Exception {
108 
109         int pos = fileName.indexOf(StringPool.SLASH);
110 
111         if (pos != -1) {
112             title = fileName.substring(0, pos);
113             fileName = fileName.substring(pos + 1);
114         }
115 
116         InputStream is = null;
117 
118         try {
119             WikiPage page = WikiPageServiceUtil.getPage(nodeId, title);
120 
121             String path = page.getAttachmentsDir() + "/" + fileName;
122 
123             is = DLLocalServiceUtil.getFileAsStream(
124                 page.getCompanyId(), CompanyConstants.SYSTEM, path);
125             int contentLength = (int)DLServiceUtil.getFileSize(
126                 page.getCompanyId(), CompanyConstants.SYSTEM, path);
127             String contentType = MimeTypesUtil.getContentType(fileName);
128 
129             ServletResponseUtil.sendFile(
130                 response, fileName, is, contentLength, contentType);
131         }
132         finally {
133             ServletResponseUtil.cleanUp(is);
134         }
135     }
136 
137     protected boolean isCheckMethodOnProcessAction() {
138         return _CHECK_METHOD_ON_PROCESS_ACTION;
139     }
140 
141     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
142 
143 }