I should pass some information coming with a HTTP header from a request to an EJB and I don't want to add parameters and pass them everywhere I need them. So i used a RequestScoped bean to hold my header values. Is this implementation ok, or do I misunderstand the concept of @RecuestScoped beans?
First I get the headers with an interceptor (Resteasy), and save them into my request scoped bean:
@Provider
@ServerInterceptor
public class SomeInterceptor implements PreProcessInterceptor {
@Inject
ApplicationRequestContext appContext;
@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod resourceMethod)
throws Failure, WebApplicationException {
List<String> values = request.getHttpHeaders().getRequestHeader("Some-Header");
if(values != null && values.size() > 0) {
appContext.setSomeHeader(values.get(0));
}
return null;
}
}
This is the bean:
@RequestScoped
public class ApplicationRequestContext implements Serializable {
private String someHeader;
public void setSomeHeader(String someHeader) {
this.someHeader = someHeader;
}
public String getSomeHeader() {
return someHeader;
}
}
Then I can access the value (different in every request) from my EJB:
@Stateless
public class CheckInWorkflow {
@Inject
ApplicationRequestContext appContext;
public void someEjbMethod() {
doSomethingWithThisHeaderValue(appContext.getSomeHeader());
}
}
Based upon the description of @RequestScoped this should work as expected:
The request scope is active:
- during the service() method of any servlet in the web application, during the doFilter() method of any servlet filter and when the container calls any ServletRequestListener or AsyncListener,
- during any Java EE web service invocation,
- during any remote method invocation of any EJB, during any asynchronous method invocation of any EJB, during any call to an EJB timeout method and
- during message delivery to any EJB message-driven bean, and during any message delivery to a MessageListener for a JMS topic or queue obtained from the Java EE component environment.