1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.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              double version = ParamUtil.getDouble(request, "version");
71  
72              long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
73  
74              String uuid = ParamUtil.getString(request, "uuid");
75              long groupId = ParamUtil.getLong(request, "groupId");
76  
77              String targetExtension = ParamUtil.getString(
78                  request, "targetExtension");
79  
80              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
81                  WebKeys.THEME_DISPLAY);
82  
83              getFile(
84                  folderId, name, version, fileShortcutId, uuid, groupId,
85                  targetExtension, themeDisplay, request, response);
86  
87              return null;
88          }
89          catch (Exception e) {
90              PortalUtil.sendError(e, request, response);
91  
92              return null;
93          }
94      }
95  
96      public void processAction(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              ActionRequest actionRequest, ActionResponse actionResponse)
99          throws Exception {
100 
101         try {
102             long folderId = ParamUtil.getLong(actionRequest, "folderId");
103             String name = ParamUtil.getString(actionRequest, "name");
104             double version = ParamUtil.getDouble(actionRequest, "version");
105 
106             long fileShortcutId = ParamUtil.getLong(
107                 actionRequest, "fileShortcutId");
108 
109             String uuid = ParamUtil.getString(actionRequest, "uuid");
110             long groupId = ParamUtil.getLong(actionRequest, "groupId");
111 
112             String targetExtension = ParamUtil.getString(
113                 actionRequest, "targetExtension");
114 
115             ThemeDisplay themeDisplay =
116                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
117 
118             HttpServletRequest request = PortalUtil.getHttpServletRequest(
119                 actionRequest);
120             HttpServletResponse response = PortalUtil.getHttpServletResponse(
121                 actionResponse);
122 
123             getFile(
124                 folderId, name, version, fileShortcutId, uuid, groupId,
125                 targetExtension, themeDisplay, request, response);
126 
127             setForward(actionRequest, ActionConstants.COMMON_NULL);
128         }
129         catch (NoSuchFileEntryException nsfee) {
130             PortalUtil.sendError(
131                 HttpServletResponse.SC_NOT_FOUND, nsfee, actionRequest,
132                 actionResponse);
133         }
134         catch (Exception e) {
135             PortalUtil.sendError(e, actionRequest, actionResponse);
136         }
137     }
138 
139     protected void getFile(
140             long folderId, String name, double version, long fileShortcutId,
141             String uuid, long groupId, String targetExtension,
142             ThemeDisplay themeDisplay, HttpServletRequest request,
143             HttpServletResponse response)
144         throws Exception {
145 
146         long companyId = themeDisplay.getCompanyId();
147         long userId = themeDisplay.getUserId();
148 
149         DLFileEntry fileEntry = null;
150 
151         if (Validator.isNotNull(uuid) && (groupId > 0)) {
152             try {
153                 fileEntry =
154                     DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
155                         uuid, groupId);
156 
157                 folderId = fileEntry.getFolderId();
158                 name = fileEntry.getName();
159             }
160             catch (Exception e) {
161             }
162         }
163 
164         if (fileShortcutId <= 0) {
165             DLFileEntryPermission.check(
166                 themeDisplay.getPermissionChecker(), folderId, name,
167                 ActionKeys.VIEW);
168         }
169         else {
170             DLFileShortcut fileShortcut =
171                 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
172 
173             folderId = fileShortcut.getToFolderId();
174             name = fileShortcut.getToName();
175         }
176 
177         if (fileEntry == null) {
178             fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
179                 folderId, name);
180         }
181 
182         if (version == 0) {
183             if (fileEntry.getVersion() > 0) {
184                 version = fileEntry.getVersion();
185             }
186             else {
187                 throw new NoSuchFileEntryException();
188             }
189         }
190 
191         InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
192             companyId, userId, folderId, name, version);
193 
194         boolean converted = false;
195 
196         String fileName = fileEntry.getTitleWithExtension();
197 
198         if (Validator.isNotNull(targetExtension)) {
199             String id = DocumentConversionUtil.getTempFileId(
200                 fileEntry.getFileEntryId(), version);
201 
202             String sourceExtension = FileUtil.getExtension(name);
203 
204             InputStream convertedIS = DocumentConversionUtil.convert(
205                 id, is, sourceExtension, targetExtension);
206 
207             if ((convertedIS != null) && (convertedIS != is)) {
208                 StringBuilder sb = new StringBuilder();
209 
210                 sb.append(fileEntry.getTitle());
211                 sb.append(StringPool.PERIOD);
212                 sb.append(targetExtension);
213 
214                 fileName = sb.toString();
215 
216                 is = convertedIS;
217 
218                 converted = true;
219             }
220         }
221 
222         int contentLength = 0;
223 
224         if (!converted) {
225             if (version >= fileEntry.getVersion()) {
226                 contentLength = fileEntry.getSize();
227             }
228             else {
229                 DLFileVersion fileVersion =
230                     DLFileVersionLocalServiceUtil.getFileVersion(
231                         folderId, name, version);
232 
233                 contentLength = fileVersion.getSize();
234             }
235         }
236 
237         String contentType = MimeTypesUtil.getContentType(fileName);
238 
239         ServletResponseUtil.sendFile(
240             request, response, fileName, is, contentLength, contentType);
241     }
242 
243     protected boolean isCheckMethodOnProcessAction() {
244         return _CHECK_METHOD_ON_PROCESS_ACTION;
245     }
246 
247     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
248 
249 }