やあみんな。ユーザーがログアウトするか、セッションが期限切れになったときに、ユーザーがアプリケーションで作成したファイルを削除できるように、HttpSessionListener でセッション Bean を取得しようとしています。セッションが破棄されているため、セッション Bean が存在しないと推測しています。これらのファイルを何らかの方法で削除したいと思っていました。助けてくれてありがとう。
@WebListener
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
System.out.print(getTime() + " (session) Created:");
System.out.println("ID=" + session.getId() + " MaxInactiveInterval="
+ session.getMaxInactiveInterval());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
FacesContext context = FacesContext.getCurrentInstance();
//UserSessionBean userSessionBean = (UserSessionBean) context.getApplication().evaluateExpressionGet(context, "#{userSessionBean}", UserSessionBean.class)
UserSessionBean userSessionBean = (UserSessionBean) session.getAttribute("userSessionBean");
System.out.println(session.getId());
System.out.println("File size :" + userSessionBean.getFileList().size());
for (File file : userSessionBean.getFileList()) {
file.delete();
}
}
}
BalusC へ: 以前に考えていた方法に戻ります。ユーザーへのバイトのストリーミングは、私のアプリケーションでは柔軟ではありませんでした。ダウンロードするファイルをストリーミングするために ajax 以外のリクエストを送信する必要がある場合、ページ上で ajax で多くのことを行う必要があることがわかりました。このようにして、重労働は ajax 呼び出し (ドキュメントの生成) で行われ、非 ajax 呼び出しで迅速かつ簡単な作業を行うことができます。
@ManagedBean(name = "userSessionBean")
@SessionScoped
public class UserSessionBean implements Serializable, HttpSessionBindingListener {
final Logger logger = LoggerFactory.getLogger(UserSessionBean.class);
@Inject
private User currentUser;
@EJB
UserService userService;
private List<File> fileList;
public UserSessionBean() {
fileList = new ArrayList<File>();
}
@PostConstruct
public void onLoad() {
Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
String email = principal.getName();
if (email != null) {
currentUser = userService.findUserbyEmail(email);
} else {
logger.error("Couldn't find user information from login!");
}
}
public User getCurrentUser() {
return currentUser;
}
public void setCurrentUser(User currentUser) {
this.currentUser = currentUser;
}
public List<File> getFileList() {
return fileList;
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
logger.info("UserSession unbound");
logger.info(String.valueOf(fileList.size()));
for (File file : fileList) {
logger.info(file.getName());
file.delete();
}
}
@Override
public void valueBound(HttpSessionBindingEvent event) {
logger.info("UserSessionBean bound");
}
}