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