1   /**
2    * Copyright (c) 2000-2007 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.documentlibrary.action;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.kernel.security.permission.ActionKeys;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.ActionConstants;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.MimeTypesUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.ActionResponseImpl;
35  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
36  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
37  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
38  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
39  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
40  import com.liferay.util.servlet.ServletResponseUtil;
41  
42  import java.io.InputStream;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  import javax.servlet.jsp.PageContext;
51  
52  import org.apache.struts.action.ActionForm;
53  import org.apache.struts.action.ActionForward;
54  import org.apache.struts.action.ActionMapping;
55  
56  /**
57   * <a href="GetFileAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   * @author Jorge Ferrer
61   * @author Charles May
62   *
63   */
64  public class GetFileAction extends PortletAction {
65  
66      public ActionForward strutsExecute(
67              ActionMapping mapping, ActionForm form, HttpServletRequest req,
68              HttpServletResponse res)
69          throws Exception {
70  
71          try {
72              long folderId = ParamUtil.getLong(req, "folderId");
73              String name = ParamUtil.getString(req, "name");
74              double version = ParamUtil.getDouble(req, "version");
75  
76              long fileShortcutId = ParamUtil.getLong(req, "fileShortcutId");
77  
78              ThemeDisplay themeDisplay =
79                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
80  
81              getFile(folderId, name, version, fileShortcutId, themeDisplay, res);
82  
83              return null;
84          }
85          catch (Exception e) {
86              req.setAttribute(PageContext.EXCEPTION, e);
87  
88              return mapping.findForward(ActionConstants.COMMON_ERROR);
89          }
90      }
91  
92      public void processAction(
93              ActionMapping mapping, ActionForm form, PortletConfig config,
94              ActionRequest req, ActionResponse res)
95          throws Exception {
96  
97          long folderId = ParamUtil.getLong(req, "folderId");
98          String name = ParamUtil.getString(req, "name");
99          double version = ParamUtil.getDouble(req, "version");
100 
101         long fileShortcutId = ParamUtil.getLong(req, "fileShortcutId");
102 
103         ThemeDisplay themeDisplay =
104             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
105 
106         HttpServletResponse httpRes =
107             ((ActionResponseImpl)res).getHttpServletResponse();
108 
109         getFile(folderId, name, version, fileShortcutId, themeDisplay, httpRes);
110 
111         setForward(req, ActionConstants.COMMON_NULL);
112     }
113 
114     protected void getFile(
115             long folderId, String name, double version, long fileShortcutId,
116             ThemeDisplay themeDisplay, HttpServletResponse res)
117         throws Exception {
118 
119         InputStream is = null;
120 
121         try {
122             long companyId = themeDisplay.getCompanyId();
123             long userId = themeDisplay.getUserId();
124 
125             if (fileShortcutId <= 0) {
126                 DLFileEntryPermission.check(
127                     themeDisplay.getPermissionChecker(), folderId, name,
128                     ActionKeys.VIEW);
129             }
130             else {
131                 DLFileShortcut fileShortcut =
132                     DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
133 
134                 folderId = fileShortcut.getToFolderId();
135                 name = fileShortcut.getToName();
136             }
137 
138             DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
139                 folderId, name);
140 
141             if (version > 0) {
142                 is = DLFileEntryLocalServiceUtil.getFileAsStream(
143                     companyId, userId, folderId, name, version);
144             }
145             else {
146                 is = DLFileEntryLocalServiceUtil.getFileAsStream(
147                     companyId, userId, folderId, name);
148             }
149 
150             String contentType = MimeTypesUtil.getContentType(name);
151 
152             ServletResponseUtil.sendFile(
153                 res, fileEntry.getTitleWithExtension(), is, contentType);
154         }
155         catch (PortalException pe) {
156             if (pe instanceof PrincipalException) {
157                 throw new PrincipalException();
158             }
159             else {
160                 res.sendError(
161                     HttpServletResponse.SC_NOT_FOUND, pe.getMessage());
162             }
163         }
164         finally {
165             ServletResponseUtil.cleanUp(is);
166         }
167     }
168 
169 }