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.portletconfiguration.action;
24  
25  import com.liferay.portal.LayoutImportException;
26  import com.liferay.portal.NoSuchLayoutException;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.model.Portlet;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.LayoutServiceUtil;
32  import com.liferay.portal.struts.ActionConstants;
33  import com.liferay.portal.util.UploadRequestUtil;
34  import com.liferay.portlet.ActionResponseImpl;
35  import com.liferay.portlet.communities.util.StagingUtil;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  import com.liferay.util.servlet.SessionErrors;
38  import com.liferay.util.servlet.SessionMessages;
39  import com.liferay.util.servlet.UploadPortletRequest;
40  
41  import java.io.File;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import javax.servlet.http.HttpServletResponse;
50  
51  import org.apache.commons.logging.Log;
52  import org.apache.commons.logging.LogFactory;
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="ExportImportAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Jorge Ferrer
61   *
62   */
63  public class ExportImportAction extends EditConfigurationAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig config,
67              ActionRequest req, ActionResponse res)
68          throws Exception {
69  
70          Portlet portlet = null;
71  
72          try {
73              portlet = getPortlet(req);
74          }
75          catch (PrincipalException pe) {
76              SessionErrors.add(req, PrincipalException.class.getName());
77  
78              setForward(req, "portlet.portlet_configuration.error");
79          }
80  
81          String cmd = ParamUtil.getString(req, Constants.CMD);
82  
83          try {
84              if (cmd.equals("copy_from_live")) {
85                  StagingUtil.copyFromLive(req, portlet);
86  
87                  sendRedirect(req, res);
88              }
89              else if (cmd.equals("export")) {
90                  exportData(req, res, portlet);
91              }
92              else if (cmd.equals("import")) {
93                  importData(req, portlet);
94  
95                  sendRedirect(req, res);
96              }
97              else if (cmd.equals("publish_to_live")) {
98                  StagingUtil.publishToLive(req, portlet);
99  
100                 sendRedirect(req, res);
101             }
102         }
103         catch (Exception e) {
104             if (e instanceof NoSuchLayoutException ||
105                 e instanceof PrincipalException) {
106 
107                 SessionErrors.add(req, e.getClass().getName());
108 
109                 setForward(req, "portlet.portlet_configuration.error");
110             }
111             else {
112                 throw e;
113             }
114         }
115     }
116 
117     public ActionForward render(
118             ActionMapping mapping, ActionForm form, PortletConfig config,
119             RenderRequest req, RenderResponse res)
120         throws Exception {
121 
122         Portlet portlet = null;
123 
124         try {
125             portlet = getPortlet(req);
126         }
127         catch (PrincipalException pe) {
128             SessionErrors.add(req, PrincipalException.class.getName());
129 
130             return mapping.findForward("portlet.portlet_configuration.error");
131         }
132 
133         res.setTitle(getTitle(portlet, req));
134 
135         return mapping.findForward(getForward(
136             req, "portlet.portlet_configuration.export_import"));
137     }
138 
139     protected void exportData(
140             ActionRequest req, ActionResponse res, Portlet portlet)
141         throws Exception {
142 
143         try {
144             long plid = ParamUtil.getLong(req, "plid");
145             String fileName = ParamUtil.getString(req, "exportFileName");
146 
147             byte[] byteArray = LayoutServiceUtil.exportPortletInfo(
148                 plid, portlet.getPortletId(), req.getParameterMap());
149 
150             HttpServletResponse httpRes =
151                 ((ActionResponseImpl)res).getHttpServletResponse();
152 
153             ServletResponseUtil.sendFile(httpRes, fileName, byteArray);
154 
155             setForward(req, ActionConstants.COMMON_NULL);
156         }
157         catch (Exception e) {
158             _log.error(e, e);
159         }
160     }
161 
162     protected void importData(ActionRequest req, Portlet portlet)
163         throws Exception {
164 
165         try {
166             UploadPortletRequest uploadReq =
167                 UploadRequestUtil.getUploadPortletRequest(req);
168 
169             long plid = ParamUtil.getLong(uploadReq, "plid");
170             File file = uploadReq.getFile("importFileName");
171 
172             LayoutServiceUtil.importPortletInfo(
173                 plid, portlet.getPortletId(), req.getParameterMap(), file);
174 
175             SessionMessages.add(req, "request_processed");
176         }
177         catch (Exception e) {
178             _log.error(e, e);
179 
180             SessionErrors.add(req, LayoutImportException.class.getName());
181         }
182     }
183 
184     private static Log _log = LogFactory.getLog(ExportImportAction.class);
185 
186 }