1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.action;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.MimeTypesUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.security.permission.ActionKeys;
23  import com.liferay.portal.struts.ActionConstants;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
31  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
32  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
33  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
34  import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
35  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
36  import com.liferay.portlet.documentlibrary.util.DLUtil;
37  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.io.InputStream;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="GetFileAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Jorge Ferrer
58   * @author Charles May
59   * @author Bruno Farache
60   */
61  public class GetFileAction extends PortletAction {
62  
63      public ActionForward strutsExecute(
64              ActionMapping mapping, ActionForm form, HttpServletRequest request,
65              HttpServletResponse response)
66          throws Exception {
67  
68          try {
69              long folderId = ParamUtil.getLong(request, "folderId");
70              String name = ParamUtil.getString(request, "name");
71              String title = ParamUtil.getString(request, "title");
72              String version = ParamUtil.getString(request, "version");
73  
74              long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
75  
76              String uuid = ParamUtil.getString(request, "uuid");
77  
78              String targetExtension = ParamUtil.getString(
79                  request, "targetExtension");
80  
81              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
82                  WebKeys.THEME_DISPLAY);
83  
84              long groupId = ParamUtil.getLong(
85                  request, "groupId", themeDisplay.getScopeGroupId());
86  
87              getFile(
88                  folderId, name, title, version, fileShortcutId, uuid, groupId,
89                  targetExtension, themeDisplay, request, response);
90  
91              return null;
92          }
93          catch (Exception e) {
94              PortalUtil.sendError(e, request, response);
95  
96              return null;
97          }
98      }
99  
100     public void processAction(
101             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102             ActionRequest actionRequest, ActionResponse actionResponse)
103         throws Exception {
104 
105         try {
106             long folderId = ParamUtil.getLong(actionRequest, "folderId");
107             String name = ParamUtil.getString(actionRequest, "name");
108             String title = ParamUtil.getString(actionRequest, "title");
109             String version = ParamUtil.getString(actionRequest, "version");
110 
111             long fileShortcutId = ParamUtil.getLong(
112                 actionRequest, "fileShortcutId");
113 
114             String uuid = ParamUtil.getString(actionRequest, "uuid");
115 
116             String targetExtension = ParamUtil.getString(
117                 actionRequest, "targetExtension");
118 
119             ThemeDisplay themeDisplay =
120                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
121 
122             long groupId = ParamUtil.getLong(
123                 actionRequest, "groupId", themeDisplay.getScopeGroupId());
124 
125             HttpServletRequest request = PortalUtil.getHttpServletRequest(
126                 actionRequest);
127             HttpServletResponse response = PortalUtil.getHttpServletResponse(
128                 actionResponse);
129 
130             getFile(
131                 folderId, name, title, version, fileShortcutId, uuid, groupId,
132                 targetExtension, themeDisplay, request, response);
133 
134             setForward(actionRequest, ActionConstants.COMMON_NULL);
135         }
136         catch (NoSuchFileEntryException nsfee) {
137             PortalUtil.sendError(
138                 HttpServletResponse.SC_NOT_FOUND, nsfee, actionRequest,
139                 actionResponse);
140         }
141         catch (Exception e) {
142             PortalUtil.sendError(e, actionRequest, actionResponse);
143         }
144     }
145 
146     protected void getFile(
147             long folderId, String name, String title, String version,
148             long fileShortcutId, String uuid, long groupId,
149             String targetExtension, ThemeDisplay themeDisplay,
150             HttpServletRequest request, HttpServletResponse response)
151         throws Exception {
152 
153         long companyId = themeDisplay.getCompanyId();
154         long userId = themeDisplay.getUserId();
155 
156         if (name.startsWith("DLFE-")) {
157             name = name.substring("DLFE-".length());
158         }
159 
160         name = FileUtil.stripExtension(name);
161 
162         DLFileEntry fileEntry = null;
163 
164         if (Validator.isNotNull(uuid) && (groupId > 0)) {
165             try {
166                 fileEntry =
167                     DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
168                         uuid, groupId);
169 
170                 folderId = fileEntry.getFolderId();
171                 name = fileEntry.getName();
172             }
173             catch (Exception e) {
174             }
175         }
176 
177         if (fileShortcutId <= 0) {
178             if (Validator.isNotNull(name)) {
179                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
180                     groupId, folderId, name);
181 
182                 title = fileEntry.getTitle();
183             }
184             else if (Validator.isNotNull(title)) {
185                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntryByTitle(
186                     groupId, folderId, title);
187 
188                 name = fileEntry.getName();
189             }
190 
191             DLFileEntryPermission.check(
192                 themeDisplay.getPermissionChecker(), fileEntry,
193                 ActionKeys.VIEW);
194         }
195         else {
196             DLFileShortcut fileShortcut =
197                 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
198 
199             folderId = fileShortcut.getToFolderId();
200             name = fileShortcut.getToName();
201 
202             fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
203                 groupId, folderId, name);
204         }
205 
206         if (Validator.isNull(version)) {
207             if (Validator.isNotNull(fileEntry.getVersion())) {
208                 version = fileEntry.getVersion();
209             }
210             else {
211                 throw new NoSuchFileEntryException();
212             }
213         }
214 
215         InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
216             companyId, userId, groupId, folderId, name, version);
217 
218         boolean converted = false;
219 
220         String fileName = fileEntry.getTitle();
221 
222         if (Validator.isNotNull(targetExtension)) {
223             String id = DocumentConversionUtil.getTempFileId(
224                 fileEntry.getFileEntryId(), version);
225 
226             String sourceExtension = FileUtil.getExtension(fileName);
227 
228             InputStream convertedIS = DocumentConversionUtil.convert(
229                 id, is, sourceExtension, targetExtension);
230 
231             if ((convertedIS != null) && (convertedIS != is)) {
232                 fileName = FileUtil.stripExtension(
233                     fileEntry.getTitle()).concat(StringPool.PERIOD).concat(
234                         targetExtension);
235 
236                 is = convertedIS;
237 
238                 converted = true;
239             }
240         }
241 
242         int contentLength = 0;
243 
244         if (!converted) {
245             if (DLUtil.compareVersions(version, fileEntry.getVersion()) >= 0) {
246                 contentLength = fileEntry.getSize();
247             }
248             else {
249                 DLFileVersion fileVersion =
250                     DLFileVersionLocalServiceUtil.getFileVersion(
251                         groupId, folderId, name, version);
252 
253                 contentLength = fileVersion.getSize();
254             }
255         }
256 
257         String contentType = MimeTypesUtil.getContentType(fileName);
258 
259         ServletResponseUtil.sendFile(
260             response, fileName, is, contentLength, contentType);
261     }
262 
263     protected boolean isCheckMethodOnProcessAction() {
264         return _CHECK_METHOD_ON_PROCESS_ACTION;
265     }
266 
267     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
268 
269 }