Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portlet.calendar.action; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.servlet.ServletResponseUtil; import com.liferay.portal.kernel.util.ContentTypes; import com.liferay.portal.kernel.util.FileUtil; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.struts.ActionConstants; import com.liferay.portal.struts.PortletAction; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.util.PortalUtil; import com.liferay.portal.util.WebKeys; import com.liferay.portlet.calendar.service.CalEventServiceUtil; import java.io.File; import java.io.FileInputStream; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * @author Michael Young * @author Bruno Farache * @author Brian Wing Shun Chan */ public class ExportEventsAction extends PortletAction { @Override public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { File file = null; try { ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); long eventId = ParamUtil.getLong(actionRequest, "eventId"); String exportFileName = ParamUtil.getString(actionRequest, "exportFileName"); if (Validator.isNull(exportFileName)) { exportFileName = "liferay.ics"; } else { exportFileName = FileUtil.getShortFileName(exportFileName); } if (eventId > 0) { file = CalEventServiceUtil.exportEvent(eventId); } else { file = CalEventServiceUtil.exportGroupEvents(themeDisplay.getScopeGroupId(), exportFileName); } HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse); ServletResponseUtil.sendFile(request, response, exportFileName, new FileInputStream(file), ContentTypes.TEXT_CALENDAR); setForward(actionRequest, ActionConstants.COMMON_NULL); } catch (Exception e) { _log.error(e, e); } finally { FileUtil.delete(file); } } @Override protected boolean isCheckMethodOnProcessAction() { return _CHECK_METHOD_ON_PROCESS_ACTION; } private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false; private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class); }