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.portal.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
020    import com.liferay.portal.kernel.servlet.DirectRequestDispatcherFactory;
021    import com.liferay.portal.kernel.servlet.DirectServletRegistryUtil;
022    import com.liferay.portal.kernel.util.CharPool;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.util.PropsValues;
025    
026    import javax.servlet.RequestDispatcher;
027    import javax.servlet.Servlet;
028    import javax.servlet.ServletContext;
029    import javax.servlet.ServletRequest;
030    
031    /**
032     * @author Raymond Aug??
033     * @author Shuyang Zhou
034     */
035    @DoPrivileged
036    public class DirectRequestDispatcherFactoryImpl
037            implements DirectRequestDispatcherFactory {
038    
039            @Override
040            public RequestDispatcher getRequestDispatcher(
041                    ServletContext servletContext, String path) {
042    
043                    RequestDispatcher requestDispatcher = doGetRequestDispatcher(
044                            servletContext, path);
045    
046                    return new ClassLoaderRequestDispatcherWrapper(
047                            servletContext, requestDispatcher);
048            }
049    
050            @Override
051            public RequestDispatcher getRequestDispatcher(
052                    ServletRequest servletRequest, String path) {
053    
054                    ServletContext servletContext =
055                            (ServletContext)servletRequest.getAttribute(WebKeys.CTX);
056    
057                    if (servletContext == null) {
058                            return servletRequest.getRequestDispatcher(path);
059                    }
060    
061                    return getRequestDispatcher(servletContext, path);
062            }
063    
064            public interface PACL {
065    
066                    public RequestDispatcher getRequestDispatcher(
067                            ServletContext servletContext, RequestDispatcher requestDispatcher);
068    
069            }
070    
071            protected RequestDispatcher doGetRequestDispatcher(
072                    ServletContext servletContext, String path) {
073    
074                    if (!PropsValues.DIRECT_SERVLET_CONTEXT_ENABLED) {
075                            return servletContext.getRequestDispatcher(path);
076                    }
077    
078                    if ((path == null) || (path.length() == 0)) {
079                            return null;
080                    }
081    
082                    if (path.charAt(0) != CharPool.SLASH) {
083                            throw new IllegalArgumentException(
084                                    "Path " + path + " is not relative to context root");
085                    }
086    
087                    String contextPath = servletContext.getContextPath();
088    
089                    String fullPath = contextPath.concat(path);
090                    String queryString = null;
091    
092                    int pos = fullPath.indexOf(CharPool.QUESTION);
093    
094                    if (pos != -1) {
095                            queryString = fullPath.substring(pos + 1);
096    
097                            fullPath = fullPath.substring(0, pos);
098                    }
099    
100                    Servlet servlet = DirectServletRegistryUtil.getServlet(fullPath);
101    
102                    RequestDispatcher requestDispatcher = null;
103    
104                    if (servlet == null) {
105                            if (_log.isDebugEnabled()) {
106                                    _log.debug("No servlet found for " + fullPath);
107                            }
108    
109                            requestDispatcher = servletContext.getRequestDispatcher(path);
110    
111                            requestDispatcher = new DirectServletPathRegisterDispatcher(
112                                    path, requestDispatcher);
113                    }
114                    else {
115                            if (_log.isDebugEnabled()) {
116                                    _log.debug("Servlet found for " + fullPath);
117                            }
118    
119                            requestDispatcher = new DirectRequestDispatcher(
120                                    servlet, path, queryString);
121                    }
122    
123                    return _pacl.getRequestDispatcher(servletContext, requestDispatcher);
124            }
125    
126            private static final Log _log = LogFactoryUtil.getLog(
127                    DirectRequestDispatcherFactoryImpl.class);
128    
129            private static final PACL _pacl = new NoPACL();
130    
131            private static class NoPACL implements PACL {
132    
133                    @Override
134                    public RequestDispatcher getRequestDispatcher(
135                            ServletContext servletContext,
136                            RequestDispatcher requestDispatcher) {
137    
138                            return requestDispatcher;
139                    }
140    
141            }
142    
143    }