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.servlet.taglib.ui;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.PortletBag;
020    import com.liferay.portal.kernel.portlet.PortletBagPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.PortletConstants;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import java.io.IOException;
027    
028    import javax.servlet.RequestDispatcher;
029    import javax.servlet.ServletContext;
030    import javax.servlet.ServletException;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    /**
035     * @author Julio Camarero
036     */
037    public abstract class BaseJSPFormNavigatorEntry<T>
038            extends BaseFormNavigatorEntry<T> implements FormNavigatorEntry<T> {
039    
040            @Override
041            public void include(
042                            HttpServletRequest request, HttpServletResponse response)
043                    throws IOException {
044    
045                    ServletContext servletContext = getServletContext(request);
046    
047                    RequestDispatcher requestDispatcher =
048                            servletContext.getRequestDispatcher(getJspPath());
049    
050                    try {
051                            requestDispatcher.include(request, response);
052                    }
053                    catch (ServletException se) {
054                            _log.error("Unable to include JSP " + getJspPath(), se);
055    
056                            throw new IOException("Unable to include " + getJspPath(), se);
057                    }
058            }
059    
060            public void setServletContext(ServletContext servletContext) {
061                    _servletContext = servletContext;
062            }
063    
064            protected abstract String getJspPath();
065    
066            protected ServletContext getServletContext(HttpServletRequest request) {
067                    if (_servletContext != null) {
068                            return _servletContext;
069                    }
070    
071                    String portletId = PortalUtil.getPortletId(request);
072    
073                    if (Validator.isNotNull(portletId)) {
074                            String rootPortletId = PortletConstants.getRootPortletId(portletId);
075    
076                            PortletBag portletBag = PortletBagPool.get(rootPortletId);
077    
078                            return portletBag.getServletContext();
079                    }
080    
081                    return (ServletContext)request.getAttribute(WebKeys.CTX);
082            }
083    
084            private static final Log _log = LogFactoryUtil.getLog(
085                    BaseJSPFormNavigatorEntry.class);
086    
087            private ServletContext _servletContext;
088    
089    }