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