@ApplicationScoped @Install
public class SessionClearerUserStatusListener implements UserStatusListener {
public void statusChanged(UserStatus us) {
if (us.isAnonymous()) { // just logout
RequestContext ctx = RequestContext.getCurrentContext();
HttpSession session = ctx.getRequest().getSessionObject();
Enumeration en = session.getAttributeNames();
Set attributesToDelete = new HashSet();
while (en.hasMoreElements()) {
String attrName = (String) en.nextElement();
Object obj = session.getAttribute(attrName);
if (obj == null || !(obj instanceof LogoutSurvivor)) {
attributesToDelete.add(attrName);
}
}
for (Iterator iterator = attributesToDelete.iterator(); iterator.hasNext();) {
String attrName = (String) iterator.next();
session.removeAttribute(attrName);
}
ctx.getRequest().getRequestObject().getSession().invalidate();
}
}