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.asset.kernel.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.AggregateResourceBundleLoader;
022    import com.liferay.portal.kernel.util.ClassResourceBundleLoader;
023    import com.liferay.portal.kernel.util.ResourceBundleLoader;
024    import com.liferay.portal.kernel.util.ResourceBundleLoaderUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.util.WebKeys;
027    
028    import java.io.IOException;
029    
030    import javax.servlet.RequestDispatcher;
031    import javax.servlet.ServletContext;
032    import javax.servlet.ServletException;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    /**
037     * @author Julio Camarero
038     */
039    public abstract class BaseJSPAssetRenderer<T>
040            extends BaseAssetRenderer<T> implements AssetRenderer<T> {
041    
042            public abstract String getJspPath(
043                    HttpServletRequest request, String template);
044    
045            @Override
046            public boolean include(
047                            HttpServletRequest request, HttpServletResponse response,
048                            String template)
049                    throws Exception {
050    
051                    ServletContext servletContext = getServletContext();
052    
053                    String jspPath = getJspPath(request, template);
054    
055                    if (Validator.isNull(jspPath)) {
056                            return false;
057                    }
058    
059                    ResourceBundleLoader resourceBundleLoader =
060                            (ResourceBundleLoader)request.getAttribute(
061                                    WebKeys.RESOURCE_BUNDLE_LOADER);
062    
063                    RequestDispatcher requestDispatcher =
064                            servletContext.getRequestDispatcher(jspPath);
065    
066                    try {
067                            request.setAttribute(
068                                    WebKeys.RESOURCE_BUNDLE_LOADER, getResourceBundleLoader());
069    
070                            requestDispatcher.include(request, response);
071    
072                            return true;
073                    }
074                    catch (ServletException se) {
075                            _log.error("Unable to include JSP " + jspPath, se);
076    
077                            throw new IOException("Unable to include " + jspPath, se);
078                    }
079                    finally {
080                            request.setAttribute(
081                                    WebKeys.RESOURCE_BUNDLE_LOADER, resourceBundleLoader);
082                    }
083            }
084    
085            public void setServletContext(ServletContext servletContext) {
086                    _servletContext = servletContext;
087            }
088    
089            protected ResourceBundleLoader getResourceBundleLoader() {
090                    if (_servletContext != null) {
091                            return ResourceBundleLoaderUtil.
092                                    getResourceBundleLoaderByServletContextName(
093                                            _servletContext.getServletContextName());
094                    }
095    
096                    return new AggregateResourceBundleLoader(
097                            new ClassResourceBundleLoader("content.Language", getClass()),
098                            ResourceBundleLoaderUtil.getPortalResourceBundleLoader());
099            }
100    
101            protected ServletContext getServletContext() {
102                    if (_servletContext != null) {
103                            return _servletContext;
104                    }
105    
106                    String portletId = getAssetRendererFactory().getPortletId();
107    
108                    PortletBag portletBag = PortletBagPool.get(portletId);
109    
110                    return portletBag.getServletContext();
111            }
112    
113            private static final Log _log = LogFactoryUtil.getLog(
114                    BaseJSPAssetRenderer.class);
115    
116            private ServletContext _servletContext;
117    
118    }