7

I'm modifying aHttpRequest on my WebFilter by adding a single header to request using a HttpServletRequestWrapper implementation class:

HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(request);
requestWrapper.addHeader(OAuth.OAUTH_ACCESS_TOKEN, accessTokenWord);
chain.doFilter(requestWrapper, response);

After doFilter(requestWrapper, response) is performed, JAX-RS leads request to its resource, which has a @RequestScoped field:

@Inject
protected HttpServletRequest request;

However, it doesn't contain any expected header:

@PostConstruct
protected void initialize_resources() throws IllegalStateException {

    this.currentUser = null;
    String accessToken = this.request.getHeader(OAuth.OAUTH_ACCESS_TOKEN);
    AccessToken accessToken = this.memcachedResources.getMemcachedAccessTokenRepository()
                                  .get(accessToken);

    if (accessToken != null && StringUtils.isNotEmpty(accessToken.getUser_id())) {
        this.currentUser = this.em.find(User.class, accessToken.getUser_id());
        this.currentClient = accessToken.getClientId();    
    }
}

So, this.request.getHeader(OAuth.OAUTH_ACCESS_TOKEN) is null.

How can I solve that?

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Jordi
  • 20,868
  • 39
  • 149
  • 333

1 Answers1

20

Refer to this question for details on how to add HTTP headers to the request using a servlet filter. If you intend to use a JAX-RS filter, keep reading.


JAX-RS filters

Once you are working with JAX-RS, you'd better using a ContainerRequestFilter like the following to add a header to the request:

@Provider
public class MyContainerRequestFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        requestContext.getHeaders().add("header", "value");
    }
}

Observe the following:

  • The ContainerRequestContext#getHeaders() returns a mutable multivalued map which contains the request headers.

  • The @Provider annotation marks an implementation of an extension interface that should be discoverable by JAX-RS runtime during a provider scanning phase.

For more details about JAX-RS filters, have a look at the Jersey documentation. The JAX-RS filters can be applied globally or can be name-bound to a subset of endpoints.

Getting the headers values

In your REST endpoints, you can inject HttpHeaders:

@Context
HttpHeaders httpHeaders;

Then use the HttpHeaders API to get the header values:

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • What if a legacy application is using JAX-RS 1.1? (The interfaces you mentioned are available from JAX-RS 2.0) – Francesco Rizzi Jul 11 '23 at 23:17
  • @FrancescoRizzi perhaps a servlet filter could help in this case? – cassiomolin Jul 11 '23 at 23:26
  • Unfortunately even with a servlet filter which wraps requests/responses, the "getheaders" method of the HttpServletRequest gets never invoked. By using @HttpHeaders jax-RS is referring to a different HttpServletRequest (maybe the original one and not the wrapped one, passed with the "doFilter" method) – Francesco Rizzi Jul 11 '23 at 23:29
  • 1
    NEVERMIND, it worked with filters, just didn't notice that i was passing the original HttpServletRequest instance instead of the wrapped one in the method "doFilter" – Francesco Rizzi Jul 12 '23 at 00:17