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