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