1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.portletconfiguration.action;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.ConfigurationAction;
25  import com.liferay.portal.kernel.portlet.ResourceServingConfigurationAction;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Portlet;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portal.security.permission.PermissionChecker;
33  import com.liferay.portal.service.PortletLocalServiceUtil;
34  import com.liferay.portal.service.permission.PortletPermissionUtil;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.PortletPreferencesFactoryUtil;
40  import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletPreferences;
46  import javax.portlet.PortletRequest;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  import javax.portlet.ResourceRequest;
50  import javax.portlet.ResourceResponse;
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              return;
83          }
84  
85          ConfigurationAction configurationAction = getConfigurationAction(
86              portlet);
87  
88          if (configurationAction != null) {
89              configurationAction.processAction(
90                  portletConfig, actionRequest, actionResponse);
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
96              RenderRequest renderRequest, RenderResponse renderResponse)
97          throws Exception {
98  
99          Portlet portlet = null;
100 
101         try {
102             portlet = getPortlet(renderRequest);
103         }
104         catch (PrincipalException pe) {
105             SessionErrors.add(
106                 renderRequest, PrincipalException.class.getName());
107 
108             return mapping.findForward("portlet.portlet_configuration.error");
109         }
110 
111         renderResponse.setTitle(getTitle(portlet, renderRequest));
112 
113         ConfigurationAction configurationAction = getConfigurationAction(
114             portlet);
115 
116         if (configurationAction != null) {
117             String path = configurationAction.render(
118                 portletConfig, renderRequest, renderResponse);
119 
120             if (_log.isDebugEnabled()) {
121                 _log.debug("Configuration action returned render path " + path);
122             }
123 
124             if (Validator.isNotNull(path)) {
125                 renderRequest.setAttribute(
126                     WebKeys.CONFIGURATION_ACTION_PATH, path);
127             }
128             else {
129                 _log.error("Configuration action returned a null path");
130             }
131         }
132 
133         return mapping.findForward(getForward(
134             renderRequest, "portlet.portlet_configuration.edit_configuration"));
135     }
136 
137     public void serveResource(
138             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
139             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
140         throws Exception {
141 
142         Portlet portlet = null;
143 
144         try {
145             portlet = getPortlet(resourceRequest);
146         }
147         catch (PrincipalException pe) {
148             return;
149         }
150 
151         ResourceServingConfigurationAction resourceServingConfigurationAction =
152             (ResourceServingConfigurationAction)getConfigurationAction(
153                 portlet);
154 
155         if (resourceServingConfigurationAction != null) {
156             resourceServingConfigurationAction.serveResource(
157                 portletConfig, resourceRequest, resourceResponse);
158         }
159     }
160 
161     protected ConfigurationAction getConfigurationAction(Portlet portlet)
162         throws Exception {
163 
164         if (portlet == null) {
165             return null;
166         }
167 
168         ConfigurationAction configurationAction =
169             portlet.getConfigurationActionInstance();
170 
171         if (configurationAction == null) {
172             _log.error(
173                 "Configuration action for portlet " + portlet.getPortletId() +
174                     " is null");
175         }
176 
177         return configurationAction;
178     }
179 
180     protected Portlet getPortlet(PortletRequest portletRequest)
181         throws Exception {
182 
183         long companyId = PortalUtil.getCompanyId(portletRequest);
184 
185         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
186             WebKeys.THEME_DISPLAY);
187 
188         PermissionChecker permissionChecker =
189             themeDisplay.getPermissionChecker();
190 
191         String portletId = ParamUtil.getString(
192             portletRequest, "portletResource");
193 
194         if (!PortletPermissionUtil.contains(
195                 permissionChecker, themeDisplay.getPlid(), portletId,
196                 ActionKeys.CONFIGURATION)) {
197 
198             throw new PrincipalException();
199         }
200 
201         return PortletLocalServiceUtil.getPortletById(companyId, portletId);
202     }
203 
204     protected String getTitle(Portlet portlet, RenderRequest renderRequest)
205         throws Exception {
206 
207         ServletContext servletContext =
208             (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
209 
210         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
211             WebKeys.THEME_DISPLAY);
212 
213         PortletPreferences portletSetup =
214             PortletPreferencesFactoryUtil.getPortletSetup(
215                 renderRequest, portlet.getPortletId());
216 
217         String title = PortletConfigurationUtil.getPortletTitle(
218             portletSetup, themeDisplay.getLanguageId());
219 
220         if (Validator.isNull(title)) {
221             title = PortalUtil.getPortletTitle(
222                 portlet, servletContext, themeDisplay.getLocale());
223         }
224 
225         return title;
226     }
227 
228     private static Log _log =
229         LogFactoryUtil.getLog(EditConfigurationAction.class);
230 
231 }