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