001    /**
002     * Copyright (c) 2000-2012 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                    PageContext pageContext = _jspFactory.getPageContext(
060                            servlet, servletRequest, servletResponse, errorPageURL,
061                            needsSession, _JSP_WRITER_BUFFER_SIZE, autoflush);
062    
063                    if (_DIRECT_SERVLET_CONTEXT_ENABLED) {
064                            String servletPath = (String)servletRequest.getAttribute(
065                                    WebKeys.SERVLET_PATH);
066    
067                            if (servletPath != null) {
068                                    servletRequest.removeAttribute(WebKeys.SERVLET_PATH);
069    
070                                    ServletContext servletContext = pageContext.getServletContext();
071    
072                                    String contextPath = ContextPathUtil.getContextPath(
073                                            servletContext);
074    
075                                    DirectServletRegistry.putServlet(
076                                            contextPath.concat(servletPath), servlet);
077                            }
078                    }
079    
080                    return new PageContextWrapper(pageContext);
081            }
082    
083            @Override
084            public void releasePageContext(PageContext pageContext) {
085                    if (pageContext instanceof PageContextWrapper) {
086                            PageContextWrapper pageContextWrapper =
087                                    (PageContextWrapper)pageContext;
088    
089                            pageContext = pageContextWrapper.getWrappedPageContext();
090                    }
091    
092                    _jspFactory.releasePageContext(pageContext);
093            }
094    
095            private static final boolean _DIRECT_SERVLET_CONTEXT_ENABLED =
096                    GetterUtil.getBoolean(
097                            PropsUtil.get(PropsKeys.DIRECT_SERVLET_CONTEXT_ENABLED));
098    
099            private static final int _JSP_WRITER_BUFFER_SIZE = GetterUtil.getInteger(
100                    PropsUtil.get(PropsKeys.JSP_WRITER_BUFFER_SIZE));
101    
102            private JspFactory _jspFactory;
103    
104    }