1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.NoSuchFileEntryException;
37  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
38  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
39  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
40  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
41  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
42  import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
43  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
44  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
45  import com.liferay.util.servlet.ServletResponseUtil;
46  
47  import java.io.InputStream;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletResponse;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="GetFileAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   * @author Jorge Ferrer
65   * @author Charles May
66   * @author Bruno Farache
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 (NoSuchFileEntryException nsfee) {
138             PortalUtil.sendError(
139                 HttpServletResponse.SC_NOT_FOUND, nsfee, actionRequest,
140                 actionResponse);
141         }
142         catch (Exception e) {
143             PortalUtil.sendError(e, actionRequest, actionResponse);
144         }
145     }
146 
147     protected void getFile(
148             long folderId, String name, double version, long fileShortcutId,
149             String uuid, long groupId, String targetExtension,
150             ThemeDisplay themeDisplay, HttpServletRequest request,
151             HttpServletResponse response)
152         throws Exception {
153 
154         long companyId = themeDisplay.getCompanyId();
155         long userId = themeDisplay.getUserId();
156 
157         DLFileEntry fileEntry = null;
158 
159         if (Validator.isNotNull(uuid) && (groupId > 0)) {
160             try {
161                 fileEntry =
162                     DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
163                         uuid, groupId);
164 
165                 folderId = fileEntry.getFolderId();
166                 name = fileEntry.getName();
167             }
168             catch (Exception e) {
169             }
170         }
171 
172         if (fileShortcutId <= 0) {
173             DLFileEntryPermission.check(
174                 themeDisplay.getPermissionChecker(), folderId, name,
175                 ActionKeys.VIEW);
176         }
177         else {
178             DLFileShortcut fileShortcut =
179                 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
180 
181             folderId = fileShortcut.getToFolderId();
182             name = fileShortcut.getToName();
183         }
184 
185         if (fileEntry == null) {
186             fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
187                 folderId, name);
188         }
189 
190         if (version == 0) {
191             if (fileEntry.getVersion() > 0) {
192                 version = fileEntry.getVersion();
193             }
194             else {
195                 throw new NoSuchFileEntryException();
196             }
197         }
198 
199         InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
200             companyId, userId, folderId, name, version);
201 
202         boolean converted = false;
203 
204         String fileName = fileEntry.getTitleWithExtension();
205 
206         if (Validator.isNotNull(targetExtension)) {
207             String id = DocumentConversionUtil.getTempFileId(
208                 fileEntry.getFileEntryId(), version);
209 
210             String sourceExtension = FileUtil.getExtension(name);
211 
212             InputStream convertedIS = DocumentConversionUtil.convert(
213                 id, is, sourceExtension, targetExtension);
214 
215             if ((convertedIS != null) && (convertedIS != is)) {
216                 StringBuilder sb = new StringBuilder();
217 
218                 sb.append(fileEntry.getTitle());
219                 sb.append(StringPool.PERIOD);
220                 sb.append(targetExtension);
221 
222                 fileName = sb.toString();
223 
224                 is = convertedIS;
225 
226                 converted = true;
227             }
228         }
229 
230         int contentLength = 0;
231 
232         if (!converted) {
233             if (version >= fileEntry.getVersion()) {
234                 contentLength = fileEntry.getSize();
235             }
236             else {
237                 DLFileVersion fileVersion =
238                     DLFileVersionLocalServiceUtil.getFileVersion(
239                         folderId, name, version);
240 
241                 contentLength = fileVersion.getSize();
242             }
243         }
244 
245         String contentType = MimeTypesUtil.getContentType(fileName);
246 
247         ServletResponseUtil.sendFile(
248             response, fileName, is, contentLength, contentType);
249     }
250 
251     protected boolean isCheckMethodOnProcessAction() {
252         return _CHECK_METHOD_ON_PROCESS_ACTION;
253     }
254 
255     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
256 
257 }