HttpServletRequest
has a method setAttribute(String, Object)
.
How can I extract this attribute from ContainterRequest
?
I didn't find: getAttribute
method!
Code
public class AuthenticationFilter implements Filter { public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpReq = (HttpServletRequest) servletRequest; // .... .... httpReq.setAttribute("businessId", businessId); } }
In Jersey Filter:
private class Filter implements ResourceFilter, ContainerRequestFilter { public ContainerRequest filter(ContainerRequest request) { // ..extract the attribute from the httpReq } }
You can't. They're not exposed through the Jersey API in any way. If you search the Jersey codebase, you'll find that there are no uses of HttpServletRequest.getAttributeNames()
, which you'd expect to be used if they were being copied en masse. You'll also find that there are only a handful of uses of HttpServletRequest.getAttribute()
, and it's strictly for internal bookkeeping.
Note, however, that when deployed in a Servlet Context, JAX-RS allows you to inject the original HttpServletRequest using the @Context
annotation. I'm not certain whether you can do this in a Jersey filter, but it works in MessageBodyReaders/Writers and in resource classes.
Update: I've checked, and you can, in fact, inject the HttpServletRequest into a Jersey ContainerRequestFilter by simply including:
@Context private HttpServletRequest httpRequest;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With