Skip to content
Snippets Groups Projects
Commit e591081f authored by Lange, Dr. Herbert's avatar Lange, Dr. Herbert
Browse files

fix handling of temp folder

parent 678d7dec
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,8 @@ import jakarta.ws.rs.core.Response;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
......@@ -53,27 +55,34 @@ class CorpusThread extends Thread {
String callbackUrl; // URL to be called when the task is done, giving an empty string means skipping the callback
CorpusThread(String name, String infile, String outfile, String functions, Properties properties, String token,
String callbackUrl) {
String callbackUrl) throws IOException {
this.corpusName=name;
if (infile.equals("tmp"))
this.inFile = System.getProperty("java.io.tmpdir") + "/corpus-files";
if (infile.equals("tmp")) {
if (System.getProperty("corpusDir") != null)
this.inFile = System.getProperty("corpusDir");
else
throw new IOException("Corpus files not ready");
}
else
this.inFile = infile;
this.functionNames = functions ;
this.props = properties;
this.token = Objects.requireNonNullElse(token, "tmp");
if (outfile.equals("tmp")) {
File tmpDir = new File(System.getProperty("java.io.tmpdir") + "/" + token);
File logDir = Paths.get(System.getProperty("java.io.tmpdir"),token).toFile();
// Create parent directory if it is missing
if (!tmpDir.exists())
tmpDir.mkdirs();
this.outFile = System.getProperty("java.io.tmpdir") + "/" + token + "/report.html";
if (!logDir.exists())
if (!logDir.mkdirs())
throw new IOException("Failed to create folders "+ logDir);
this.outFile = Paths.get(logDir.toString(), "report.html").toString();
}
else
this.outFile = outfile;
this.token = token ;
this.callbackUrl = callbackUrl ;
logger.info("Input: " + this.inFile + " output: " + this.outFile + " functions: " + functions + " params: " +
this.props + " token: " + this.token + " callback: " + this.callbackUrl);
}
public void run() {
......@@ -152,6 +161,13 @@ class CorpusThread extends Thread {
//XStream xstream = new XStream();
//String reportOutput = xstream.toXML(rawStatistics);
// Cleanup folder
try {
FileUtils.deleteDirectory(new File(inFile));
System.getProperties().remove("corpusDir");
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Writing report");
try {
BufferedWriter out = new BufferedWriter(new FileWriter(outFile));
......@@ -217,7 +233,7 @@ public class CorpusChecker {
@QueryParam("functions") String functions,
@QueryParam("params") String paramStr,
@QueryParam("token") String token,
@QueryParam("callback") String callbackUrl) {
@QueryParam("callback") String callbackUrl) throws IOException {
boolean error = false ;
ArrayList<String> missing = new ArrayList<>();
if (input == null) {
......
......@@ -20,9 +20,10 @@ public class ExceptionLogger implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception e) {
log.severe("Exception:" + e);
//log.severe("Exception:" + e);
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.severe("Exception:" + e + "\n" + sw);
return Response.status(500).entity(sw.toString()).build();
}
}
......@@ -30,6 +30,8 @@ public class Report {
@GET
public Response getReport(@QueryParam("token") String token) {
// TODO place report somewhere else because files should be deleted after check
if (token == null)
token = "tmp";
String reportFileName = System.getProperty("java.io.tmpdir") + "/" + token + "/report.html";
logger.info("Loading report file " + reportFileName);
File reportFile = new File(reportFileName);
......
......@@ -9,8 +9,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
/**
......@@ -63,29 +63,44 @@ public class SendFile {
*/
@POST
public Response getFile(String fileData) {
try {
// New folder in temp
String corpusDirStr = System.getProperty("java.io.tmpdir") + "/corpus-files";
File corpusDir = new File(corpusDirStr);
File corpusDir;
if (System.getProperty("corpusDir") == null) {
corpusDir = Files.createTempDirectory("corpus-files").toFile();
System.setProperty("corpusDir", corpusDir.toString());
}
else {
corpusDir = new File(System.getProperty("corpusDir"));
}
// Create if folder is missing
if (!corpusDir.exists())
corpusDir.mkdirs();
try {
// if (!corpusDir.exists()) {
// if (!corpusDir.mkdirs()) {
// throw new IOException("Failed to create directory " + corpusDir);
// }
// }
// Parse json
CorpusFile cf = new ObjectMapper().readerFor(CorpusFile.class).readValue(fileData);
//logger.info("Got: " + cf);
// Create a new file in temp folder and write the data we got
File file = new File(new URI(corpusDir.toURI() + "/" + cf.getName())) ;
File file = Paths.get(corpusDir.getPath(),cf.getName()).toFile();
logger.info("FILE: " + file);
// Delete file if it already exists
if (file.exists())
file.delete();
if (!file.createNewFile()) throw new IOException("Cannot create file");
logger.info(String.valueOf(file.toURI()));
if (file.exists()) {
boolean deleted = file.delete();
if (!deleted)
throw new IOException("Failed to delete file " + file);
}
if (!file.createNewFile()) throw new IOException("Failed to create file " + file);
FileOutputStream fs = new FileOutputStream(file);
fs.write(cf.getData());
fs.close();
// logger.info("Wrote file " + file + ": " + file.exists());
// logger.info(Arrays.asList(corpusDir.listFiles()).toString());
// Everything okay
return Response.ok().build();
} catch (IOException | URISyntaxException e) {
} catch (IOException e) {
// On exception print error and return error code
logger.error("Error reading " + fileData + ": " + e);
e.printStackTrace();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment