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