001    /**
002     * Copyright (c) 2000-present 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.PortletLayoutListener;
021    import com.liferay.portal.kernel.portlet.ResourceServingConfigurationAction;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.Portlet;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.WebKeys;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    import javax.portlet.ResourceRequest;
037    import javax.portlet.ResourceResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Raymond Aug??
046     */
047    public class EditConfigurationAction extends PortletAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055    
056                    Portlet portlet = null;
057    
058                    try {
059                            portlet = ActionUtil.getPortlet(actionRequest);
060                    }
061                    catch (PrincipalException pe) {
062                            SessionErrors.add(
063                                    actionRequest, PrincipalException.class.getName());
064    
065                            setForward(actionRequest, "portlet.portlet_configuration.error");
066    
067                            return;
068                    }
069    
070                    actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, null);
071    
072                    ConfigurationAction configurationAction = getConfigurationAction(
073                            portlet);
074    
075                    if (configurationAction == null) {
076                            return;
077                    }
078    
079                    configurationAction.processAction(
080                            portletConfig, actionRequest, actionResponse);
081    
082                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
083                            WebKeys.THEME_DISPLAY);
084    
085                    Layout layout = themeDisplay.getLayout();
086    
087                    PortletLayoutListener portletLayoutListener =
088                            portlet.getPortletLayoutListenerInstance();
089    
090                    if (portletLayoutListener != null) {
091                            portletLayoutListener.onSetup(
092                                    portlet.getPortletId(), layout.getPlid());
093                    }
094            }
095    
096            @Override
097            public ActionForward render(
098                            ActionMapping actionMapping, ActionForm actionForm,
099                            PortletConfig portletConfig, RenderRequest renderRequest,
100                            RenderResponse renderResponse)
101                    throws Exception {
102    
103                    Portlet portlet = null;
104    
105                    try {
106                            portlet = ActionUtil.getPortlet(renderRequest);
107                    }
108                    catch (PrincipalException pe) {
109                            SessionErrors.add(
110                                    renderRequest, PrincipalException.class.getName());
111    
112                            return actionMapping.findForward(
113                                    "portlet.portlet_configuration.error");
114                    }
115    
116                    renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);
117    
118                    renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));
119    
120                    ConfigurationAction configurationAction = getConfigurationAction(
121                            portlet);
122    
123                    if (configurationAction != null) {
124                            String path = configurationAction.render(
125                                    portletConfig, renderRequest, renderResponse);
126    
127                            if (_log.isDebugEnabled()) {
128                                    _log.debug("Configuration action returned render path " + path);
129                            }
130    
131                            if (Validator.isNotNull(path)) {
132                                    renderRequest.setAttribute(
133                                            WebKeys.CONFIGURATION_ACTION_PATH, path);
134                            }
135                            else {
136                                    _log.error("Configuration action returned a null path");
137                            }
138                    }
139    
140                    return actionMapping.findForward(
141                            getForward(
142                                    renderRequest,
143                                    "portlet.portlet_configuration.edit_configuration"));
144            }
145    
146            @Override
147            public void serveResource(
148                            ActionMapping actionMapping, ActionForm actionForm,
149                            PortletConfig portletConfig, ResourceRequest resourceRequest,
150                            ResourceResponse resourceResponse)
151                    throws Exception {
152    
153                    Portlet portlet = null;
154    
155                    try {
156                            portlet = ActionUtil.getPortlet(resourceRequest);
157                    }
158                    catch (PrincipalException pe) {
159                            return;
160                    }
161    
162                    resourceRequest = ActionUtil.getWrappedResourceRequest(
163                            resourceRequest, null);
164    
165                    ResourceServingConfigurationAction resourceServingConfigurationAction =
166                            (ResourceServingConfigurationAction)getConfigurationAction(portlet);
167    
168                    if (resourceServingConfigurationAction == null) {
169                            return;
170                    }
171    
172                    resourceServingConfigurationAction.serveResource(
173                            portletConfig, resourceRequest, resourceResponse);
174            }
175    
176            protected ConfigurationAction getConfigurationAction(Portlet portlet)
177                    throws Exception {
178    
179                    if (portlet == null) {
180                            return null;
181                    }
182    
183                    ConfigurationAction configurationAction =
184                            portlet.getConfigurationActionInstance();
185    
186                    if (configurationAction == null) {
187                            _log.error(
188                                    "Configuration action for portlet " + portlet.getPortletId() +
189                                            " is null");
190                    }
191    
192                    return configurationAction;
193            }
194    
195            private static final Log _log = LogFactoryUtil.getLog(
196                    EditConfigurationAction.class);
197    
198    }