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.struts.ActionConstants;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
28  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
29  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
30  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
31  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
32  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
33  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
34  import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
35  import com.liferay.portlet.documentlibrary.util.DLUtil;
36  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
37  import com.liferay.util.servlet.ServletResponseUtil;
38  
39  import java.io.InputStream;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="GetFileAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   * @author Jorge Ferrer
57   * @author Charles May
58   * @author Bruno Farache
59   */
60  public class GetFileAction extends PortletAction {
61  
62      public ActionForward strutsExecute(
63              ActionMapping mapping, ActionForm form, HttpServletRequest request,
64              HttpServletResponse response)
65          throws Exception {
66  
67          try {
68              long folderId = ParamUtil.getLong(request, "folderId");
69              String name = ParamUtil.getString(request, "name");
70              String title = ParamUtil.getString(request, "title");
71              String version = ParamUtil.getString(request, "version");
72  
73              long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
74  
75              String uuid = ParamUtil.getString(request, "uuid");
76  
77              String targetExtension = ParamUtil.getString(
78                  request, "targetExtension");
79  
80              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
81                  WebKeys.THEME_DISPLAY);
82  
83              long groupId = ParamUtil.getLong(
84                  request, "groupId", themeDisplay.getScopeGroupId());
85  
86              getFile(
87                  folderId, name, title, version, fileShortcutId, uuid, groupId,
88                  targetExtension, themeDisplay, request, response);
89  
90              return null;
91          }
92          catch (Exception e) {
93              PortalUtil.sendError(e, request, response);
94  
95              return null;
96          }
97      }
98  
99      public void processAction(
100             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101             ActionRequest actionRequest, ActionResponse actionResponse)
102         throws Exception {
103 
104         try {
105             long folderId = ParamUtil.getLong(actionRequest, "folderId");
106             String name = ParamUtil.getString(actionRequest, "name");
107             String title = ParamUtil.getString(actionRequest, "title");
108             String version = ParamUtil.getString(actionRequest, "version");
109 
110             long fileShortcutId = ParamUtil.getLong(
111                 actionRequest, "fileShortcutId");
112 
113             String uuid = ParamUtil.getString(actionRequest, "uuid");
114 
115             String targetExtension = ParamUtil.getString(
116                 actionRequest, "targetExtension");
117 
118             ThemeDisplay themeDisplay =
119                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
120 
121             long groupId = ParamUtil.getLong(
122                 actionRequest, "groupId", themeDisplay.getScopeGroupId());
123 
124             HttpServletRequest request = PortalUtil.getHttpServletRequest(
125                 actionRequest);
126             HttpServletResponse response = PortalUtil.getHttpServletResponse(
127                 actionResponse);
128 
129             getFile(
130                 folderId, name, title, version, fileShortcutId, uuid, groupId,
131                 targetExtension, themeDisplay, request, response);
132 
133             setForward(actionRequest, ActionConstants.COMMON_NULL);
134         }
135         catch (NoSuchFileEntryException nsfee) {
136             PortalUtil.sendError(
137                 HttpServletResponse.SC_NOT_FOUND, nsfee, actionRequest,
138                 actionResponse);
139         }
140         catch (Exception e) {
141             PortalUtil.sendError(e, actionRequest, actionResponse);
142         }
143     }
144 
145     protected void getFile(
146             long folderId, String name, String title, String version,
147             long fileShortcutId, String uuid, long groupId,
148             String targetExtension, ThemeDisplay themeDisplay,
149             HttpServletRequest request, HttpServletResponse response)
150         throws Exception {
151 
152         long companyId = themeDisplay.getCompanyId();
153         long userId = themeDisplay.getUserId();
154 
155         if (name.startsWith("DLFE-")) {
156             name = name.substring("DLFE-".length());
157         }
158 
159         name = FileUtil.stripExtension(name);
160 
161         DLFileEntry fileEntry = null;
162 
163         if (Validator.isNotNull(uuid) && (groupId > 0)) {
164             try {
165                 fileEntry = DLFileEntryServiceUtil.getFileEntryByUuidAndGroupId(
166                     uuid, groupId);
167 
168                 folderId = fileEntry.getFolderId();
169                 name = fileEntry.getName();
170             }
171             catch (Exception e) {
172             }
173         }
174 
175         if (fileShortcutId <= 0) {
176             if (Validator.isNotNull(name)) {
177                 fileEntry = DLFileEntryServiceUtil.getFileEntry(
178                     groupId, folderId, name);
179 
180                 title = fileEntry.getTitle();
181             }
182             else if (Validator.isNotNull(title)) {
183                 fileEntry = DLFileEntryServiceUtil.getFileEntryByTitle(
184                     groupId, folderId, title);
185 
186                 name = fileEntry.getName();
187             }
188         }
189         else {
190             DLFileShortcut fileShortcut =
191                 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
192 
193             folderId = fileShortcut.getToFolderId();
194             name = fileShortcut.getToName();
195 
196             fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
197                 groupId, folderId, name);
198         }
199 
200         if (Validator.isNull(version)) {
201             if (Validator.isNotNull(fileEntry.getVersion())) {
202                 version = fileEntry.getVersion();
203             }
204             else {
205                 throw new NoSuchFileEntryException();
206             }
207         }
208 
209         InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
210             companyId, userId, groupId, folderId, name, version);
211 
212         boolean converted = false;
213 
214         String fileName = fileEntry.getTitle();
215 
216         if (Validator.isNotNull(targetExtension)) {
217             String id = DocumentConversionUtil.getTempFileId(
218                 fileEntry.getFileEntryId(), version);
219 
220             String sourceExtension = FileUtil.getExtension(fileName);
221 
222             InputStream convertedIS = DocumentConversionUtil.convert(
223                 id, is, sourceExtension, targetExtension);
224 
225             if ((convertedIS != null) && (convertedIS != is)) {
226                 fileName = FileUtil.stripExtension(
227                     fileEntry.getTitle()).concat(StringPool.PERIOD).concat(
228                         targetExtension);
229 
230                 is = convertedIS;
231 
232                 converted = true;
233             }
234         }
235 
236         int contentLength = 0;
237 
238         if (!converted) {
239             if (DLUtil.compareVersions(version, fileEntry.getVersion()) >= 0) {
240                 contentLength = (int)fileEntry.getSize();
241             }
242             else {
243                 DLFileVersion fileVersion =
244                     DLFileVersionLocalServiceUtil.getFileVersion(
245                         groupId, folderId, name, version);
246 
247                 contentLength = (int)fileVersion.getSize();
248             }
249         }
250 
251         String contentType = MimeTypesUtil.getContentType(fileName);
252 
253         ServletResponseUtil.sendFile(
254             request, response, fileName, is, contentLength, contentType);
255     }
256 
257     protected boolean isCheckMethodOnProcessAction() {
258         return _CHECK_METHOD_ON_PROCESS_ACTION;
259     }
260 
261     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
262 
263 }