diff --git a/src/main/java/de/uni_hamburg/corpora/server/CorpusChecker.java b/src/main/java/de/uni_hamburg/corpora/server/CorpusChecker.java index 0722cb3da74bd6a837e38b2f39b6242215bc968d..18af8b05a76114579aa37020786f11d0207e8892 100644 --- a/src/main/java/de/uni_hamburg/corpora/server/CorpusChecker.java +++ b/src/main/java/de/uni_hamburg/corpora/server/CorpusChecker.java @@ -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) { diff --git a/src/main/java/de/uni_hamburg/corpora/server/ExceptionLogger.java b/src/main/java/de/uni_hamburg/corpora/server/ExceptionLogger.java index 1ad45bc8ffbbd059041d2fd503bf253a4936e10e..8b206aa0f091f6d5f6f9c98853c515210d9112f6 100644 --- a/src/main/java/de/uni_hamburg/corpora/server/ExceptionLogger.java +++ b/src/main/java/de/uni_hamburg/corpora/server/ExceptionLogger.java @@ -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(); } } diff --git a/src/main/java/de/uni_hamburg/corpora/server/Report.java b/src/main/java/de/uni_hamburg/corpora/server/Report.java index 0cc445f84d3fe53f92eda46fe6ac8bd42b24991a..ca3c89435c4b3b16509951efd7d7a9927de9fb57 100644 --- a/src/main/java/de/uni_hamburg/corpora/server/Report.java +++ b/src/main/java/de/uni_hamburg/corpora/server/Report.java @@ -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); diff --git a/src/main/java/de/uni_hamburg/corpora/server/SendFile.java b/src/main/java/de/uni_hamburg/corpora/server/SendFile.java index 372f12d0c7bf9c28b3976dc4e128eb0cda545323..e844fccf0ef684f135500a05f67acbab898400e3 100644 --- a/src/main/java/de/uni_hamburg/corpora/server/SendFile.java +++ b/src/main/java/de/uni_hamburg/corpora/server/SendFile.java @@ -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) { - // New folder in temp - String corpusDirStr = System.getProperty("java.io.tmpdir") + "/corpus-files"; - File corpusDir = new File(corpusDirStr); - // Create if folder is missing - if (!corpusDir.exists()) - corpusDir.mkdirs(); try { + // New folder in temp + 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()) { +// 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();