I need to get the current user in JPA EntityListener
, in a restful webservice. This is my code:
Service Web:
@POST
@Produces({"application/json"})
@Consumes({"application/json"})
public Response create(@HeaderParam("Authorization") String tokenAuth,
AreaTran areaT) {
Response res;
User user = null;
try {
user = testUser(tokenAuth);
Area area = areaCont.create(user, areaT);
res = AppConf.genOk("Area " + area.getNombre() + " creada correctamente");
} catch (Exception e) {
res = le.gen(e, user);
}
return res;
}
Here I get the logged user user = testUser(tokenAuth);
My problem is here:
public class AuditEntityListener {
@PrePersist
public void prePersist(AuditableEntity e) {
e.setCreatedDate(new Date());
//how get the current user for this transaction HERE!!!!!
}
@PreUpdate
public void preUpdate(AuditableEntity e) {
e.setLastModifiedDate(new Date());
}
}
Any way to get the user of the request flow?
This is the solution:
1)In this class store data of current transaction
public class RequestContext {
private static ThreadLocal<Map<Object, Object>> attributes = new ThreadLocal<>();
public static void initialize() {
attributes.set(new HashMap());
}
public static void cleanup() {
attributes.set(null);
}
public static <T> T getAttribute(Object key) {
return (T) attributes.get().get(key);
}
public static void setAttribute(Object key, Object value) {
attributes.get().put(key, value);
}
}
2) Listener to inicialize and clear
@Provider
public class RequestContextFilter implements ContainerRequestFilter,ContainerResponseFilter {
@EJB
UserCont uCont;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
RequestContext.initialize();
//Here your code for assign data
//RequestContext.setAttribute("myAtribute", value);
//In my case
String tokenAuth = requestContext.getHeaderString("Authorization");
Long u = null;
try {
u = Sessions.get(tokenAuth);
} catch (Exception e) {
}
User user = null;
if (u != null) {
user = uCont.find(u);
RequestContext.setAttribute("user", user);
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
RequestContext.cleanup();
}
}
3) Use it in any place
public class AuditEntityListener {
@PrePersist
public void prePersist(AuditableEntity e) {
e.setCreatedDate(new Date());
//In my case
e.setCreatedBy((User) RequestContext.getAttribute("user"));
}
@PreUpdate
public void preUpdate(AuditableEntity e) {
e.setLastModifiedDate(new Date());
}
}