From 4dd7010378bc63e39081e06c5c4b2dd968e1bf2c Mon Sep 17 00:00:00 2001
From: "Lange, Dr. Herbert" <herbert.lange@uni-hamburg.de>
Date: Mon, 4 Oct 2021 15:53:46 +0200
Subject: [PATCH] add additional handlers to have a working local corpus
 services interface

---
 .../uni_hamburg/corpora/server/Favicon.java   | 30 ++++++
 .../de/uni_hamburg/corpora/server/Report.java | 46 +++++++++
 .../uni_hamburg/corpora/server/SendFile.java  | 99 +++++++++++++++++++
 .../de/uni_hamburg/corpora/server/Static.java | 51 ++++++++++
 .../uni_hamburg/corpora/server/WebRoot.java   | 48 +++++++++
 5 files changed, 274 insertions(+)
 create mode 100644 src/main/java/de/uni_hamburg/corpora/server/Favicon.java
 create mode 100644 src/main/java/de/uni_hamburg/corpora/server/Report.java
 create mode 100644 src/main/java/de/uni_hamburg/corpora/server/SendFile.java
 create mode 100644 src/main/java/de/uni_hamburg/corpora/server/Static.java
 create mode 100644 src/main/java/de/uni_hamburg/corpora/server/WebRoot.java

