Author: markt Date: Mon Feb 27 23:51:10 2017 New Revision: 1784673 URL: http://svn.apache.org/viewvc?rev=1784673&view=rev Log: Implement getServletMapping() for async requests as discussed in Servlet 4.0 EG
Added: tomcat/tc8.5.x/trunk/java/org/apache/catalina/servlet4preview/AsyncContext.java Modified: tomcat/tc8.5.x/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java tomcat/tc8.5.x/trunk/test/org/apache/catalina/core/TestApplicationMapping.java Modified: tomcat/tc8.5.x/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java URL: http://svn.apache.org/viewvc/tomcat/tc8.5.x/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java?rev=1784673&r1=1784672&r2=1784673&view=diff ============================================================================== --- tomcat/tc8.5.x/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java (original) +++ tomcat/tc8.5.x/trunk/java/org/apache/catalina/core/ApplicationDispatcher.java Mon Feb 27 23:51:10 2017 @@ -354,9 +354,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, @@ -381,7 +379,7 @@ final class ApplicationDispatcher implem mapping); } - wrequest.setContextPath(contextPath); + wrequest.setContextPath(context.getPath()); wrequest.setRequestURI(requestURI); wrequest.setServletPath(servletPath); wrequest.setPathInfo(pathInfo); @@ -623,13 +621,20 @@ 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()); + ServletMapping mapping; + if (hrequest instanceof org.apache.catalina.servlet4preview.http.HttpServletRequest) { + mapping = ((org.apache.catalina.servlet4preview.http.HttpServletRequest) + hrequest).getServletMapping(); + } else { + mapping = (new ApplicationMapping(null)).getServletMapping(); + } + wrequest.setAttribute( + org.apache.catalina.servlet4preview.AsyncContext.ASYNC_MAPPING, mapping); wrequest.setContextPath(context.getPath()); wrequest.setRequestURI(requestURI); @@ -639,6 +644,7 @@ final class ApplicationDispatcher implem wrequest.setQueryString(queryString); wrequest.setQueryParams(queryString); } + wrequest.setMapping(this.mapping); invoke(state.outerRequest, state.outerResponse, state); } Added: tomcat/tc8.5.x/trunk/java/org/apache/catalina/servlet4preview/AsyncContext.java URL: http://svn.apache.org/viewvc/tomcat/tc8.5.x/trunk/java/org/apache/catalina/servlet4preview/AsyncContext.java?rev=1784673&view=auto ============================================================================== --- tomcat/tc8.5.x/trunk/java/org/apache/catalina/servlet4preview/AsyncContext.java (added) +++ tomcat/tc8.5.x/trunk/java/org/apache/catalina/servlet4preview/AsyncContext.java Mon Feb 27 23:51:10 2017 @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.servlet4preview; + +public interface AsyncContext extends javax.servlet.AsyncContext { + + public static final String ASYNC_MAPPING = "javax.servlet.async.mapping"; +} Modified: tomcat/tc8.5.x/trunk/test/org/apache/catalina/core/TestApplicationMapping.java URL: http://svn.apache.org/viewvc/tomcat/tc8.5.x/trunk/test/org/apache/catalina/core/TestApplicationMapping.java?rev=1784673&r1=1784672&r2=1784673&view=diff ============================================================================== --- tomcat/tc8.5.x/trunk/test/org/apache/catalina/core/TestApplicationMapping.java (original) +++ tomcat/tc8.5.x/trunk/test/org/apache/catalina/core/TestApplicationMapping.java Mon Feb 27 23:51:10 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; @@ -29,6 +30,7 @@ import org.junit.Assert; import org.junit.Test; import org.apache.catalina.Context; +import org.apache.catalina.Wrapper; import org.apache.catalina.servlet4preview.http.ServletMapping; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; @@ -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; @@ -307,6 +353,14 @@ public class TestApplicationMapping exte pw.println("ForwardMatchType=[" + forwardMapping.getMappingMatch() + "]"); pw.println("ForwardServletName=[" + forwardMapping.getServletName() + "]"); } + ServletMapping asyncMapping = (ServletMapping) req.getAttribute( + org.apache.catalina.servlet4preview.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