When I created my subresource, my Injection (authenticatedUser) is null. But in the root class (authenticatedUser) the Injection had the correct values. Therefore, when I used
/medicos/me
I get the correct values, but when I used
/medicos/me/pacientes
I get a error null, from authenticatedUser in subresource PacienteResource. I don't understand and how I can resolve this dependency in sub resource. This dependency (authenticatedUser) is produced in authentification filter.
@Path("/medicos")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MedicoResource {
@Inject
@AuthenticatedUser
Usuario authenticatedUser;
@GET
@Path("me")
@Produces(MediaType.TEXT_PLAIN)
public Response getMedico() {
return Response.ok(authenticatedUser.getMedico().getNombre()).build();
}
@Path("/me/pacientes")
public PacienteResource getMedicoPacientes() {
System.out.println("get medico pacientes");
return new PacienteResource();//This is the problem.
}
}
Subresource class also had the authenticatedUser, but when I used it, this is null.
public class PacienteResource {
@Inject
@AuthenticatedUser
private Usuario authenticatedUser;
@Inject
PacienteService pacienteService;
@GET
@RolesAllowed({"MEDICO"})
public Set<Paciente> getPacientes() {
Set<Paciente> pacientes = authenticatedUser.getMedico().getPacienteSet();
return pacientes;
}
}
This is my producer of @AuthenticatedUser. The filter "Authentification"throw a event, for this Observer "handleAuthenticationEvent". Then when I need a authenticatedUser I only should inject a @AuthenticatedUser. But when I created the new instance of pacienteResource, the AuthenticatedUser is not produced.
@RequestScoped
public class AuthenticatedUserProducer {
@Inject
private UsuarioService usuarioService;
private Usuario authenticatedUser;
public void handleAuthenticationEvent(@Observes @AuthenticatedUser String username) {
this.authenticatedUser = findUser(username);
}
private Usuario findUser(String username) {
Usuario usuario = usuarioService.findByUserName(username);
return usuario;
}
@Produces
@RequestScoped
@AuthenticatedUser
public Usuario createLogger() {
return authenticatedUser;
}
}
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER})
public @interface AuthenticatedUser {
}