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(getForward(
129                            renderRequest, "portlet.portlet_configuration.edit_configuration"));
130            }
131    
132            @Override
133            public void serveResource(
134                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
135                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
136                    throws Exception {
137    
138                    Portlet portlet = null;
139    
140                    try {
141                            portlet = getPortlet(resourceRequest);
142                    }
143                    catch (PrincipalException pe) {
144                            return;
145                    }
146    
147                    ResourceServingConfigurationAction resourceServingConfigurationAction =
148                            (ResourceServingConfigurationAction)getConfigurationAction(portlet);
149    
150                    if (resourceServingConfigurationAction != null) {
151                            resourceServingConfigurationAction.serveResource(
152                                    portletConfig, resourceRequest, resourceResponse);
153                    }
154            }
155    
156            protected ConfigurationAction getConfigurationAction(Portlet portlet)
157                    throws Exception {
158    
159                    if (portlet == null) {
160                            return null;
161                    }
162    
163                    ConfigurationAction configurationAction =
164                            portlet.getConfigurationActionInstance();
165    
166                    if (configurationAction == null) {
167                            _log.error(
168                                    "Configuration action for portlet " + portlet.getPortletId() +
169                                            " is null");
170                    }
171    
172                    return configurationAction;
173            }
174    
175            protected Portlet getPortlet(PortletRequest portletRequest)
176                    throws Exception {
177    
178                    long companyId = PortalUtil.getCompanyId(portletRequest);
179    
180                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
181                            WebKeys.THEME_DISPLAY);
182    
183                    PermissionChecker permissionChecker =
184                            themeDisplay.getPermissionChecker();
185    
186                    String portletId = ParamUtil.getString(
187                            portletRequest, "portletResource");
188    
189                    if (!PortletPermissionUtil.contains(
190                                    permissionChecker, themeDisplay.getLayout(), portletId,
191                                    ActionKeys.CONFIGURATION)) {
192    
193                            throw new PrincipalException();
194                    }
195    
196                    return PortletLocalServiceUtil.getPortletById(companyId, portletId);
197            }
198    
199            protected String getTitle(Portlet portlet, RenderRequest renderRequest)
200                    throws Exception {
201    
202                    ServletContext servletContext =
203                            (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
204    
205                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
206                            WebKeys.THEME_DISPLAY);
207    
208                    PortletPreferences portletSetup =
209                            PortletPreferencesFactoryUtil.getPortletSetup(
210                                    renderRequest, portlet.getPortletId());
211    
212                    String title = PortletConfigurationUtil.getPortletTitle(
213                            portletSetup, themeDisplay.getLanguageId());
214    
215                    if (Validator.isNull(title)) {
216                            title = PortalUtil.getPortletTitle(
217                                    portlet, servletContext, themeDisplay.getLocale());
218                    }
219    
220                    return title;
221            }
222    
223            private static Log _log = LogFactoryUtil.getLog(
224                    EditConfigurationAction.class);
225    
226    }