001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.portletconfiguration.action;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.ConfigurationAction;
020    import com.liferay.portal.kernel.portlet.ResourceServingConfigurationAction;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.service.PortletLocalServiceUtil;
029    import com.liferay.portal.service.permission.PortletPermissionUtil;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.PortletPreferencesFactoryUtil;
035    import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.PortletPreferences;
041    import javax.portlet.PortletRequest;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    import javax.portlet.ResourceRequest;
045    import javax.portlet.ResourceResponse;
046    
047    import javax.servlet.ServletContext;
048    
049    import org.apache.struts.action.ActionForm;
050    import org.apache.struts.action.ActionForward;
051    import org.apache.struts.action.ActionMapping;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     */
056    public class EditConfigurationAction extends PortletAction {
057    
058            public void processAction(
059                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
060                            ActionRequest actionRequest, ActionResponse actionResponse)
061                    throws Exception {
062    
063                    Portlet portlet = null;
064    
065                    try {
066                            portlet = getPortlet(actionRequest);
067                    }
068                    catch (PrincipalException pe) {
069                            SessionErrors.add(
070                                    actionRequest, PrincipalException.class.getName());
071    
072                            setForward(actionRequest, "portlet.portlet_configuration.error");
073    
074                            return;
075                    }
076    
077                    ConfigurationAction configurationAction = getConfigurationAction(
078                            portlet);
079    
080                    if (configurationAction != null) {
081                            configurationAction.processAction(
082                                    portletConfig, actionRequest, actionResponse);
083                    }
084            }
085    
086            public ActionForward render(
087                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
088                            RenderRequest renderRequest, RenderResponse renderResponse)
089                    throws Exception {
090    
091                    Portlet portlet = null;
092    
093                    try {
094                            portlet = getPortlet(renderRequest);
095                    }
096                    catch (PrincipalException pe) {
097                            SessionErrors.add(
098                                    renderRequest, PrincipalException.class.getName());
099    
100                            return mapping.findForward("portlet.portlet_configuration.error");
101                    }
102    
103                    renderResponse.setTitle(getTitle(portlet, renderRequest));
104    
105                    ConfigurationAction configurationAction = getConfigurationAction(
106                            portlet);
107    
108                    if (configurationAction != null) {
109                            String path = configurationAction.render(
110                                    portletConfig, renderRequest, renderResponse);
111    
112                            if (_log.isDebugEnabled()) {
113                                    _log.debug("Configuration action returned render path " + path);
114                            }
115    
116                            if (Validator.isNotNull(path)) {
117                                    renderRequest.setAttribute(
118                                            WebKeys.CONFIGURATION_ACTION_PATH, path);
119                            }
120                            else {
121                                    _log.error("Configuration action returned a null path");
122                            }
123                    }
124    
125                    return mapping.findForward(getForward(
126                            renderRequest, "portlet.portlet_configuration.edit_configuration"));
127            }
128    
129            public void serveResource(
130                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
131                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
132                    throws Exception {
133    
134                    Portlet portlet = null;
135    
136                    try {
137                            portlet = getPortlet(resourceRequest);
138                    }
139                    catch (PrincipalException pe) {
140                            return;
141                    }
142    
143                    ResourceServingConfigurationAction resourceServingConfigurationAction =
144                            (ResourceServingConfigurationAction)getConfigurationAction(
145                                    portlet);
146    
147                    if (resourceServingConfigurationAction != null) {
148                            resourceServingConfigurationAction.serveResource(
149                                    portletConfig, resourceRequest, resourceResponse);
150                    }
151            }
152    
153            protected ConfigurationAction getConfigurationAction(Portlet portlet)
154                    throws Exception {
155    
156                    if (portlet == null) {
157                            return null;
158                    }
159    
160                    ConfigurationAction configurationAction =
161                            portlet.getConfigurationActionInstance();
162    
163                    if (configurationAction == null) {
164                            _log.error(
165                                    "Configuration action for portlet " + portlet.getPortletId() +
166                                            " is null");
167                    }
168    
169                    return configurationAction;
170            }
171    
172            protected Portlet getPortlet(PortletRequest portletRequest)
173                    throws Exception {
174    
175                    long companyId = PortalUtil.getCompanyId(portletRequest);
176    
177                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
178                            WebKeys.THEME_DISPLAY);
179    
180                    PermissionChecker permissionChecker =
181                            themeDisplay.getPermissionChecker();
182    
183                    String portletId = ParamUtil.getString(
184                            portletRequest, "portletResource");
185    
186                    if (!PortletPermissionUtil.contains(
187                                    permissionChecker, themeDisplay.getPlid(), portletId,
188                                    ActionKeys.CONFIGURATION)) {
189    
190                            throw new PrincipalException();
191                    }
192    
193                    return PortletLocalServiceUtil.getPortletById(companyId, portletId);
194            }
195    
196            protected String getTitle(Portlet portlet, RenderRequest renderRequest)
197                    throws Exception {
198    
199                    ServletContext servletContext =
200                            (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
201    
202                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
203                            WebKeys.THEME_DISPLAY);
204    
205                    PortletPreferences portletSetup =
206                            PortletPreferencesFactoryUtil.getPortletSetup(
207                                    renderRequest, portlet.getPortletId());
208    
209                    String title = PortletConfigurationUtil.getPortletTitle(
210                            portletSetup, themeDisplay.getLanguageId());
211    
212                    if (Validator.isNull(title)) {
213                            title = PortalUtil.getPortletTitle(
214                                    portlet, servletContext, themeDisplay.getLocale());
215                    }
216    
217                    return title;
218            }
219    
220            private static Log _log = LogFactoryUtil.getLog(
221                    EditConfigurationAction.class);
222    
223    }