From 7111f5f1617bbdb1f4f0a25aad016873108cfa06 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 14 May 2020 08:26:44 +1000 Subject: [PATCH] Issue #4861 - AsyncAttributes should wrap ServletAttributes inner AttributesMap Signed-off-by: Lachlan Roberts --- .../eclipse/jetty/server/AsyncAttributes.java | 22 ++++---- .../org/eclipse/jetty/server/Request.java | 56 +++++++++++-------- .../jetty/server/ServletAttributes.java | 4 +- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncAttributes.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncAttributes.java index 9d040cb9a06..8121d61cb5f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncAttributes.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncAttributes.java @@ -118,17 +118,17 @@ class AsyncAttributes extends Attributes.Wrapper super.clearAttributes(); } - public void applyToAttributes(Attributes attributes) + public static void applyAsyncAttributes(Attributes attributes, String requestURI, String contextPath, String servletPath, String pathInfo, String queryString) { - if (_requestURI != null) - attributes.setAttribute(AsyncContext.ASYNC_REQUEST_URI, _requestURI); - if (_contextPath != null) - attributes.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH, _contextPath); - if (_servletPath != null) - attributes.setAttribute(AsyncContext.ASYNC_SERVLET_PATH, _servletPath); - if (_pathInfo != null) - attributes.setAttribute(AsyncContext.ASYNC_PATH_INFO, _pathInfo); - if (_queryString != null) - attributes.setAttribute(AsyncContext.ASYNC_QUERY_STRING, _queryString); + if (requestURI != null) + attributes.setAttribute(AsyncContext.ASYNC_REQUEST_URI, requestURI); + if (contextPath != null) + attributes.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH, contextPath); + if (servletPath != null) + attributes.setAttribute(AsyncContext.ASYNC_SERVLET_PATH, servletPath); + if (pathInfo != null) + attributes.setAttribute(AsyncContext.ASYNC_PATH_INFO, pathInfo); + if (queryString != null) + attributes.setAttribute(AsyncContext.ASYNC_QUERY_STRING, queryString); } } 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 34f9e57bbb8..6565a6e7a7b 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 @@ -1982,6 +1982,30 @@ public class Request implements HttpServletRequest if (getAttribute(AsyncContext.ASYNC_REQUEST_URI) != null) return; + String requestURI; + String contextPath; + String servletPath; + String pathInfo; + String queryString; + + // Have we been forwarded before? + requestURI = (String)getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); + if (requestURI != null) + { + contextPath = (String)getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH); + servletPath = (String)getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH); + pathInfo = (String)getAttribute(RequestDispatcher.FORWARD_PATH_INFO); + queryString = (String)getAttribute(RequestDispatcher.FORWARD_QUERY_STRING); + } + else + { + requestURI = getRequestURI(); + contextPath = getContextPath(); + servletPath = getServletPath(); + pathInfo = getPathInfo(); + queryString = getQueryString(); + } + // Unwrap the _attributes to get the base attributes instance. Attributes baseAttributes; if (_attributes == null) @@ -1989,31 +2013,17 @@ public class Request implements HttpServletRequest else baseAttributes = Attributes.unwrap(_attributes); - AsyncAttributes asyncAttributes; - // Have we been forwarded before? - String uri = (String)getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); - if (uri != null) - { - String contextPath = (String)getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH); - String servletPath = (String)getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH); - String pathInfo = (String)getAttribute(RequestDispatcher.FORWARD_PATH_INFO); - String queryString = (String)getAttribute(RequestDispatcher.FORWARD_QUERY_STRING); - asyncAttributes = new AsyncAttributes(baseAttributes, uri, contextPath, servletPath, pathInfo, queryString); - } - else - { - String requestURI = getRequestURI(); - String contextPath = getContextPath(); - String servletPath = getServletPath(); - String pathInfo = getPathInfo(); - String queryString = getQueryString(); - asyncAttributes = new AsyncAttributes(baseAttributes, requestURI, contextPath, servletPath, pathInfo, queryString); - } - if (baseAttributes instanceof ServletAttributes) - ((ServletAttributes)_attributes).setAsyncAttributes(asyncAttributes); + { + // Set the AsyncAttributes on the ServletAttributes. + ServletAttributes servletAttributes = (ServletAttributes)baseAttributes; + servletAttributes.setAsyncAttributes(requestURI, contextPath, servletPath, pathInfo, queryString); + } else - asyncAttributes.applyToAttributes(_attributes); + { + // If ServletAttributes has been replaced just set them on the top level Attributes. + AsyncAttributes.applyAsyncAttributes(_attributes, requestURI, contextPath, servletPath, pathInfo, queryString); + } } /** diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ServletAttributes.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ServletAttributes.java index ec24e37216c..b98a3a04e9c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ServletAttributes.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ServletAttributes.java @@ -28,9 +28,9 @@ public class ServletAttributes implements Attributes private final Attributes _attributes = new AttributesMap(); private AsyncAttributes _asyncAttributes; - public void setAsyncAttributes(AsyncAttributes attributes) + public void setAsyncAttributes(String requestURI, String contextPath, String servletPath, String pathInfo, String queryString) { - _asyncAttributes = attributes; + _asyncAttributes = new AsyncAttributes(_attributes, requestURI, contextPath, servletPath, pathInfo, queryString); } private Attributes getAttributes()