I have a web service:
@Path("/projects")
public class Projects {
[...]
@Inject
CurrentRequest current;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Response getProject(@PathParam("id") String id) {
if (current.isUserAuthenticated()) {
[...do something...]
} else {
[...produce an error...]
}
}
}
And a CDI bean with an authentication method as follows:
@RequestScoped
public class CurrentRequest {
public boolean isUserAuthenticated() {
[...do some header checking...]
}
}
My problem is that I can't grab hold of HTTP headers from the inside out for a lifetime CurrentRequest
. I tried to enter HttpServletRequest
, but not initialized. I tried to use @Context
the same thing. Obviously, this FacesContext.getCurrentInstance()
does not work either due to the lack of a FacesContext.
I see that this question basically asks the same thing, but has not received much attention.
My current approach is to use @Context HttpServletRequest request
inside Projects
and pass it as a parameter to current.isUserAuthenticated(request)
. But this is so wrong. Should a CDI bean know its own request?
What am I missing?