71 lines
2.1 KiB
Java
71 lines
2.1 KiB
Java
/*********************************************************************************************************************************
|
|
* 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.faces.bean.ManagedBean;
|
|
import javax.faces.bean.ManagedProperty;
|
|
import javax.faces.context.ExternalContext;
|
|
import javax.faces.context.FacesContext;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.Part;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Files;
|
|
|
|
/**
|
|
* Bean for file upload.
|
|
*/
|
|
@ManagedBean(name = "UploadFileBean")
|
|
public class UploadDocumentBean {
|
|
|
|
private static final Logger logger = Logger.getLogger( UploadDocumentBean.class );
|
|
|
|
/**
|
|
* The part of the form containing the file.
|
|
*/
|
|
private Part document;
|
|
|
|
@ManagedProperty(value="#{applicationBean}")
|
|
private ApplicationBean application;
|
|
|
|
/**
|
|
* Saves the sent file.
|
|
*/
|
|
public void save(final HttpServletRequest request) throws IOException {
|
|
if (!request.isUserInRole("admin")) {
|
|
final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
|
|
externalContext.responseReset();
|
|
externalContext.setResponseStatus(403);
|
|
return;
|
|
}
|
|
|
|
try (final InputStream input = document.getInputStream()) {
|
|
Files.copy(input, new File(application.getDocumentsDirectory(), document.getSubmittedFileName()).toPath());
|
|
}
|
|
catch (final IOException e) {
|
|
logger.error( "Error", e );
|
|
}
|
|
|
|
FacesContext.getCurrentInstance().getExternalContext().redirect("index.jsp");
|
|
}
|
|
|
|
public Part getDocument() {
|
|
return document;
|
|
}
|
|
|
|
public void setDocument(final Part document) {
|
|
this.document = document;
|
|
}
|
|
|
|
public void setApplication( ApplicationBean application ) {
|
|
this.application = application;
|
|
}
|
|
}
|