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                            if (_log.isErrorEnabled()) {
055                                    _log.error("Unable to include JSP", se);
056                            }
057    
058                            throw new IOException("Unable to include JSP", se);
059                    }
060            }
061    
062            public void setServletContext(ServletContext servletContext) {
063                    _servletContext = servletContext;
064            }
065    
066            protected abstract String getJspPath();
067    
068            protected ServletContext getServletContext(HttpServletRequest request) {
069                    if (_servletContext != null) {
070                            return _servletContext;
071                    }
072    
073                    String portletId = PortalUtil.getPortletId(request);
074    
075                    if (Validator.isNotNull(portletId)) {
076                            String rootPortletId = PortletConstants.getRootPortletId(portletId);
077    
078                            PortletBag portletBag = PortletBagPool.get(rootPortletId);
079    
080                            return portletBag.getServletContext();
081                    }
082    
083                    return (ServletContext)request.getAttribute(WebKeys.CTX);
084            }
085    
086            private static final Log _log = LogFactoryUtil.getLog(
087                    BaseJSPFormNavigatorEntry.class);
088    
089            private ServletContext _servletContext;
090    
091    }