Author: markt Date: Mon Feb 27 21:01:07 2017 New Revision: 1784661 URL: http://svn.apache.org/viewvc?rev=1784661&view=rev Log: Implement getServletMapping() for async requests as discussed in Servlet 4.0 EG
Modified: tomcat/trunk/java/javax/servlet/AsyncContext.java tomcat/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java tomcat/trunk/test/org/apache/catalina/core/TestApplicationMapping.java Modified: tomcat/trunk/java/javax/servlet/AsyncContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/javax/servlet/AsyncContext.java?rev=1784661&r1=1784660&r2=1784661&view=diff ============================================================================== --- tomcat/trunk/java/javax/servlet/AsyncContext.java (original) +++ tomcat/trunk/java/javax/servlet/AsyncContext.java Mon Feb 27 21:01:07 2017 @@ -25,6 +25,8 @@ public interface AsyncContext { "javax.servlet.async.request_uri"; public static final String ASYNC_CONTEXT_PATH = "javax.servlet.async.context_path"; + public static final String ASYNC_MAPPING = + "javax.servlet.async.mapping"; public static final String ASYNC_PATH_INFO = "javax.servlet.async.path_info"; public static final String ASYNC_SERVLET_PATH = Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java?rev=1784661&r1=1784660&r2=1784661&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java Mon Feb 27 21:01:07 2017 @@ -22,6 +22,7 @@ import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import javax.servlet.AsyncContext; import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; @@ -354,9 +355,7 @@ final class ApplicationDispatcher implem // Handle an HTTP path-based forward else { - ApplicationHttpRequest wrequest = - (ApplicationHttpRequest) wrapRequest(state); - String contextPath = context.getPath(); + ApplicationHttpRequest wrequest = (ApplicationHttpRequest) wrapRequest(state); HttpServletRequest hrequest = state.hrequest; if (hrequest.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI) == null) { wrequest.setAttribute(RequestDispatcher.FORWARD_REQUEST_URI, @@ -372,7 +371,7 @@ final class ApplicationDispatcher implem wrequest.setAttribute(RequestDispatcher.FORWARD_MAPPING, hrequest.getServletMapping()); } - wrequest.setContextPath(contextPath); + wrequest.setContextPath(context.getPath()); wrequest.setRequestURI(requestURI); wrequest.setServletPath(servletPath); wrequest.setPathInfo(pathInfo); @@ -612,13 +611,12 @@ final class ApplicationDispatcher implem // Create a wrapped response to use for this request wrapResponse(state); - ApplicationHttpRequest wrequest = - (ApplicationHttpRequest) wrapRequest(state); + ApplicationHttpRequest wrequest = (ApplicationHttpRequest) wrapRequest(state); + HttpServletRequest hrequest = state.hrequest; - wrequest.setAttribute(Globals.DISPATCHER_TYPE_ATTR, - DispatcherType.ASYNC); - wrequest.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR, - getCombinedPath()); + wrequest.setAttribute(Globals.DISPATCHER_TYPE_ATTR, DispatcherType.ASYNC); + wrequest.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR, getCombinedPath()); + wrequest.setAttribute(AsyncContext.ASYNC_MAPPING, hrequest.getServletMapping()); wrequest.setContextPath(context.getPath()); wrequest.setRequestURI(requestURI); @@ -628,6 +626,7 @@ final class ApplicationDispatcher implem wrequest.setQueryString(queryString); wrequest.setQueryParams(queryString); } + wrequest.setMapping(mapping); invoke(state.outerRequest, state.outerResponse, state); } Modified: tomcat/trunk/test/org/apache/catalina/core/TestApplicationMapping.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestApplicationMapping.java?rev=1784661&r1=1784660&r2=1784661&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TestApplicationMapping.java (original) +++ tomcat/trunk/test/org/apache/catalina/core/TestApplicationMapping.java Mon Feb 27 21:01:07 2017 @@ -19,6 +19,7 @@ package org.apache.catalina.core; import java.io.IOException; import java.io.PrintWriter; +import javax.servlet.AsyncContext; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -30,6 +31,7 @@ import org.junit.Assert; import org.junit.Test; import org.apache.catalina.Context; +import org.apache.catalina.Wrapper; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.tomcat.util.buf.ByteChunk; @@ -101,6 +103,9 @@ public class TestApplicationMapping exte tearDown(); setUp(); doTestMappingNamedForward(contextPath, mapping, requestPath, matchValue, matchType); + tearDown(); + setUp(); + doTestMappingAsync(contextPath, mapping, requestPath, matchValue, matchType); } private void doTestMappingDirect(String contextPath, String mapping, String requestPath, @@ -226,6 +231,35 @@ public class TestApplicationMapping exte Assert.assertTrue(body, body.contains("ServletName=[Forward]")); } + private void doTestMappingAsync(String contextPath, String mapping, String requestPath, + String matchValue, String matchType) throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // No file system docBase required + Context ctx = tomcat.addContext(contextPath, null); + + Wrapper w = Tomcat.addServlet(ctx, "Async", new AsyncServlet()); + w.setAsyncSupported(true); + ctx.addServletMappingDecoded(mapping, "Async"); + Tomcat.addServlet(ctx, "Mapping", new MappingServlet()); + ctx.addServletMappingDecoded("/mapping", "Mapping"); + + tomcat.start(); + + ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath); + String body = bc.toString(); + + Assert.assertTrue(body, body.contains("MatchValue=[/mapping]")); + Assert.assertTrue(body, body.contains("Pattern=[/mapping]")); + Assert.assertTrue(body, body.contains("MatchType=[EXACT]")); + Assert.assertTrue(body, body.contains("ServletName=[Mapping]")); + + Assert.assertTrue(body, body.contains("AsyncMatchValue=[" + matchValue + "]")); + Assert.assertTrue(body, body.contains("AsyncPattern=[" + mapping + "]")); + Assert.assertTrue(body, body.contains("AsyncMatchType=[" + matchType + "]")); + Assert.assertTrue(body, body.contains("AsyncServletName=[Async]")); + } + private static class IncludeServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -275,6 +309,18 @@ public class TestApplicationMapping exte } + private static class AsyncServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + AsyncContext ac = req.startAsync(); + ac.dispatch("/mapping"); + } + } + + private static class MappingServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -289,7 +335,8 @@ public class TestApplicationMapping exte pw.println("Pattern=[" + mapping.getPattern() + "]"); pw.println("MatchType=[" + mapping.getMappingMatch() + "]"); pw.println("ServletName=[" + mapping.getServletName() + "]"); - ServletMapping includeMapping = (ServletMapping) req.getAttribute(RequestDispatcher.INCLUDE_MAPPING); + ServletMapping includeMapping = + (ServletMapping) req.getAttribute(RequestDispatcher.INCLUDE_MAPPING); if (includeMapping != null) { pw.println("IncludeMatchValue=[" + includeMapping.getMatchValue() + "]"); pw.println("IncludePattern=[" + includeMapping.getPattern() + "]"); @@ -297,13 +344,22 @@ public class TestApplicationMapping exte pw.println("IncludeServletName=[" + includeMapping.getServletName() + "]"); } - ServletMapping forwardMapping = (ServletMapping) req.getAttribute(RequestDispatcher.FORWARD_MAPPING); + ServletMapping forwardMapping = + (ServletMapping) req.getAttribute(RequestDispatcher.FORWARD_MAPPING); if (forwardMapping != null) { pw.println("ForwardMatchValue=[" + forwardMapping.getMatchValue() + "]"); pw.println("ForwardPattern=[" + forwardMapping.getPattern() + "]"); pw.println("ForwardMatchType=[" + forwardMapping.getMappingMatch() + "]"); pw.println("ForwardServletName=[" + forwardMapping.getServletName() + "]"); } + ServletMapping asyncMapping = + (ServletMapping) req.getAttribute(AsyncContext.ASYNC_MAPPING); + if (asyncMapping != null) { + pw.println("AsyncMatchValue=[" + asyncMapping.getMatchValue() + "]"); + pw.println("AsyncPattern=[" + asyncMapping.getPattern() + "]"); + pw.println("AsyncMatchType=[" + asyncMapping.getMappingMatch() + "]"); + pw.println("AsyncServletName=[" + asyncMapping.getServletName() + "]"); + } } } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org