1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.userProperties.action;
37 import java.io.InputStream;
39 import javax.portlet.ActionRequest;
40 import javax.portlet.ActionResponse;
41 import javax.portlet.PortletConfig;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
45 import org.apache.struts.action.ActionForm;
46 import org.apache.struts.action.ActionForward;
47 import org.apache.struts.action.ActionMapping;
49 import com.liferay.portal.kernel.util.MimeTypesOverrideUtil;
50 import com.liferay.portal.kernel.util.ParamUtil;
51 import com.liferay.portal.model.CompanyConstants;
52 import com.liferay.portal.model.User;
53 import com.liferay.portal.struts.ActionConstants;
54 import com.liferay.portal.struts.PortletAction;
55 import com.liferay.portal.util.PortalUtil;
56 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
57 import com.pentila.entSavoie.servlet.ServletResponseServiceUtil;
60 * <a href="GetMessageAttachmentAction.java.html"><b><i>View Source</i></b></a>
62 * @author Brian Wing Shun Chan
65 public class GetUserArchiveAttachmentAction extends PortletAction {
67 public ActionForward strutsExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
70 long userId = ParamUtil.getLong(request, "userId");
71 String fileName = ParamUtil.getString(request, "attachment");
74 getFile(userId, fileName, response, request);
79 PortalUtil.sendError(e, request, response);
85 public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
88 long userId = ParamUtil.getLong(actionRequest, "userId");
89 String fileName = ParamUtil.getString(actionRequest, "attachment");
92 HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
93 HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
95 getFile(userId, fileName, response, request);
97 setForward(actionRequest, ActionConstants.COMMON_NULL);
100 PortalUtil.sendError(e, actionRequest, actionResponse);
105 protected void getFile(long userId, String fileName, HttpServletResponse response, HttpServletRequest request) throws Exception {
107 InputStream is = null;
110 User user = PortalUtil.getUser(request);
111 if(user.getUserId() != userId){
113 System.out.println("Trying to download an other user archive. User authenticated: "+ user.getUserId() + " User expected: " + userId);
116 String path = "exportDataENT/" + userId + "/" + fileName;
118 is = DLStoreUtil.getFileAsStream(user.getCompanyId(), CompanyConstants.SYSTEM, path);
120 int contentLength = (int) DLStoreUtil.getFileSize(user.getCompanyId(), CompanyConstants.SYSTEM, path);
124 String contentType = MimeTypesOverrideUtil.getContentType(fileName);
126 //System.out.println("contentType: " + contentType);
129 String forceContentDisposition = ParamUtil.getString(request, "contentDispo");
130 String fcd = forceContentDisposition == null ? "" : forceContentDisposition;
133 ServletResponseServiceUtil.sendFile(
134 response, fileName, is, contentLength, contentType, fcd);
145 protected boolean isCheckMethodOnProcessAction() {
146 return _CHECK_METHOD_ON_PROCESS_ACTION;
149 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;