1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.communities.action;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.service.LayoutServiceUtil;
29  import com.liferay.portal.struts.ActionConstants;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portlet.ActionResponseImpl;
32  import com.liferay.util.servlet.ServletResponseUtil;
33  import com.liferay.util.servlet.SessionErrors;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import javax.servlet.http.HttpServletResponse;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="ExportPagesAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Alexander Chow
53   * @author Raymond Augé
54   *
55   */
56  public class ExportPagesAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig config,
60              ActionRequest req, ActionResponse res)
61          throws Exception {
62  
63          try {
64              long groupId = ParamUtil.getLong(req, "groupId");
65              boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
66              String fileName = ParamUtil.getString(req, "exportFileName");
67  
68              byte[] byteArray = LayoutServiceUtil.exportLayouts(
69                  groupId, privateLayout, req.getParameterMap());
70  
71              HttpServletResponse httpRes =
72                  ((ActionResponseImpl)res).getHttpServletResponse();
73  
74              ServletResponseUtil.sendFile(httpRes, fileName, byteArray);
75  
76              setForward(req, ActionConstants.COMMON_NULL);
77          }
78          catch (Exception e) {
79              _log.error(e, e);
80          }
81      }
82  
83      public ActionForward render(
84              ActionMapping mapping, ActionForm form, PortletConfig config,
85              RenderRequest req, RenderResponse res)
86          throws Exception {
87  
88          try {
89              ActionUtil.getGroup(req);
90          }
91          catch (Exception e) {
92              if (e instanceof NoSuchGroupException ||
93                  e instanceof PrincipalException) {
94  
95                  SessionErrors.add(req, e.getClass().getName());
96  
97                  return mapping.findForward("portlet.communities.error");
98              }
99              else {
100                 throw e;
101             }
102         }
103 
104         return mapping.findForward(
105             getForward(req, "portlet.communities.export_pages"));
106     }
107 
108     private static Log _log = LogFactory.getLog(ExportPagesAction.class);
109 
110 }