added session listener

This commit is contained in:
francesco 2020-10-20 17:41:44 +02:00
parent be0d48c4c8
commit c4ba087512
2 changed files with 62 additions and 4 deletions

View File

@ -0,0 +1,54 @@
package org.gcube.portlets.widgets.mpformbuilder.server;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.gcube.portlets.widgets.mpformbuilder.client.ConstantsMPFormBuilder;
import org.gcube.portlets.widgets.mpformbuilder.shared.upload.FileUploaded;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebListener
public class UploadedFileHttpSessionListener implements HttpSessionListener {
private static final Logger LOG = LoggerFactory.getLogger(UploadedFileHttpSessionListener.class);
@Override
public void sessionDestroyed(HttpSessionEvent event) {
LOG.info("sessionDestroyed called. Session id is: "+event.getSession().getId());
try {
HttpSession httpSession = event.getSession();
List<FileUploaded> listFileUploaded = (List<FileUploaded>) httpSession.getAttribute(ConstantsMPFormBuilder.FILE_UPLOADED_SESSION_ATTR);
if(listFileUploaded!=null) {
LOG.info("found file uploded in session, removin it");
for (FileUploaded fileUploaded : listFileUploaded) {
try {
File file = new File(fileUploaded.getTempSystemPath());
boolean result = Files.deleteIfExists(file.toPath());
LOG.info("File "+fileUploaded.getTempSystemPath() +" deleted? "+result);
}catch (Exception e) {
LOG.warn("Error on deleting file: "+fileUploaded, e);
}
}
}
}catch (Exception e) {
LOG.warn("Error tryng to delete uploaded file", e);
}
}
@Override
public void sessionCreated(HttpSessionEvent arg0) {
LOG.info("sessionCreated called. Session id is: "+arg0.getSession().getId());
//session.setMaxInactiveInterval(5);//in seconds
}
}

View File

@ -1,9 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<web-app>
<listener>
<listener-class>org.gcube.portlets.widgets.mpformbuilder.server.UploadedFileHttpSessionListener</listener-class>
</listener>
<!-- Servlets -->
<servlet>