001    /**
002     * Copyright (c) 2000-2012 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     * @author Raymond Augé
056     */
057    public class EditConfigurationAction extends PortletAction {
058    
059            @Override
060            public void processAction(
061                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062                            ActionRequest actionRequest, ActionResponse actionResponse)
063                    throws Exception {
064    
065                    Portlet portlet = null;
066    
067                    try {
068                            portlet = getPortlet(actionRequest);
069                    }
070                    catch (PrincipalException pe) {
071                            SessionErrors.add(
072                                    actionRequest, PrincipalException.class.getName());
073    
074                            setForward(actionRequest, "portlet.portlet_configuration.error");
075    
076                            return;
077                    }
078    
079                    ConfigurationAction configurationAction = getConfigurationAction(
080                            portlet);
081    
082                    if (configurationAction != null) {
083                            configurationAction.processAction(
084                                    portletConfig, actionRequest, actionResponse);
085                    }
086            }
087    
088            @Override
089            public ActionForward render(
090                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
091                            RenderRequest renderRequest, RenderResponse renderResponse)
092                    throws Exception {
093    
094                    Portlet portlet = null;
095    
096                    try {
097                            portlet = getPortlet(renderRequest);
098                    }
099                    catch (PrincipalException pe) {
100                            SessionErrors.add(
101                                    renderRequest, PrincipalException.class.getName());
102    
103                            return mapping.findForward("portlet.portlet_configuration.error");
104                    }
105    
106                    renderResponse.setTitle(getTitle(portlet, renderRequest));
107    
108                    ConfigurationAction configurationAction = getConfigurationAction(
109                            portlet);
110    
111                    if (configurationAction != null) {
112                            String path = configurationAction.render(
113                                    portletConfig, renderRequest, renderResponse);
114    
115                            if (_log.isDebugEnabled()) {
116                                    _log.debug("Configuration action returned render path " + path);
117                            }
118    
119                            if (Validator.isNotNull(path)) {
120                                    renderRequest.setAttribute(
121                                            WebKeys.CONFIGURATION_ACTION_PATH, path);
122                            }
123                            else {
124                                    _log.error("Configuration action returned a null path");
125                            }
126                    }
127    
128                    return mapping.findForward(
129                            getForward(
130                                    renderRequest,
131                                    "portlet.portlet_configuration.edit_configuration"));
132            }
133    
134            @Override
135            public void serveResource(
136                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
137                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
138                    throws Exception {
139    
140                    Portlet portlet = null;
141    
142                    try {
143                            portlet = getPortlet(resourceRequest);
144                    }
145                    catch (PrincipalException pe) {
146                            return;
147                    }
148    
149                    ResourceServingConfigurationAction resourceServingConfigurationAction =
150                            (ResourceServingConfigurationAction)getConfigurationAction(portlet);
151    
152                    if (resourceServingConfigurationAction != null) {
153                            resourceServingConfigurationAction.serveResource(
154                                    portletConfig, resourceRequest, resourceResponse);
155                    }
156            }
157    
158            protected ConfigurationAction getConfigurationAction(Portlet portlet)
159                    throws Exception {
160    
161                    if (portlet == null) {
162                            return null;
163                    }
164    
165                    ConfigurationAction configurationAction =
166                            portlet.getConfigurationActionInstance();
167    
168                    if (configurationAction == null) {
169                            _log.error(
170                                    "Configuration action for portlet " + portlet.getPortletId() +
171                                            " is null");
172                    }
173    
174                    return configurationAction;
175            }
176    
177            protected Portlet getPortlet(PortletRequest portletRequest)
178                    throws Exception {
179    
180                    long companyId = PortalUtil.getCompanyId(portletRequest);
181    
182                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
183                            WebKeys.THEME_DISPLAY);
184    
185                    PermissionChecker permissionChecker =
186                            themeDisplay.getPermissionChecker();
187    
188                    String portletId = ParamUtil.getString(
189                            portletRequest, "portletResource");
190    
191                    if (!PortletPermissionUtil.contains(
192                                    permissionChecker, themeDisplay.getLayout(), portletId,
193                                    ActionKeys.CONFIGURATION)) {
194    
195                            throw new PrincipalException();
196                    }
197    
198                    return PortletLocalServiceUtil.getPortletById(companyId, portletId);
199            }
200    
201            protected String getTitle(Portlet portlet, RenderRequest renderRequest)
202                    throws Exception {
203    
204                    ServletContext servletContext =
205                            (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
206    
207                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
208                            WebKeys.THEME_DISPLAY);
209    
210                    PortletPreferences portletSetup =
211                            PortletPreferencesFactoryUtil.getPortletSetup(
212                                    renderRequest, portlet.getPortletId());
213    
214                    String title = PortletConfigurationUtil.getPortletTitle(
215                            portletSetup, themeDisplay.getLanguageId());
216    
217                    if (Validator.isNull(title)) {
218                            title = PortalUtil.getPortletTitle(
219                                    portlet, servletContext, themeDisplay.getLocale());
220                    }
221    
222                    return title;
223            }
224    
225            private static Log _log = LogFactoryUtil.getLog(
226                    EditConfigurationAction.class);
227    
228    }