1   /**
2    * Copyright (c) 2000-2008 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.NoSuchFileException;
26  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
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.MimeTypesUtil;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.ActionRequestImpl;
35  import com.liferay.portlet.ActionResponseImpl;
36  import com.liferay.portlet.wiki.NoSuchPageException;
37  import com.liferay.portlet.wiki.model.WikiPage;
38  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  
41  import java.io.InputStream;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  import javax.servlet.jsp.PageContext;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="GetPageAttachmentAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Jorge Ferrer
59   *
60   */
61  public class GetPageAttachmentAction extends PortletAction {
62  
63      public ActionForward strutsExecute(
64              ActionMapping mapping, ActionForm form, HttpServletRequest req,
65              HttpServletResponse res)
66          throws Exception {
67  
68          try {
69              long nodeId = ParamUtil.getLong(req, "nodeId");
70              String title = ParamUtil.getString(req, "title");
71              String fileName = ParamUtil.getString(req, "fileName");
72  
73              getFile(nodeId, title, fileName, req, res);
74  
75              return null;
76          }
77          catch (Exception e) {
78              req.setAttribute(PageContext.EXCEPTION, e);
79  
80              return mapping.findForward(ActionConstants.COMMON_ERROR);
81          }
82      }
83  
84      public void processAction(
85              ActionMapping mapping, ActionForm form, PortletConfig config,
86              ActionRequest req, ActionResponse res)
87          throws Exception {
88  
89          long nodeId = ParamUtil.getLong(req, "nodeId");
90          String title = ParamUtil.getString(req, "title");
91          String fileName = ParamUtil.getString(req, "fileName");
92  
93          HttpServletRequest httpReq =
94              ((ActionRequestImpl)req).getHttpServletRequest();
95          HttpServletResponse httpRes =
96              ((ActionResponseImpl)res).getHttpServletResponse();
97  
98          getFile(nodeId, title, fileName, httpReq, httpRes);
99  
100         setForward(req, ActionConstants.COMMON_NULL);
101     }
102 
103     protected void getFile(
104             long nodeId, String title, String fileName, HttpServletRequest req,
105             HttpServletResponse res)
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             is = DLLocalServiceUtil.getFileAsStream(
121                 page.getCompanyId(), CompanyConstants.SYSTEM,
122                 page.getAttachmentsDir() + "/" + fileName);
123 
124             String contentType = MimeTypesUtil.getContentType(fileName);
125 
126             ServletResponseUtil.sendFile(res, fileName, is, contentType);
127         }
128         catch (NoSuchFileException nsfe) {
129             PortalUtil.sendError(
130                 HttpServletResponse.SC_NOT_FOUND, nsfe, req, res);
131         }
132         catch (NoSuchPageException nspe) {
133             PortalUtil.sendError(
134                 HttpServletResponse.SC_NOT_FOUND, nspe, req, res);
135         }
136         finally {
137             ServletResponseUtil.cleanUp(is);
138         }
139     }
140 
141     protected boolean isCheckMethodOnProcessAction() {
142         return _CHECK_METHOD_ON_PROCESS_ACTION;
143     }
144 
145     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
146 
147 }