package net.sourceforge.fenixedu.domain.jobs; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import org.apache.axis.utils.ByteArrayOutputStream; import org.apache.log4j.lf5.util.StreamUtils; import pt.ist.fenixWebFramework.services.Service; public class JobExecutionFileInput extends JobExecutionRequestor { private static final String FILE_INPUT = "fileInput"; private String filename; private String contentType; private transient InputStream stream; private transient byte[] content; public JobExecutionFileInput() { super(); } public void setInputStream(InputStream stream) { this.stream = stream; } public InputStream getInputStream() { try { if (content == null && stream != null) { ByteArrayOutputStream output = new ByteArrayOutputStream(); StreamUtils.copyThenClose(stream, output); content = output.toByteArray(); } else if (content == null && getFileInput() != null) { content = getFileInput().getContent(); } return content != null ? new ByteArrayInputStream(content) : null; } catch (IOException e) { throw new DomainException("error.job.ioexception", e); } } public void setInputFilename(String filename) { this.filename = filename; } public String getInputFilename() { return filename; } public void setInputContentType(String contentType) { this.contentType = contentType; } public String getInputContentType() { return contentType; } public void setFileInput(JobFileInput input) { setDomainObject(FILE_INPUT, input); } public JobFileInput getFileInput() { return getDomainObject(FILE_INPUT); } @Override public boolean isFormInputNeeded() { return true; } @Service public void saveFile() { getInputStream(); // force consumption JobFileInput input = new JobFileInput(getInputFilename(), content); input.setContentType(getInputContentType()); setFileInput(input); } }