001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.repository.model.FileEntry;
018    import com.liferay.portal.kernel.repository.model.FileVersion;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.MimeTypesUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.security.permission.ActionKeys;
030    import com.liferay.portal.security.permission.PermissionChecker;
031    import com.liferay.portal.service.UserLocalServiceUtil;
032    import com.liferay.portal.struts.ActionConstants;
033    import com.liferay.portal.struts.PortletAction;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.Portal;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portal.util.WebKeys;
038    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
039    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
040    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
041    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
042    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
043    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
044    import com.liferay.portlet.documentlibrary.util.DLUtil;
045    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
046    
047    import java.io.File;
048    import java.io.FileInputStream;
049    import java.io.IOException;
050    import java.io.InputStream;
051    
052    import java.util.List;
053    
054    import javax.portlet.ActionRequest;
055    import javax.portlet.ActionResponse;
056    import javax.portlet.PortletConfig;
057    
058    import javax.servlet.ServletException;
059    import javax.servlet.http.HttpServletRequest;
060    import javax.servlet.http.HttpServletResponse;
061    
062    import org.apache.struts.action.ActionForm;
063    import org.apache.struts.action.ActionForward;
064    import org.apache.struts.action.ActionMapping;
065    
066    /**
067     * @author Brian Wing Shun Chan
068     * @author Jorge Ferrer
069     * @author Charles May
070     * @author Bruno Farache
071     */
072    public class GetFileAction extends PortletAction {
073    
074            @Override
075            public void processAction(
076                            ActionMapping actionMapping, ActionForm actionForm,
077                            PortletConfig portletConfig, ActionRequest actionRequest,
078                            ActionResponse actionResponse)
079                    throws Exception {
080    
081                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
082                            actionRequest);
083                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
084                            actionResponse);
085    
086                    try {
087                            long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
088    
089                            long folderId = ParamUtil.getLong(actionRequest, "folderId");
090                            String name = ParamUtil.getString(actionRequest, "name");
091                            String title = ParamUtil.getString(actionRequest, "title");
092                            String version = ParamUtil.getString(actionRequest, "version");
093    
094                            long fileShortcutId = ParamUtil.getLong(
095                                    actionRequest, "fileShortcutId");
096    
097                            String uuid = ParamUtil.getString(actionRequest, "uuid");
098    
099                            String targetExtension = ParamUtil.getString(
100                                    actionRequest, "targetExtension");
101    
102                            ThemeDisplay themeDisplay =
103                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
104    
105                            long groupId = ParamUtil.getLong(
106                                    actionRequest, "groupId", themeDisplay.getScopeGroupId());
107    
108                            getFile(
109                                    fileEntryId, folderId, name, title, version, fileShortcutId,
110                                    uuid, groupId, targetExtension, request, response);
111    
112                            setForward(actionRequest, ActionConstants.COMMON_NULL);
113                    }
114                    catch (NoSuchFileEntryException nsfee) {
115                            PortalUtil.sendError(
116                                    HttpServletResponse.SC_NOT_FOUND, nsfee, actionRequest,
117                                    actionResponse);
118                    }
119                    catch (PrincipalException pe) {
120                            processPrincipalException(pe, request, response);
121                    }
122                    catch (Exception e) {
123                            PortalUtil.sendError(e, actionRequest, actionResponse);
124                    }
125            }
126    
127            @Override
128            public ActionForward strutsExecute(
129                            ActionMapping actionMapping, ActionForm actionForm,
130                            HttpServletRequest request, HttpServletResponse response)
131                    throws Exception {
132    
133                    try {
134                            long fileEntryId = ParamUtil.getLong(request, "fileEntryId");
135    
136                            long folderId = ParamUtil.getLong(request, "folderId");
137                            String name = ParamUtil.getString(request, "name");
138                            String title = ParamUtil.getString(request, "title");
139                            String version = ParamUtil.getString(request, "version");
140    
141                            long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
142    
143                            String uuid = ParamUtil.getString(request, "uuid");
144    
145                            String targetExtension = ParamUtil.getString(
146                                    request, "targetExtension");
147    
148                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
149                                    WebKeys.THEME_DISPLAY);
150    
151                            long groupId = ParamUtil.getLong(
152                                    request, "groupId", themeDisplay.getScopeGroupId());
153    
154                            getFile(
155                                    fileEntryId, folderId, name, title, version, fileShortcutId,
156                                    uuid, groupId, targetExtension, request, response);
157    
158                            return null;
159                    }
160                    catch (PrincipalException pe) {
161                            processPrincipalException(pe, request, response);
162    
163                            return null;
164                    }
165                    catch (Exception e) {
166                            PortalUtil.sendError(e, request, response);
167    
168                            return null;
169                    }
170            }
171    
172            protected void getFile(
173                            long fileEntryId, long folderId, String name, String title,
174                            String version, long fileShortcutId, String uuid, long groupId,
175                            String targetExtension, HttpServletRequest request,
176                            HttpServletResponse response)
177                    throws Exception {
178    
179                    if (name.startsWith("DLFE-")) {
180                            name = name.substring(5);
181                    }
182    
183                    name = FileUtil.stripExtension(name);
184    
185                    FileEntry fileEntry = null;
186    
187                    if (Validator.isNotNull(uuid) && (groupId > 0)) {
188                            fileEntry = DLAppServiceUtil.getFileEntryByUuidAndGroupId(
189                                    uuid, groupId);
190    
191                            folderId = fileEntry.getFolderId();
192                    }
193    
194                    if (fileEntryId > 0) {
195                            fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);
196                    }
197                    else if (fileShortcutId <= 0) {
198                            if (Validator.isNotNull(title)) {
199                                    fileEntry = DLAppServiceUtil.getFileEntry(
200                                            groupId, folderId, title);
201                            }
202                            else if (Validator.isNotNull(name)) {
203                                    DLFileEntry dlFileEntry =
204                                            DLFileEntryLocalServiceUtil.fetchFileEntryByName(
205                                                    groupId, folderId, name);
206    
207                                    if (dlFileEntry == null) {
208    
209                                            // LPS-30374
210    
211                                            List<DLFileEntry> dlFileEntries =
212                                                    DLFileEntryLocalServiceUtil.getFileEntries(
213                                                            folderId, name);
214    
215                                            if (!dlFileEntries.isEmpty()) {
216                                                    dlFileEntry = dlFileEntries.get(0);
217                                            }
218                                    }
219    
220                                    if (dlFileEntry != null) {
221                                            ThemeDisplay themeDisplay =
222                                                    (ThemeDisplay)request.getAttribute(
223                                                            WebKeys.THEME_DISPLAY);
224    
225                                            PermissionChecker permissionChecker =
226                                                    themeDisplay.getPermissionChecker();
227    
228                                            DLFileEntryPermission.check(
229                                                    permissionChecker, dlFileEntry, ActionKeys.VIEW);
230    
231                                            fileEntry = new LiferayFileEntry(dlFileEntry);
232                                    }
233                            }
234                    }
235                    else {
236                            DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
237                                    fileShortcutId);
238    
239                            fileEntryId = fileShortcut.getToFileEntryId();
240    
241                            fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);
242                    }
243    
244                    if (Validator.isNull(version)) {
245                            if ((fileEntry != null) &&
246                                    Validator.isNotNull(fileEntry.getVersion())) {
247    
248                                    version = fileEntry.getVersion();
249                            }
250                            else {
251                                    throw new NoSuchFileEntryException();
252                            }
253                    }
254    
255                    FileVersion fileVersion = fileEntry.getFileVersion(version);
256    
257                    InputStream is = fileVersion.getContentStream(true);
258                    String fileName = fileVersion.getTitle();
259                    long contentLength = fileVersion.getSize();
260                    String contentType = fileVersion.getMimeType();
261    
262                    if (Validator.isNotNull(targetExtension)) {
263                            String id = DLUtil.getTempFileId(
264                                    fileEntry.getFileEntryId(), version);
265    
266                            String sourceExtension = fileVersion.getExtension();
267    
268                            if (!fileName.endsWith(StringPool.PERIOD + sourceExtension)) {
269                                    fileName += StringPool.PERIOD + sourceExtension;
270                            }
271    
272                            File convertedFile = DocumentConversionUtil.convert(
273                                    id, is, sourceExtension, targetExtension);
274    
275                            if (convertedFile != null) {
276                                    fileName = FileUtil.stripExtension(fileName).concat(
277                                            StringPool.PERIOD).concat(targetExtension);
278                                    is = new FileInputStream(convertedFile);
279                                    contentLength = convertedFile.length();
280                                    contentType = MimeTypesUtil.getContentType(fileName);
281                            }
282                    }
283    
284                    ServletResponseUtil.sendFile(
285                            request, response, fileName, is, contentLength, contentType);
286            }
287    
288            @Override
289            protected boolean isCheckMethodOnProcessAction() {
290                    return _CHECK_METHOD_ON_PROCESS_ACTION;
291            }
292    
293            protected void processPrincipalException(
294                            Throwable t, HttpServletRequest request,
295                            HttpServletResponse response)
296                    throws IOException, ServletException {
297    
298                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
299                            WebKeys.THEME_DISPLAY);
300    
301                    PermissionChecker permissionChecker =
302                            themeDisplay.getPermissionChecker();
303    
304                    long userId = permissionChecker.getUserId();
305    
306                    User user = null;
307    
308                    try {
309                            user = UserLocalServiceUtil.getUser(userId);
310                    }
311                    catch (Exception e) {
312                    }
313    
314                    if ((user != null) && !user.isDefaultUser()) {
315                            PortalUtil.sendError(
316                                    HttpServletResponse.SC_UNAUTHORIZED, (Exception)t, request,
317                                    response);
318    
319                            return;
320                    }
321    
322                    String redirect =
323                            request.getContextPath() + Portal.PATH_MAIN + "/portal/login";
324    
325                    String currentURL = PortalUtil.getCurrentURL(request);
326    
327                    redirect = HttpUtil.addParameter(redirect, "redirect", currentURL);
328    
329                    response.sendRedirect(redirect);
330            }
331    
332            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
333    
334    }