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.portal.servlet;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.HttpUtil;
20  import com.liferay.portal.kernel.util.MimeTypesUtil;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.Company;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.security.auth.PrincipalThreadLocal;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.security.permission.PermissionChecker;
30  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
31  import com.liferay.portal.security.permission.PermissionThreadLocal;
32  import com.liferay.portal.service.CompanyLocalServiceUtil;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
35  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
36  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
37  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
38  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
39  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
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.DLUtil;
44  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
45  import com.liferay.util.servlet.ServletResponseUtil;
46  
47  import java.io.IOException;
48  import java.io.InputStream;
49  
50  import javax.servlet.ServletException;
51  import javax.servlet.http.HttpServlet;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  /**
56   * <a href="DocumentServlet.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Alexander Chow
59   */
60  public class DocumentServlet extends HttpServlet {
61  
62      public void service(
63              HttpServletRequest request, HttpServletResponse response)
64          throws IOException, ServletException {
65  
66          try {
67              long companyId = PortalUtil.getCompanyId(request);
68  
69              User user = PortalUtil.getUser(request);
70  
71              if (user == null) {
72                  Company company = CompanyLocalServiceUtil.getCompany(companyId);
73  
74                  user = company.getDefaultUser();
75              }
76  
77              PermissionChecker permissionChecker = null;
78  
79              PrincipalThreadLocal.setName(user.getUserId());
80  
81              permissionChecker = PermissionCheckerFactoryUtil.create(user, true);
82  
83              PermissionThreadLocal.setPermissionChecker(permissionChecker);
84  
85              String path = request.getPathInfo();
86  
87              if (path.startsWith(StringPool.SLASH)) {
88                  path = path.substring(1);
89              }
90  
91              if (path.endsWith(StringPool.SLASH)) {
92                  path = path.substring(0, path.length() - 1);
93              }
94  
95              String[] pathArray = StringUtil.split(path, StringPool.SLASH);
96  
97              if ((pathArray.length > 0) && (pathArray.length <= 3)) {
98                  sendFile(request, response, permissionChecker, pathArray);
99              }
100             else {
101                 throw new NoSuchFileEntryException();
102             }
103         }
104         catch (NoSuchFileEntryException nsfee) {
105             PortalUtil.sendError(
106                 HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
107         }
108         catch (Exception e) {
109             PortalUtil.sendError(e, request, response);
110         }
111     }
112 
113     protected void sendFile(
114             HttpServletRequest request, HttpServletResponse response,
115             PermissionChecker permissionChecker, String[] pathArray)
116         throws Exception {
117 
118         long groupId = 0;
119         long folderId = 0;
120         String name = null;
121         String fileName = null;
122 
123         DLFileEntry fileEntry = null;
124 
125         if (pathArray.length == 1) {
126             long fileShortcutId = GetterUtil.getLong(pathArray[0]);
127 
128             DLFileShortcut fileShortcut =
129                 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
130 
131             groupId = fileShortcut.getGroupId();
132             folderId = fileShortcut.getToFolderId();
133             name = fileShortcut.getToName();
134 
135             fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
136                 groupId, folderId, name);
137 
138             fileName = fileEntry.getTitle();
139         }
140         else if (pathArray.length == 2) {
141             groupId = GetterUtil.getLong(pathArray[0]);
142 
143             fileEntry =
144                 DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
145                     pathArray[1], groupId);
146 
147             folderId = fileEntry.getFolderId();
148             fileName = fileEntry.getTitle();
149             name = fileEntry.getName();
150         }
151         else {
152             groupId = GetterUtil.getLong(pathArray[0]);
153             folderId = GetterUtil.getLong(pathArray[1]);
154             fileName = HttpUtil.decodeURL(pathArray[2], true);
155 
156             fileEntry = DLFileEntryServiceUtil.getFileEntryByTitle(
157                 groupId, folderId, fileName);
158 
159             name = fileEntry.getName();
160         }
161 
162         if (fileEntry == null) {
163             throw new NoSuchFileEntryException();
164         }
165 
166         DLFileEntryPermission.check(
167             permissionChecker, fileEntry, ActionKeys.VIEW);
168 
169         String version = ParamUtil.getString(request, "version");
170 
171         String targetExtension = ParamUtil.getString(
172             request, "targetExtension");
173 
174         if (Validator.isNull(version)) {
175             if (Validator.isNotNull(fileEntry.getVersion())) {
176                 version = fileEntry.getVersion();
177             }
178             else {
179                 throw new NoSuchFileEntryException();
180             }
181         }
182 
183         InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
184             permissionChecker.getCompanyId(), permissionChecker.getUserId(),
185             groupId, folderId, name, version);
186 
187         boolean converted = false;
188 
189         if (Validator.isNotNull(targetExtension)) {
190             String id = DocumentConversionUtil.getTempFileId(
191                 fileEntry.getFileEntryId(), version);
192 
193             String sourceExtension = FileUtil.getExtension(fileName);
194 
195             InputStream convertedIS = DocumentConversionUtil.convert(
196                 id, is, sourceExtension, targetExtension);
197 
198             if ((convertedIS != null) && (convertedIS != is)) {
199                 fileName = FileUtil.stripExtension(fileName).concat(
200                     StringPool.PERIOD).concat(targetExtension);
201 
202                 is = convertedIS;
203 
204                 converted = true;
205             }
206         }
207 
208         int contentLength = 0;
209 
210         if (!converted) {
211             if (DLUtil.compareVersions(version, fileEntry.getVersion()) >= 0) {
212                 contentLength = fileEntry.getSize();
213             }
214             else {
215                 DLFileVersion fileVersion =
216                     DLFileVersionLocalServiceUtil.getFileVersion(
217                         groupId, folderId, name, version);
218 
219                 contentLength = fileVersion.getSize();
220             }
221         }
222 
223         String contentType = MimeTypesUtil.getContentType(fileName);
224 
225         ServletResponseUtil.sendFile(
226             response, fileName, is, contentLength, contentType);
227     }
228 
229 }