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