title: HttpServletRequest injection on RequestScoped bean CDI
body: <p>I'm looking for a way in order to inject a <code>@RequestScoped</code> custom class into my <code>@Stateless</code> JAX-RS endpoint:</p>
  
  <p>I want each time the application receives a request my custom class is injected in my JAX-RS endpoint.</p>
  
  <p>Custom class:</p>
  
  <pre><code>@RequestScoped
  public class CurrentTransaction {
  
      private String user;
      private String token;
  
      @PersistenceContext(name="mysql")
      protected EntityManager em;
  
      @Inject HttpServletRequest request;
  
      public CurrentTransaction() {
          this.user = request.getHeader("user");
          this.token = request.getHeader("token");
      }
  
      //getters and setters ...
  }
  </code></pre>
  
  <p>So, I declare my <code>CurrentTransaction</code> class as <code>@RequestScoped</code> in order to be initialized each time a request is received.
  In order to do this, I need to access to <code>HttpServletResquest</code> in order to get header parameters.</p>
  
  <p>JAX-RS endpoint:</p>
  
  <pre><code>@Stateless
  @Path("/areas")
  public class AreasEndpoint {
  
     @PersistenceContext(unitName = "mysql")
     protected EntityManager em;
  
     @Inject
     protected CurrentTransaction current_user_service;
  
      @POST
      @Path("postman")
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      @Authentication
      public Response create(AreaRequest request) {
  
          if (this.current_user_service.getUser() == null) {
              System.out.println("Go!!!");
              return Response.status(Status.FORBIDDEN).build();
          } else {
              System.out.println("---- user: " + this.current_user_service.getUser());
              System.out.println("---- token: " + this.current_user_service.getToken());
          }
      }
  
      // ...
  }
  </code></pre>
  
  <p>CDI arrive to perform the constructor of <code>CurrentTransaction</code> class. However, <code>HttpServletRequest</code> request field is not initialized (injected).</p>
  
  <p>What am I doing wrong?</p>