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