And for reasons
unbeknownst to me this is actually kind of working in the Open Liberty Beta for JEE 8.
So what I'd like to achieve is to implement a validation message interpolator that uses the locale from the JAX-RS Headers (HelloResource.java). But since the message interpolator is global in the application it has to work for servlet requests and outside of request processing as well (TestServlet.java / SomeSingleton.java).
The piece of code I'm interested in is in
https://github.com/NiklasMehner/jaxrsValidationTest/blob/master/src/main/java/de/niklasmehner/HelloMessageInterpolator.java :
@javax.ws.rs.core.Context |
private HttpHeaders headers; |
private Instance<HttpServletRequest> request;
This fields are actually successfully injected. From my understanding of the specs it should not actually be possible to inject
HttpHeaders into a CDI-bBean. Is this correct? If yes: Is there any defined behavior in the case that the injection is not possible?
In this implementation the only way to find out if the call comes from a JAX-RS resource is the check "headers.getAcceptableLanguages() != null". Which is in my option the worst way: The object is there, the method call succeeds, but no value is returned.
(the Instance<HttpServletRequest> is broken in a similar way (request.isResolvable() returns true when the request is not available), but this is probably an implementation issue or at least covered by a different spec)
The
HttpServletRequest injection also works. But I could not find anything in the spec, that indicates that the CDI request scope is actually active while a JAX-RS request is executed. Is this up to the implementors to decide?
As I have written in the comment in ticket 544 I don't think jax-rs can specify its own way of doing the bean validation, because a resource annotated with @Stateless has to follow the CDI specification. So the only way to solve this (I currently see) is to allow the injection of
HttpHeaders into CDI beans (or expect that JAX-RS implementation are always based on servlets).
Is my understanding of the situation basically correct, or am I missing something here?