AsyncContext within a Filter is not brittle.
We have a DoSFilter that uses it just fine.
https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/DoSFilter.java
AsyncContext itself has many rules that you have to follow.
> Basically, how do I know when the filter chain has completed? I need to set a cookie, but only at the very end of the request.
"At the very end of the request" would be well after the committed state has been reached.
Once you have a committed response you cannot change the headers any more as the response headers have been sent already, this is normal servlet behavior, and applies to all usages (async or not).
Unfortunately, there's no servlet mechanism to hook into the "about to go committed" event.
Cookies are special and will be applied regardless of the dispatch (normal, async, error, include, forward, etc).
They will never be cleared / rewritten by servlet container behaviors once set.
Why do you want to wait till the end of the request to set it?
Why not set the Cookie before you execute the filter chain?
While the HTTP spec has a concept of Trailers, which would allow for setting Cookies after the request, using Trailers from within the Servlet spec isn't easy.
And not all HTTP clients (some browsers included) support Trailers.