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