title: Using @Context in JAX-RS Provider to provide context information to CDI beans
body: <p>I have some web-service  (JAX-RS, WildFly 9, Resteasy)</p>
  
  <pre><code>@RequestScoped
  public class SomeService{
  // operations
  }
  </code></pre>
  
  <p>Now I want to extract context information like user agent, which can be done using</p>
  
  <pre><code>@Context
  private HttpHeaders httpHeaders;
  </code></pre>
  
  <p>It seems only be possible to inject this context in JAX-RS-related classes, but not in CDI beans called by the webservice. It is possible to put it into the webservice but this clutters the service with stuff not related to the core response of the service.</p>
  
  <p>After some searching, I ended up using the <code>javax.ws.rs.ext.Provider</code> annotation. It seems that the produced <code>ContextInformation</code> object can then be used in other CDI-beans, not just in JAX-RS beans.</p>
  
  <pre><code>@Provider
  public class ContextInformationProducer {
      @Produces
      @RequestScoped
      public ContextInformation create() {
          ContextInformation contextInformation = new ContextInformation();
          contextInformation.setBrowserUserAgent(httpHeaders.getHeaderString("User-Agent"));
      }
  </code></pre>
  
  <p>The question is if this is good practice? Or is it just a coincidence that this works? If it is not good practice, how can I do that in a better way?
  After looking at <a href="https://stackoverflow.com/questions/13557442/what-does-provider-in-jax-rs-mean">What does Provider in JAX-RS mean?</a>, I am not sure if I am 'extending and customizing the JAX-RS runtime'. Should this used by application developers?</p>