diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java index 7666fcdfdfe..0658e1edc38 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java @@ -2416,6 +2416,14 @@ public class Request implements HttpServletRequest @Override public HttpServletMapping getHttpServletMapping() { + // TODO This is to pass the current TCK. This has been challenged in https://github.com/eclipse-ee4j/jakartaee-tck/issues/585 + if (_dispatcherType == DispatcherType.ASYNC) + { + Object async = getAttribute(AsyncContext.ASYNC_MAPPING); + if (async != null) + return (ServletPathMapping)async; + } + // The mapping returned is normally for the current servlet. Except during an // INCLUDE dispatch, in which case this method returns the mapping of the source servlet, // which we recover from the IncludeAttributes wrapper. diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java index fa17a6310fb..33c2746bd71 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.List; +import javax.servlet.AsyncContext; import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -469,6 +470,17 @@ public class DispatcherTest testForward(); } + @Test + public void testDispatchMapping() throws Exception + { + _contextHandler.addServlet(new ServletHolder("TestServlet", MappingServlet.class), "/TestServlet"); + _contextHandler.addServlet(new ServletHolder("DispatchServlet", AsyncDispatch2TestServlet.class), "/DispatchServlet"); + + String response = _connector.getResponse("GET /context/DispatchServlet HTTP/1.0\n\n"); + // TODO This is to pass the current TCK. This has been challenged in https://github.com/eclipse-ee4j/jakartaee-tck/issues/585 + assertThat(response, containsString("matchValue=DispatchServlet, pattern=/DispatchServlet, servletName=DispatchServlet, mappingMatch=EXACT")); + } + public static class WrappingFilter implements Filter { @Override @@ -1060,4 +1072,45 @@ public class DispatcherTest response.getOutputStream().print(request.getDispatcherType().toString()); } } + + public static class MappingServlet extends HttpServlet + { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException + { + HttpServletMapping mapping = req.getHttpServletMapping(); + if (mapping == null) + { + resp.getWriter().println("Get null HttpServletMapping"); + } + else + { + StringBuilder sb = new StringBuilder(); + sb.append("matchValue=" + mapping.getMatchValue()) + .append(", pattern=" + mapping.getPattern()) + .append(", servletName=" + mapping.getServletName()) + .append(", mappingMatch=" + mapping.getMappingMatch()); + resp.getWriter().println(sb.toString()); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException + { + this.doGet(req, resp); + } + } + + public static class AsyncDispatch2TestServlet extends HttpServlet + { + public void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException + { + AsyncContext asyncContext = req.startAsync(); + asyncContext.setTimeout(0); + asyncContext.dispatch("/TestServlet"); + } + } }