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.portal.kernel.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.model.PortletConstants;
023    import com.liferay.portal.util.PortalUtil;
024    
025    import java.io.IOException;
026    
027    import javax.portlet.PortletConfig;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    
031    import javax.servlet.RequestDispatcher;
032    import javax.servlet.ServletContext;
033    import javax.servlet.ServletException;
034    import javax.servlet.http.HttpServletRequest;
035    
036    /**
037     * @author Iv??n Zaera
038     */
039    public class BaseJSPSettingsConfigurationAction
040            extends SettingsConfigurationAction
041            implements ConfigurationAction, ResourceServingConfigurationAction {
042    
043            public String getJspPath(RenderRequest renderRequest) {
044                    PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
045    
046                    String configTemplate = selPortletConfig.getInitParameter(
047                            "config-template");
048    
049                    if (Validator.isNotNull(configTemplate)) {
050                            return configTemplate;
051                    }
052    
053                    String configJSP = selPortletConfig.getInitParameter("config-jsp");
054    
055                    if (Validator.isNotNull(configJSP)) {
056                            return configJSP;
057                    }
058    
059                    return "/configuration.jsp";
060            }
061    
062            @Override
063            public void include(
064                            PortletConfig portletConfig, RenderRequest renderRequest,
065                            RenderResponse renderResponse)
066                    throws Exception {
067    
068                    ServletContext servletContext = getServletContext(
069                            PortalUtil.getHttpServletRequest(renderRequest));
070    
071                    RequestDispatcher requestDispatcher =
072                            servletContext.getRequestDispatcher(getJspPath(renderRequest));
073    
074                    try {
075                            requestDispatcher.include(
076                                    PortalUtil.getHttpServletRequest(renderRequest),
077                                    PortalUtil.getHttpServletResponse(renderResponse));
078                    }
079                    catch (ServletException se) {
080                            if (_log.isErrorEnabled()) {
081                                    _log.error("Unable to include JSP", se);
082                            }
083    
084                            throw new IOException("Unable to include JSP", se);
085                    }
086            }
087    
088            public void setServletContext(ServletContext servletContext) {
089                    _servletContext = servletContext;
090            }
091    
092            protected ServletContext getServletContext(HttpServletRequest request) {
093                    if (_servletContext != null) {
094                            return _servletContext;
095                    }
096    
097                    String portletResource = ParamUtil.getString(
098                            request, "portletResource");
099    
100                    if (Validator.isNotNull(portletResource)) {
101                            String rootPortletId = PortletConstants.getRootPortletId(
102                                    portletResource);
103    
104                            PortletBag portletBag = PortletBagPool.get(rootPortletId);
105    
106                            return portletBag.getServletContext();
107                    }
108    
109                    return (ServletContext)request.getAttribute(WebKeys.CTX);
110            }
111    
112            private static final Log _log = LogFactoryUtil.getLog(
113                    BaseJSPSettingsConfigurationAction.class);
114    
115            private ServletContext _servletContext;
116    
117    }