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.FileUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.struts.ActionConstants;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
33  import com.liferay.util.servlet.ServletResponseUtil;
34  
35  import java.io.BufferedInputStream;
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.InputStream;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  
44  import javax.servlet.http.HttpServletResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="ExportEventsAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Michael Young
53   * @author Bruno Farache
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class ExportEventsAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          File file = null;
65          InputStream is = null;
66  
67          try {
68              ThemeDisplay themeDisplay =
69                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
70  
71              long eventId = ParamUtil.getLong(actionRequest, "eventId");
72  
73              String exportFileName = ParamUtil.getString(
74                  actionRequest, "exportFileName");
75  
76              if (eventId > 0) {
77                  file = CalEventServiceUtil.exportEvent(eventId);
78              }
79              else {
80                  file = CalEventServiceUtil.exportGroupEvents(
81                      themeDisplay.getScopeGroupId(), exportFileName);
82              }
83  
84              is = new BufferedInputStream(new FileInputStream(file));
85  
86              HttpServletResponse response = PortalUtil.getHttpServletResponse(
87                  actionResponse);
88  
89              ServletResponseUtil.sendFile(
90                  response, file.getName(), is, ContentTypes.TEXT_CALENDAR);
91  
92              setForward(actionRequest, ActionConstants.COMMON_NULL);
93          }
94          catch (Exception e) {
95              _log.error(e, e);
96          }
97          finally {
98              ServletResponseUtil.cleanUp(is);
99  
100             FileUtil.delete(file);
101         }
102     }
103 
104     protected boolean isCheckMethodOnProcessAction() {
105         return _CHECK_METHOD_ON_PROCESS_ACTION;
106     }
107 
108     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
109 
110     private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
111 
112 }