diff --git a/src/main/java/de/uni_hamburg/corpora/server/Favicon.java b/src/main/java/de/uni_hamburg/corpora/server/Favicon.java
new file mode 100644
index 0000000..382d744
--- /dev/null
+++ b/src/main/java/de/uni_hamburg/corpora/server/Favicon.java
@@ -0,0 +1,30 @@
+package de.uni_hamburg.corpora.server;
+
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Favicon
+ */
+
+@Path("/{fileName: .*ico}")
+public class Favicon {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
+    /**
+     * Method handling HTTP GET requests. The returned object will be sent
+     * to the client as "text/plain" media type.
+     *
+     * @return String that will be returned as a text/plain response.
+     */
+    @GET
+    @Produces({"image/x-icon"})
+    public Response getFavicon() {
+        logger.info("Here");
+        return Response.ok().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
new file mode 100644
index 0000000..eb3ab28
--- /dev/null
+++ b/src/main/java/de/uni_hamburg/corpora/server/Report.java
@@ -0,0 +1,46 @@
+package de.uni_hamburg.corpora.server;
+
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.core.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+
+/**
+ * @author bba1792 Dr. Herbert Lange
+ * @version 20211004
+ * Resource to show an existing local report
+ * Scope: local
+ */
+@Path("report")
+public class Report {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
+    /**
+     * Method handling HTTP GET requests for the local report.
+     *
+     * @return Response containing the file or an error code
+     */
+    @GET
+    public Response getReport() {
+        String reportFileName = System.getProperty("java.io.tmpdir") + "/corpus-files/report.html";
+        logger.info("Loading report file " + reportFileName);
+        File reportFile = new File(reportFileName);
+        if (!reportFile.exists()) {
+            logger.info("Report is missing");
+            return Response.status(404,"Invalid report file").build();
+        }
+        else {
+            try {
+                return Response.ok(new FileInputStream(reportFile), Files.probeContentType(reportFile.toPath())).build();
+            } catch (IOException e) {
+                return Response.status(500, "Error loading file").build();
+            }
+        }
+    }
+}
diff --git a/src/main/java/de/uni_hamburg/corpora/server/SendFile.java b/src/main/java/de/uni_hamburg/corpora/server/SendFile.java
new file mode 100644
index 0000000..568cf9f
--- /dev/null
+++ b/src/main/java/de/uni_hamburg/corpora/server/SendFile.java
@@ -0,0 +1,99 @@
+package de.uni_hamburg.corpora.server;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import jakarta.ws.rs.*;
+import jakarta.ws.rs.core.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+import java.util.Base64;
+
+/**
+ * @author bba1792 Dr. Herbert Lange
+ * @version 20211004
+ * Class handling files sent to the server
+ */
+@Path("/send")
+public class SendFile {
+    public static class CorpusFile {
+
+        String name;
+        String type;
+        byte[] data;
+
+        @JsonCreator
+        public CorpusFile(
+                @JsonProperty("name") String name,
+                @JsonProperty("data") String data) throws IOException {
+            this.name = name;
+            String[] parts = data.split(";");
+            if (parts.length == 1)
+                throw new IOException("Invalid data") ;
+            this.type = parts[0].substring(5);
+            this.data = Base64.getDecoder().decode(parts[1].substring(7));
+        }
+
+        public String toString() {
+            return "name:" + this.name + ", type:" + this.type + ", data:" + new String(this.data);
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public String getType() {
+            return type;
+        }
+
+        public byte[] getData() {
+            return data;
+        }
+    }
+    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
+    /**
+     * Accepts files sent via post and data-url-encoded. Only for local use because we don't avoid potential
+     * conflicts with parallel uploads!
+     *
+     * @return Response containing the file or an error code
+     */
+    @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 {
+            // 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())) ;
+            // 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()));
+            FileOutputStream fs = new FileOutputStream(file);
+            fs.write(cf.getData());
+            fs.close();
+            // Everything okay
+            return Response.ok().build();
+        } catch (IOException | URISyntaxException e) {
+            // On exception print error and return error code
+            logger.error("Error reading " + fileData + ": " + e);
+            e.printStackTrace();
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),e.toString()).build();
+        }
+    }
+}
diff --git a/src/main/java/de/uni_hamburg/corpora/server/Static.java b/src/main/java/de/uni_hamburg/corpora/server/Static.java
new file mode 100644
index 0000000..a2df865
--- /dev/null
+++ b/src/main/java/de/uni_hamburg/corpora/server/Static.java
@@ -0,0 +1,51 @@
+package de.uni_hamburg.corpora.server;
+
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.core.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+
+/**
+ * @author bba1792 Dr. Herbert Lange
+ * @version 20211004
+ * Class handling static files
+ */
+@Path("/static/{staticFile}")
+public class Static {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
+    /**
+     * Method handling HTTP GET requests for static files. The returned object will be sent
+     * to the client
+     *
+     * @return Response containing the file or an error code
+     */
+    @GET
+    public Response getStatic(@PathParam("staticFile") String fileName) {
+        logger.info("Loading file " + fileName);
+        // Get file from resource
+        URL resource = this.getClass().getClassLoader().getResource("static/"+ fileName);
+        if (resource == null) {
+            logger.info("Resource is null");
+            return Response.status(404,"Invalid static file").build();
+        }
+        else {
+            try {
+                File staticFile = new File(resource.toURI());
+                logger.info("Resource is " + resource.toURI());
+                return Response.ok(new FileInputStream(staticFile),Files.probeContentType(staticFile.toPath())).build();
+            } catch (URISyntaxException | IOException e) {
+                return Response.status(500, "Error loading file").build();
+            }
+        }
+    }
+}
diff --git a/src/main/java/de/uni_hamburg/corpora/server/WebRoot.java b/src/main/java/de/uni_hamburg/corpora/server/WebRoot.java
new file mode 100644
index 0000000..df6c349
--- /dev/null
+++ b/src/main/java/de/uni_hamburg/corpora/server/WebRoot.java
@@ -0,0 +1,48 @@
+package de.uni_hamburg.corpora.server;
+
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.MediaType;
+
+import jakarta.ws.rs.core.Response;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.Template;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.StringWriter;
+import java.net.URL;
+
+/**
+ * /**
+ * @author bba1792 Dr. Herbert Lange
+ * @version 20211004
+ * Root resource (exposed at "/" path)
+ */
+@Path("/")
+public class WebRoot {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
+
+    /**
+     * Method handling HTTP GET requests. The returned object will be sent
+     * to the client as "text/plain" media type.
+     *
+     * @return String that will be returned as a text/plain response.
+     */
+    @GET
+    @Produces(MediaType.TEXT_HTML)
+    public Response getRoot() {
+        ClassLoader cl = this.getClass().getClassLoader();
+        URL res = this.getClass().getClassLoader().getResource("templates/root.vm");
+        if (res == null)
+            return Response.status(500,"Template not found").build();
+        StringWriter result = new StringWriter();
+        VelocityContext context = new VelocityContext();
+        context.put("functions", new ListCorpusFunctions().listFunctions());
+        Velocity.mergeTemplate("templates/root.vm", "UTF-8",context,result) ;
+        return Response.ok(result.toString()).build() ;
+    }
+}
-- 
GitLab