Added FileUtil to remove uplaoded files

This commit is contained in:
Francesco Mangiacrapa 2020-10-21 12:28:18 +02:00
parent c4ba087512
commit 9e3b1ea064
6 changed files with 86 additions and 33 deletions

View File

@ -6,12 +6,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>

View File

@ -373,6 +373,7 @@ public class CreateMetadataForm extends Composite implements HasGenericFormListe
private void prepareMetadataList(final List<MetaDataProfileBean> profiles) {
if(profiles != null && !profiles.isEmpty()){
GWT.log("Building form/s for profile/s: "+profiles);
if(profiles.size()>1)
showChooseProfileForm(true);

View File

@ -0,0 +1,45 @@
package org.gcube.portlets.widgets.mpformbuilder.server;
import java.io.File;
import java.nio.file.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class FileUtil.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Oct 21, 2020
*/
public class FileUtil {
private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);
/**
* Delete file.
*
* @param path the path
* @return true, if successful
*/
public static boolean deleteFile(String path) {
LOG.info("called deleteFile for path: "+path);
boolean deleted = false;
if(path!=null) {
try {
File file = new File(path);
deleted = Files.deleteIfExists(file.toPath());
LOG.info("File "+path+" deleted? "+deleted);
}catch (Exception e) {
LOG.warn("Error on deleting file: "+path, e);
}
}else {
LOG.warn("I cannot delete the file. NullPointer parameter 'path'");
}
return deleted;
}
}

View File

@ -100,7 +100,8 @@ public class MetadataDiscovery {
MetadataProfileReader reader = new MetadataProfileReader(gRSecondaryType);
List<MetadataProfile> profiles = reader.getListOfMetadataProfiles();
LOG.debug("Profiles are " + profiles);
prettyPrintProfiles(profiles);
LOG.info("Profiles are " + profiles.size());
List<NamespaceCategory> categories = reader.getListOfNamespaceCategories();
if (categories == null)
@ -109,17 +110,14 @@ public class MetadataDiscovery {
LOG.debug("All Categories are " + categories);
for (MetadataProfile profile : profiles) {
LOG.debug(
"Wrapping profile with name " + profile.getName() + " and type " + profile.getMetadataType());
LOG.debug("Wrapping profile with name " + profile.getName() + " and type " + profile.getMetadataType());
MetadataFormat metadata = reader.getMetadataFormatForMetadataProfile(profile);
MetaDataProfileBean bean = toMetaDataProfileBean(metadata, categories, profile.getName());
beans.add(bean);
}
LOG.debug("List of beans is " + beans);
prettyPrintList(beans);
LOG.info("Returning " + beans.size() + " profile/s");
} catch (Exception e) {
LOG.error("Error while retrieving metadata beans ", e);
@ -131,6 +129,27 @@ public class MetadataDiscovery {
return beans;
}
private static void prettyPrintProfiles(List<MetadataProfile> profiles) {
if(LOG.isDebugEnabled() && profiles!=null) {
LOG.debug("Pretty print list of profiles: ");
for (MetadataProfile profile : profiles) {
LOG.debug(profile.toString());
}
}
}
private static void prettyPrintList(List<MetaDataProfileBean> beans) {
if(LOG.isDebugEnabled() && beans!=null) {
LOG.debug("Pretty print list of beans: ");
for (MetaDataProfileBean metaDataProfileBean : beans) {
LOG.debug(metaDataProfileBean.toString());
}
}
}
/**
@ -196,6 +215,7 @@ public class MetadataDiscovery {
wrapperObj.setMandatory(metadataField.getMandatory());
wrapperObj.setNote(metadataField.getNote());
MetadataValidator validator = metadataField.getValidator();
LOG.debug(metadataField.getFieldName() +" validator is: "+validator);
if (validator != null)
wrapperObj.setValidator(validator.getRegularExpression());

View File

@ -1,7 +1,5 @@
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;
@ -21,20 +19,17 @@ public class UploadedFileHttpSessionListener implements HttpSessionListener {
@Override
public void sessionDestroyed(HttpSessionEvent event) {
LOG.info("sessionDestroyed called. Session id is: "+event.getSession().getId());
LOG.info("sessionDestroyed called");
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);
if(httpSession!=null) {
LOG.info("Sesson id is: "+httpSession.getId());
List<FileUploaded> listFileUploaded = (List<FileUploaded>) httpSession.getAttribute(ConstantsMPFormBuilder.FILE_UPLOADED_SESSION_ATTR);
if(listFileUploaded!=null) {
LOG.info("found file uploded in session, removing it");
for (FileUploaded fileUploaded : listFileUploaded) {
FileUtil.deleteFile(fileUploaded.getTempSystemPath());
}
}
}
@ -47,8 +42,5 @@ public class UploadedFileHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent arg0) {
LOG.info("sessionCreated called. Session id is: "+arg0.getSession().getId());
//session.setMaxInactiveInterval(5);//in seconds
}
}

View File

@ -5,10 +5,6 @@
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<listener>
<listener-class>org.gcube.portlets.widgets.mpformbuilder.server.UploadedFileHttpSessionListener</listener-class>
</listener>
<!-- Servlets -->
<servlet>
<servlet-name>mpformbuilderServlet</servlet-name>
@ -36,4 +32,9 @@
<welcome-file>MetadataProfileFormBuilder.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.gcube.portlets.widgets.mpformbuilder.server.UploadedFileHttpSessionListener</listener-class>
</listener>
</web-app>