/********************************************************************************************************************************* * Autorskie Prawa Majątkowe - Moose Spółka z ograniczoną odpowiedzialnością * Copyright 2019 Moose Spółka z ograniczoną odpowiedzialnością ********************************************************************************************************************************/ package eu.mooseinc.documents.explorer.bean; import eu.mooseinc.documents.explorer.ApplicationBean; import org.apache.log4j.Logger; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.RequestScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Bean for operations related to files. */ @RequestScoped @ManagedBean(name = "FilesBean") public class FilesBean { private static final Logger logger = Logger.getLogger( FilesBean.class ); private final List documents = new ArrayList<>(); @ManagedProperty(value="#{applicationBean}") private ApplicationBean application; @PostConstruct public void init() { try (Stream stream = Files.walk(Paths.get(application.getDocumentsDirectory()))) { this.documents.addAll( stream .filter(Files::isRegularFile) .map(file -> file.getFileName().toString()) .collect(Collectors.toList()) ); } catch (final IOException e) { logger.error( "Read documents error", e ); } } /** * Finds uploaded files on the disk. * * @return List of files available for download. */ public List getDocuments() { return documents; } /** * Returns requested file. * * @param documentName The name of the requested file. */ public void downloadDocument(final String documentName) { final FacesContext facesContext = FacesContext.getCurrentInstance(); final ExternalContext externalContext = facesContext.getExternalContext(); final Path pathFile = Paths.get(application.getDocumentsDirectory() + File.separator + documentName); final File file = pathFile.toFile(); if (!file.exists()) { externalContext.responseReset(); externalContext.setResponseStatus(404); return; } externalContext.responseReset(); try { externalContext.setResponseContentType(Files.probeContentType(pathFile)); // mimetype externalContext.setResponseContentLength((int) file.length()); // length final String attachmentName = MessageFormat.format("attachment; filename=\"{0}\"", documentName); externalContext.setResponseHeader("Content-Disposition", attachmentName); final OutputStream outputStream = externalContext.getResponseOutputStream(); Files.copy(pathFile, outputStream); facesContext.responseComplete(); } catch (final IOException e) { logger.error( "Error ", e ); } } /** * Deletes the file from the disk. * * @param documentName The name of the file to be deleted. * * @throws IOException Exception thrown during redirection. */ public void deleteDocument(final String documentName, final HttpServletRequest request) throws IOException { if (!request.isUserInRole("admin")) { final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); externalContext.responseReset(); externalContext.setResponseStatus(403); return; } final File fileToDelete = Paths.get(application.getDocumentsDirectory() + File.separator + documentName).toFile(); if (fileToDelete.exists() && fileToDelete.isFile()) Files.delete( fileToDelete.toPath() ); FacesContext.getCurrentInstance().getExternalContext().redirect("index.jsp"); } public void setApplication( ApplicationBean application ) { this.application = application; } }