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.servlet;
016    
017    import com.liferay.portal.kernel.servlet.PluginContextListener;
018    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
019    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
020    import com.liferay.portal.security.pacl.PACLPolicy;
021    import com.liferay.portal.security.pacl.PACLPolicyManager;
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.ServletRequest;
029    import javax.servlet.ServletResponse;
030    
031    /**
032     * @author Raymond Augé
033     */
034    public class PACLRequestDispatcherWrapper implements RequestDispatcher {
035    
036            public PACLRequestDispatcherWrapper(
037                    ServletContext servletContext, RequestDispatcher requestDispatcher) {
038    
039                    _servletContext = servletContext;
040                    _requestDispatcher = requestDispatcher;
041            }
042    
043            public void forward(
044                            ServletRequest servletRequest, ServletResponse servletResponse)
045                    throws IOException, ServletException {
046    
047                    doDispatch(servletRequest, servletResponse, false);
048            }
049    
050            public void include(
051                            ServletRequest servletRequest, ServletResponse servletResponse)
052                    throws IOException, ServletException {
053    
054                    doDispatch(servletRequest, servletResponse, true);
055            }
056    
057            protected void doDispatch(
058                            ServletRequest servletRequest, ServletResponse servletResponse,
059                            boolean include)
060                    throws IOException, ServletException {
061    
062                    ClassLoader contextClassLoader =
063                            PACLClassLoaderUtil.getContextClassLoader();
064    
065                    ClassLoader pluginClassLoader =
066                            (ClassLoader)_servletContext.getAttribute(
067                                    PluginContextListener.PLUGIN_CLASS_LOADER);
068    
069                    PACLPolicy paclPolicy =
070                            PortalSecurityManagerThreadLocal.getPACLPolicy();
071    
072                    try {
073                            if (pluginClassLoader == null) {
074                                    PortalSecurityManagerThreadLocal.setPACLPolicy(null);
075    
076                                    PACLClassLoaderUtil.setContextClassLoader(
077                                            PACLClassLoaderUtil.getPortalClassLoader());
078                            }
079                            else {
080                                    PACLPolicy pluginPACLPolicy = PACLPolicyManager.getPACLPolicy(
081                                            pluginClassLoader);
082    
083                                    PortalSecurityManagerThreadLocal.setPACLPolicy(
084                                            pluginPACLPolicy);
085    
086                                    PACLClassLoaderUtil.setContextClassLoader(pluginClassLoader);
087                            }
088    
089                            if (include) {
090                                    _requestDispatcher.include(servletRequest, servletResponse);
091                            }
092                            else {
093                                    _requestDispatcher.forward(servletRequest, servletResponse);
094                            }
095                    }
096                    finally {
097                            PACLClassLoaderUtil.setContextClassLoader(contextClassLoader);
098    
099                            PortalSecurityManagerThreadLocal.setPACLPolicy(paclPolicy);
100                    }
101            }
102    
103            private RequestDispatcher _requestDispatcher;
104            private ServletContext _servletContext;
105    
106    }