1   /**
2    * Copyright (c) 2000-2007 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.kernel.portlet.ConfigurationAction;
26  import com.liferay.portal.kernel.security.permission.ActionKeys;
27  import com.liferay.portal.kernel.security.permission.PermissionChecker;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Portlet;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.PortletLocalServiceUtil;
33  import com.liferay.portal.service.permission.PortletPermissionUtil;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.util.servlet.SessionErrors;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.PortletRequest;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import javax.servlet.ServletContext;
48  
49  import org.apache.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="EditConfigurationAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class EditConfigurationAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig config,
65              ActionRequest req, ActionResponse res)
66          throws Exception {
67  
68          Portlet portlet = null;
69  
70          try {
71              portlet = getPortlet(req);
72          }
73          catch (PrincipalException pe) {
74              SessionErrors.add(req, PrincipalException.class.getName());
75  
76              setForward(req, "portlet.portlet_configuration.error");
77          }
78  
79          ConfigurationAction configurationAction = getConfigurationAction(
80              portlet);
81  
82          if (configurationAction != null) {
83              configurationAction.processAction(config, req, res);
84          }
85      }
86  
87      public ActionForward render(
88              ActionMapping mapping, ActionForm form, PortletConfig config,
89              RenderRequest req, RenderResponse res)
90          throws Exception {
91  
92          Portlet portlet = null;
93  
94          try {
95              portlet = getPortlet(req);
96          }
97          catch (PrincipalException pe) {
98              SessionErrors.add(req, PrincipalException.class.getName());
99  
100             return mapping.findForward("portlet.portlet_configuration.error");
101         }
102 
103         ServletContext ctx =
104             (ServletContext)req.getAttribute(WebKeys.CTX);
105 
106         ThemeDisplay themeDisplay =
107             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
108 
109         res.setTitle(
110             PortalUtil.getPortletTitle(portlet, ctx, themeDisplay.getLocale()));
111 
112         ConfigurationAction configurationAction = getConfigurationAction(
113             portlet);
114 
115         if (configurationAction != null) {
116             String path = configurationAction.render(config, req, res);
117 
118             if (_log.isDebugEnabled()) {
119                 _log.debug("Configuration action returned render path " + path);
120             }
121 
122             if (Validator.isNotNull(path)) {
123                 req.setAttribute(WebKeys.CONFIGURATION_ACTION_PATH, path);
124             }
125             else {
126                 _log.error("Configuration action returned a null path");
127             }
128         }
129 
130         return mapping.findForward(getForward(
131             req, "portlet.portlet_configuration.edit_configuration"));
132     }
133 
134     protected ConfigurationAction getConfigurationAction(Portlet portlet)
135         throws Exception {
136 
137         ConfigurationAction configurationAction =
138             portlet.getConfigurationActionInstance();
139 
140         if (configurationAction == null) {
141             _log.error(
142                 "Configuration action for portlet " + portlet.getPortletId() +
143                     " is null");
144         }
145 
146         return configurationAction;
147     }
148 
149     protected Portlet getPortlet(PortletRequest req) throws Exception {
150         long companyId = PortalUtil.getCompanyId(req);
151 
152         ThemeDisplay themeDisplay =
153             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
154 
155         PermissionChecker permissionChecker =
156             themeDisplay.getPermissionChecker();
157 
158         String portletId = ParamUtil.getString(req, "portletResource");
159 
160         if (!PortletPermissionUtil.contains(
161                 permissionChecker, themeDisplay.getPlid(), portletId,
162                 ActionKeys.CONFIGURATION)) {
163 
164             throw new PrincipalException();
165         }
166 
167         return PortletLocalServiceUtil.getPortletById(companyId, portletId);
168     }
169 
170     private static Log _log = LogFactory.getLog(EditConfigurationAction.class);
171 
172 }