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.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ContextPathUtil;
020    
021    import java.io.InputStream;
022    
023    import java.net.MalformedURLException;
024    import java.net.URL;
025    
026    import java.util.Enumeration;
027    import java.util.Set;
028    
029    import javax.servlet.RequestDispatcher;
030    import javax.servlet.Servlet;
031    import javax.servlet.ServletContext;
032    import javax.servlet.ServletException;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class DirectServletContext implements ServletContext {
038    
039            public DirectServletContext(ServletContext servletContext) {
040                    _servletContext = servletContext;
041    
042                    _contextPath = ContextPathUtil.getContextPath(servletContext);
043            }
044    
045            public Object getAttribute(String name) {
046                    return _servletContext.getAttribute(name);
047            }
048    
049            public Enumeration<String> getAttributeNames() {
050                    return _servletContext.getAttributeNames();
051            }
052    
053            public ServletContext getContext(String uriPath) {
054                    return _servletContext.getContext(uriPath);
055            }
056    
057            public String getContextPath() {
058                    return _contextPath;
059            }
060    
061            public String getInitParameter(String name) {
062                    return _servletContext.getInitParameter(name);
063            }
064    
065            public Enumeration<String> getInitParameterNames() {
066                    return _servletContext.getInitParameterNames();
067            }
068    
069            public int getMajorVersion() {
070                    return _servletContext.getMajorVersion();
071            }
072    
073            public String getMimeType(String file) {
074                    return _servletContext.getMimeType(file);
075            }
076    
077            public int getMinorVersion() {
078                    return _servletContext.getMinorVersion();
079            }
080    
081            public RequestDispatcher getNamedDispatcher(String name) {
082                    return _servletContext.getNamedDispatcher(name);
083            }
084    
085            public String getRealPath(String path) {
086                    return _servletContext.getRealPath(path);
087            }
088    
089            public RequestDispatcher getRequestDispatcher(String path) {
090                    String fullPath = _contextPath.concat(path);
091    
092                    Servlet servlet = DirectServletRegistry.getServlet(fullPath);
093    
094                    if (servlet == null) {
095                            if (_log.isDebugEnabled()) {
096                                    _log.debug("No servlet found for " + fullPath);
097                            }
098    
099                            return _servletContext.getRequestDispatcher(path);
100                    }
101                    else {
102                            if (_log.isDebugEnabled()) {
103                                    _log.debug("Servlet found for " + fullPath);
104                            }
105    
106                            return new DirectRequestDispatcher(servlet);
107                    }
108            }
109    
110            public URL getResource(String path) throws MalformedURLException {
111                    return _servletContext.getResource(path);
112            }
113    
114            public InputStream getResourceAsStream(String path) {
115                    return _servletContext.getResourceAsStream(path);
116            }
117    
118            public Set<String> getResourcePaths(String path) {
119                    return _servletContext.getResourcePaths(path);
120            }
121    
122            public String getServerInfo() {
123                    return _servletContext.getServerInfo();
124            }
125    
126            /**
127             * @deprecated
128             */
129            public Servlet getServlet(String name) throws ServletException {
130                    return _servletContext.getServlet(name);
131            }
132    
133            public String getServletContextName() {
134                    return _servletContext.getServletContextName();
135            }
136    
137            /**
138             * @deprecated
139             */
140            public Enumeration<String> getServletNames() {
141                    return _servletContext.getServletNames();
142            }
143    
144            /**
145             * @deprecated
146             */
147            public Enumeration<Servlet> getServlets() {
148                    return _servletContext.getServlets();
149            }
150    
151            /**
152             * @deprecated
153             */
154            public void log(Exception exception, String message) {
155                    _servletContext.log(exception, message);
156            }
157    
158            public void log(String message) {
159                    _servletContext.log(message);
160            }
161    
162            public void log(String message, Throwable t) {
163                    _servletContext.log(message, t);
164            }
165    
166            public void removeAttribute(String name) {
167                    _servletContext.removeAttribute(name);
168            }
169    
170            public void setAttribute(String name, Object value) {
171                    _servletContext.setAttribute(name, value);
172            }
173    
174            private static Log _log = LogFactoryUtil.getLog(DirectServletContext.class);
175    
176            private String _contextPath;
177            private ServletContext _servletContext;
178    
179    }