001    /**
002     * Copyright (c) 2000-2013 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;
016    
017    import com.liferay.portal.kernel.util.ContextPathUtil;
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 = ContextPathUtil.getContextPath(
077                                            servletContext);
078    
079                                    DirectServletRegistryUtil.putServlet(
080                                            contextPath.concat(servletPath), servlet);
081                            }
082                    }
083    
084                    return new PageContextWrapper(pageContext);
085            }
086    
087            @Override
088            public void releasePageContext(PageContext pageContext) {
089                    if (pageContext instanceof PageContextWrapper) {
090                            PageContextWrapper pageContextWrapper =
091                                    (PageContextWrapper)pageContext;
092    
093                            pageContext = pageContextWrapper.getWrappedPageContext();
094                    }
095    
096                    _jspFactory.releasePageContext(pageContext);
097            }
098    
099            private static final boolean _DIRECT_SERVLET_CONTEXT_ENABLED =
100                    GetterUtil.getBoolean(
101                            PropsUtil.get(PropsKeys.DIRECT_SERVLET_CONTEXT_ENABLED));
102    
103            private static final int _JSP_WRITER_BUFFER_SIZE = GetterUtil.getInteger(
104                    PropsUtil.get(PropsKeys.JSP_WRITER_BUFFER_SIZE));
105    
106            private JspFactory _jspFactory;
107    
108    }