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