001
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.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
034 public class DirectRequestDispatcherFactoryImpl
035 implements DirectRequestDispatcherFactory {
036
037 public RequestDispatcher getRequestDispatcher(
038 ServletContext servletContext, String path) {
039
040 return new PACLRequestDispatcherWrapper(
041 servletContext, doGetRequestDispatcher(servletContext, path));
042 }
043
044 public RequestDispatcher getRequestDispatcher(
045 ServletRequest servletRequest, String path) {
046
047 if (!PropsValues.DIRECT_SERVLET_CONTEXT_ENABLED) {
048 return servletRequest.getRequestDispatcher(path);
049 }
050
051 ServletContext servletContext =
052 (ServletContext)servletRequest.getAttribute(WebKeys.CTX);
053
054 if (servletContext == null) {
055 throw new IllegalStateException(
056 "Cannot find servlet context in request attributes");
057 }
058
059 return getRequestDispatcher(servletContext, path);
060 }
061
062 protected RequestDispatcher doGetRequestDispatcher(
063 ServletContext servletContext, String path) {
064
065 if (!PropsValues.DIRECT_SERVLET_CONTEXT_ENABLED) {
066 return servletContext.getRequestDispatcher(path);
067 }
068
069 if ((path == null) || (path.length() == 0)) {
070 return null;
071 }
072
073 if (path.charAt(0) != CharPool.SLASH) {
074 throw new IllegalArgumentException(
075 "Path " + path + " is not relative to context root");
076 }
077
078 String contextPath = ContextPathUtil.getContextPath(servletContext);
079
080 String fullPath = contextPath.concat(path);
081 String queryString = null;
082
083 int pos = fullPath.indexOf(CharPool.QUESTION);
084
085 if (pos != -1) {
086 queryString = fullPath.substring(pos + 1);
087
088 fullPath = fullPath.substring(0, pos);
089 }
090
091 Servlet servlet = DirectServletRegistryUtil.getServlet(fullPath);
092
093 if (servlet == null) {
094 if (_log.isDebugEnabled()) {
095 _log.debug("No servlet found for " + fullPath);
096 }
097
098 RequestDispatcher requestDispatcher =
099 servletContext.getRequestDispatcher(path);
100
101 return new DirectServletPathRegisterDispatcher(
102 path, requestDispatcher);
103 }
104 else {
105 if (_log.isDebugEnabled()) {
106 _log.debug("Servlet found for " + fullPath);
107 }
108
109 return new DirectRequestDispatcher(servlet, queryString);
110 }
111 }
112
113 private static Log _log = LogFactoryUtil.getLog(
114 DirectRequestDispatcherFactoryImpl.class);
115
116 }