Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract request attributes from Jersey's ContainerRequest?

Tags:

java

jersey

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     } } 
like image 278
Dejell Avatar asked Jun 04 '13 06:06

Dejell


1 Answers

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; 
like image 79
Ryan Stewart Avatar answered Nov 10 '22 11:11

Ryan Stewart