001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
149                                    portlet);
150    
151                    if (resourceServingConfigurationAction != null) {
152                            resourceServingConfigurationAction.serveResource(
153                                    portletConfig, resourceRequest, resourceResponse);
154                    }
155            }
156    
157            protected ConfigurationAction getConfigurationAction(Portlet portlet)
158                    throws Exception {
159    
160                    if (portlet == null) {
161                            return null;
162                    }
163    
164                    ConfigurationAction configurationAction =
165                            portlet.getConfigurationActionInstance();
166    
167                    if (configurationAction == null) {
168                            _log.error(
169                                    "Configuration action for portlet " + portlet.getPortletId() +
170                                            " is null");
171                    }
172    
173                    return configurationAction;
174            }
175    
176            protected Portlet getPortlet(PortletRequest portletRequest)
177                    throws Exception {
178    
179                    long companyId = PortalUtil.getCompanyId(portletRequest);
180    
181                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
182                            WebKeys.THEME_DISPLAY);
183    
184                    PermissionChecker permissionChecker =
185                            themeDisplay.getPermissionChecker();
186    
187                    String portletId = ParamUtil.getString(
188                            portletRequest, "portletResource");
189    
190                    if (!PortletPermissionUtil.contains(
191                                    permissionChecker, themeDisplay.getLayout(), portletId,
192                                    ActionKeys.CONFIGURATION)) {
193    
194                            throw new PrincipalException();
195                    }
196    
197                    return PortletLocalServiceUtil.getPortletById(companyId, portletId);
198            }
199    
200            protected String getTitle(Portlet portlet, RenderRequest renderRequest)
201                    throws Exception {
202    
203                    ServletContext servletContext =
204                            (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
205    
206                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
207                            WebKeys.THEME_DISPLAY);
208    
209                    PortletPreferences portletSetup =
210                            PortletPreferencesFactoryUtil.getPortletSetup(
211                                    renderRequest, portlet.getPortletId());
212    
213                    String title = PortletConfigurationUtil.getPortletTitle(
214                            portletSetup, themeDisplay.getLanguageId());
215    
216                    if (Validator.isNull(title)) {
217                            title = PortalUtil.getPortletTitle(
218                                    portlet, servletContext, themeDisplay.getLocale());
219                    }
220    
221                    return title;
222            }
223    
224            private static Log _log = LogFactoryUtil.getLog(
225                    EditConfigurationAction.class);
226    
227    }