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.taglib.servlet;
016    
017    import com.liferay.portal.kernel.servlet.DirectServletRegistryUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.PropsUtil;
021    import com.liferay.portal.kernel.util.WebKeys;
022    
023    import javax.servlet.Servlet;
024    import javax.servlet.ServletContext;
025    import javax.servlet.ServletRequest;
026    import javax.servlet.ServletResponse;
027    import javax.servlet.jsp.JspApplicationContext;
028    import javax.servlet.jsp.JspEngineInfo;
029    import javax.servlet.jsp.JspFactory;
030    import javax.servlet.jsp.PageContext;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class JspFactoryWrapper extends JspFactory {
036    
037            public JspFactoryWrapper(JspFactory jspFactory) {
038                    _jspFactory = jspFactory;
039            }
040    
041            @Override
042            public JspEngineInfo getEngineInfo() {
043                    return _jspFactory.getEngineInfo();
044            }
045    
046            @Override
047            public JspApplicationContext getJspApplicationContext(
048                    ServletContext servletContext) {
049    
050                    return _jspFactory.getJspApplicationContext(servletContext);
051            }
052    
053            @Override
054            public PageContext getPageContext(
055                    Servlet servlet, ServletRequest servletRequest,
056                    ServletResponse servletResponse, String errorPageURL,
057                    boolean needsSession, int buffer, boolean autoflush) {
058    
059                    if (autoflush) {
060                            buffer = _JSP_WRITER_BUFFER_SIZE;
061                    }
062    
063                    PageContext pageContext = _jspFactory.getPageContext(
064                            servlet, servletRequest, servletResponse, errorPageURL,
065                            needsSession, buffer, autoflush);
066    
067                    if (_DIRECT_SERVLET_CONTEXT_ENABLED) {
068                            String servletPath = (String)servletRequest.getAttribute(
069                                    WebKeys.SERVLET_PATH);
070    
071                            if (servletPath != null) {
072                                    servletRequest.removeAttribute(WebKeys.SERVLET_PATH);
073    
074                                    ServletContext servletContext = pageContext.getServletContext();
075    
076                                    String contextPath = servletContext.getContextPath();
077    
078                                    DirectServletRegistryUtil.putServlet(
079                                            contextPath.concat(servletPath), servlet);
080                            }
081                    }
082    
083                    return new PageContextWrapper(pageContext);
084            }
085    
086            @Override
087            public void releasePageContext(PageContext pageContext) {
088                    if (pageContext instanceof PageContextWrapper) {
089                            PageContextWrapper pageContextWrapper =
090                                    (PageContextWrapper)pageContext;
091    
092                            pageContext = pageContextWrapper.getWrappedPageContext();
093                    }
094    
095                    _jspFactory.releasePageContext(pageContext);
096            }
097    
098            private static final boolean _DIRECT_SERVLET_CONTEXT_ENABLED =
099                    GetterUtil.getBoolean(
100                            PropsUtil.get(PropsKeys.DIRECT_SERVLET_CONTEXT_ENABLED));
101    
102            private static final int _JSP_WRITER_BUFFER_SIZE = GetterUtil.getInteger(
103                    PropsUtil.get(PropsKeys.JSP_WRITER_BUFFER_SIZE));
104    
105            private final JspFactory _jspFactory;
106    
107    }