1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.calendar.action;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ContentTypes;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.struts.ActionConstants;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
32  import com.liferay.util.servlet.ServletResponseUtil;
33  
34  import java.io.BufferedInputStream;
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.InputStream;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="ExportEventsAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Michael Young
52   * @author Bruno Farache
53   *
54   */
55  public class ExportEventsAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          InputStream is = null;
63  
64          try {
65              ThemeDisplay themeDisplay =
66                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
67  
68              long eventId = ParamUtil.getLong(actionRequest, "eventId");
69  
70              String exportFileName = ParamUtil.getString(
71                  actionRequest, "exportFileName");
72  
73              File file = null;
74  
75              if (eventId > 0) {
76                  file = CalEventServiceUtil.exportEvent(eventId);
77              }
78              else {
79                  file = CalEventServiceUtil.exportGroupEvents(
80                      themeDisplay.getPlid(), exportFileName);
81              }
82  
83              is = new BufferedInputStream(new FileInputStream(file));
84  
85              HttpServletResponse response = PortalUtil.getHttpServletResponse(
86                  actionResponse);
87  
88              ServletResponseUtil.sendFile(
89                  response, file.getName(), is, ContentTypes.TEXT_CALENDAR);
90  
91              setForward(actionRequest, ActionConstants.COMMON_NULL);
92          }
93          catch (Exception e) {
94              _log.error(e, e);
95          }
96          finally {
97              ServletResponseUtil.cleanUp(is);
98          }
99      }
100 
101     private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
102 
103 }