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.security.pacl.servlet;
016    
017    import com.liferay.portal.kernel.servlet.PluginContextListener;
018    
019    import java.io.IOException;
020    
021    import java.security.AccessController;
022    import java.security.PrivilegedActionException;
023    import java.security.PrivilegedExceptionAction;
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 pluginClassLoader =
063                            (ClassLoader)_servletContext.getAttribute(
064                                    PluginContextListener.PLUGIN_CLASS_LOADER);
065    
066                    DispatchPrivilegedExceptionAction dispatchPrivilegedExceptionAction =
067                            new DispatchPrivilegedExceptionAction(
068                                    _requestDispatcher, servletRequest, servletResponse, include);
069    
070                    try {
071                            if (pluginClassLoader == null) {
072                                    AccessController.doPrivileged(
073                                            dispatchPrivilegedExceptionAction);
074                            }
075                            else {
076                                    dispatchPrivilegedExceptionAction.run();
077                            }
078                    }
079                    catch (PrivilegedActionException pae) {
080                            Exception e = pae.getException();
081    
082                            if (e instanceof IOException) {
083                                    throw (IOException)e;
084                            }
085    
086                            throw (ServletException)pae.getException();
087                    }
088            }
089    
090            private RequestDispatcher _requestDispatcher;
091            private ServletContext _servletContext;
092    
093            private class DispatchPrivilegedExceptionAction
094                    implements PrivilegedExceptionAction<Void> {
095    
096                    public DispatchPrivilegedExceptionAction(
097                            RequestDispatcher requestDispatcher, ServletRequest servletRequest,
098                            ServletResponse servletResponse, boolean include) {
099    
100                            _requestDispatcher = requestDispatcher;
101                            _servletRequest = servletRequest;
102                            _servletResponse = servletResponse;
103                            _include = include;
104                    }
105    
106                    public Void run() throws IOException, ServletException {
107                            if (_include) {
108                                    _requestDispatcher.include(_servletRequest, _servletResponse);
109                            }
110                            else {
111                                    _requestDispatcher.forward(_servletRequest, _servletResponse);
112                            }
113    
114                            return null;
115                    }
116    
117                    private boolean _include;
118                    private RequestDispatcher _requestDispatcher;
119                    private ServletRequest _servletRequest;
120                    private ServletResponse _servletResponse;
121    
122            }
123    
124    }