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.asset.model;
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    
023    import java.io.IOException;
024    
025    import javax.servlet.RequestDispatcher;
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletException;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Julio Camarero
033     */
034    public abstract class BaseJSPAssetRenderer<T>
035            extends BaseAssetRenderer<T> implements AssetRenderer<T> {
036    
037            public abstract String getJspPath(
038                    HttpServletRequest request, String template);
039    
040            @Override
041            public boolean include(
042                            HttpServletRequest request, HttpServletResponse response,
043                            String template)
044                    throws Exception {
045    
046                    ServletContext servletContext = getServletContext();
047    
048                    String jspPath = getJspPath(request, template);
049    
050                    if (Validator.isNull(jspPath)) {
051                            return false;
052                    }
053    
054                    RequestDispatcher requestDispatcher =
055                            servletContext.getRequestDispatcher(jspPath);
056    
057                    try {
058                            requestDispatcher.include(request, response);
059    
060                            return true;
061                    }
062                    catch (ServletException se) {
063                            _log.error("Unable to include JSP " + jspPath, se);
064    
065                            throw new IOException("Unable to include " + jspPath, se);
066                    }
067            }
068    
069            public void setServletContext(ServletContext servletContext) {
070                    _servletContext = servletContext;
071            }
072    
073            protected ServletContext getServletContext() {
074                    if (_servletContext != null) {
075                            return _servletContext;
076                    }
077    
078                    String portletId = getAssetRendererFactory().getPortletId();
079    
080                    PortletBag portletBag = PortletBagPool.get(portletId);
081    
082                    return portletBag.getServletContext();
083            }
084    
085            private static final Log _log = LogFactoryUtil.getLog(
086                    BaseJSPAssetRenderer.class);
087    
088            private ServletContext _servletContext;
089    
090    }