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.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  public class GetPageAttachmentAction extends PortletAction {
57  
58      public ActionForward strutsExecute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              long nodeId = ParamUtil.getLong(request, "nodeId");
65              String title = ParamUtil.getString(request, "title");
66              String fileName = ParamUtil.getString(request, "fileName");
67  
68              getFile(nodeId, title, fileName, request, response);
69  
70              return null;
71          }
72          catch (Exception e) {
73              PortalUtil.sendError(e, request, response);
74  
75              return null;
76          }
77      }
78  
79      public void processAction(
80              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
81              ActionRequest actionRequest, ActionResponse actionResponse)
82          throws Exception {
83  
84          try {
85              long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
86              String title = ParamUtil.getString(actionRequest, "title");
87              String fileName = ParamUtil.getString(actionRequest, "fileName");
88  
89              HttpServletRequest request = PortalUtil.getHttpServletRequest(
90                  actionRequest);
91              HttpServletResponse response = PortalUtil.getHttpServletResponse(
92                  actionResponse);
93  
94              getFile(nodeId, title, fileName, request, response);
95  
96              setForward(actionRequest, ActionConstants.COMMON_NULL);
97          }
98          catch (Exception e) {
99              PortalUtil.sendError(e, actionRequest, actionResponse);
100         }
101     }
102 
103     protected void getFile(
104             long nodeId, String title, String fileName,
105             HttpServletRequest request, HttpServletResponse response)
106         throws Exception {
107 
108         int pos = fileName.indexOf(StringPool.SLASH);
109 
110         if (pos != -1) {
111             title = fileName.substring(0, pos);
112             fileName = fileName.substring(pos + 1);
113         }
114 
115         InputStream is = null;
116 
117         try {
118             WikiPage page = WikiPageServiceUtil.getPage(nodeId, title);
119 
120             String path = page.getAttachmentsDir() + "/" + fileName;
121 
122             is = DLLocalServiceUtil.getFileAsStream(
123                 page.getCompanyId(), CompanyConstants.SYSTEM, path);
124             int contentLength = (int)DLServiceUtil.getFileSize(
125                 page.getCompanyId(), CompanyConstants.SYSTEM, path);
126             String contentType = MimeTypesUtil.getContentType(fileName);
127 
128             ServletResponseUtil.sendFile(
129                 response, fileName, is, contentLength, contentType);
130         }
131         finally {
132             ServletResponseUtil.cleanUp(is);
133         }
134     }
135 
136     protected boolean isCheckMethodOnProcessAction() {
137         return _CHECK_METHOD_ON_PROCESS_ACTION;
138     }
139 
140     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
141 
142 }