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.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  public class GetFileAction extends PortletAction {
68  
69      public ActionForward strutsExecute(
70              ActionMapping mapping, ActionForm form, HttpServletRequest request,
71              HttpServletResponse response)
72          throws Exception {
73  
74          try {
75              long folderId = ParamUtil.getLong(request, "folderId");
76              String name = ParamUtil.getString(request, "name");
77              double version = ParamUtil.getDouble(request, "version");
78  
79              long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
80  
81              String uuid = ParamUtil.getString(request, "uuid");
82              long groupId = ParamUtil.getLong(request, "groupId");
83  
84              String targetExtension = ParamUtil.getString(
85                  request, "targetExtension");
86  
87              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
88                  WebKeys.THEME_DISPLAY);
89  
90              getFile(
91                  folderId, name, version, fileShortcutId, uuid, groupId,
92                  targetExtension, themeDisplay, request, response);
93  
94              return null;
95          }
96          catch (Exception e) {
97              PortalUtil.sendError(e, request, response);
98  
99              return null;
100         }
101     }
102 
103     public void processAction(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             ActionRequest actionRequest, ActionResponse actionResponse)
106         throws Exception {
107 
108         try {
109             long folderId = ParamUtil.getLong(actionRequest, "folderId");
110             String name = ParamUtil.getString(actionRequest, "name");
111             double version = ParamUtil.getDouble(actionRequest, "version");
112 
113             long fileShortcutId = ParamUtil.getLong(
114                 actionRequest, "fileShortcutId");
115 
116             String uuid = ParamUtil.getString(actionRequest, "uuid");
117             long groupId = ParamUtil.getLong(actionRequest, "groupId");
118 
119             String targetExtension = ParamUtil.getString(
120                 actionRequest, "targetExtension");
121 
122             ThemeDisplay themeDisplay =
123                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
124 
125             HttpServletRequest request = PortalUtil.getHttpServletRequest(
126                 actionRequest);
127             HttpServletResponse response = PortalUtil.getHttpServletResponse(
128                 actionResponse);
129 
130             getFile(
131                 folderId, name, version, fileShortcutId, uuid, groupId,
132                 targetExtension, themeDisplay, request, response);
133 
134             setForward(actionRequest, ActionConstants.COMMON_NULL);
135         }
136         catch (Exception e) {
137             PortalUtil.sendError(e, actionRequest, actionResponse);
138         }
139     }
140 
141     protected void getFile(
142             long folderId, String name, double version, long fileShortcutId,
143             String uuid, long groupId, String targetExtension,
144             ThemeDisplay themeDisplay, HttpServletRequest request,
145             HttpServletResponse response)
146         throws Exception {
147 
148         InputStream is = null;
149 
150         try {
151             long companyId = themeDisplay.getCompanyId();
152             long userId = themeDisplay.getUserId();
153 
154             DLFileEntry fileEntry = null;
155 
156             if (Validator.isNotNull(uuid) && (groupId > 0)) {
157                 try {
158                     fileEntry = DLFileEntryLocalServiceUtil.
159                         getFileEntryByUuidAndGroupId(
160                             uuid, groupId);
161 
162                     folderId = fileEntry.getFolderId();
163                     name = fileEntry.getName();
164                 }
165                 catch (Exception e) {
166                 }
167             }
168 
169             if (fileShortcutId <= 0) {
170                 DLFileEntryPermission.check(
171                     themeDisplay.getPermissionChecker(), folderId, name,
172                     ActionKeys.VIEW);
173             }
174             else {
175                 DLFileShortcut fileShortcut =
176                     DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
177 
178                 folderId = fileShortcut.getToFolderId();
179                 name = fileShortcut.getToName();
180             }
181 
182             if (fileEntry == null) {
183                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
184                     folderId, name);
185             }
186 
187             if (version == 0) {
188                 version = fileEntry.getVersion();
189             }
190 
191             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                 response, fileName, is, contentLength, contentType);
241         }
242         finally {
243             ServletResponseUtil.cleanUp(is);
244         }
245     }
246 
247     protected boolean isCheckMethodOnProcessAction() {
248         return _CHECK_METHOD_ON_PROCESS_ACTION;
249     }
250 
251     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
252 
253 }