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 0000000000000000000000000000000000000000..382d74409ee38cbf91894ce4043750f883dee162 --- /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 0000000000000000000000000000000000000000..eb3ab28a0adb83b087878b6ac707c629c3b9cd06 --- /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 0000000000000000000000000000000000000000..568cf9fc6c92fd5a08ccb2678d4a78db7236c7bd --- /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 0000000000000000000000000000000000000000..a2df865e07016f27a5cadf6386d7b71086a5522a --- /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 0000000000000000000000000000000000000000..df6c3493284293a22ad9c2e8c47777b9e20c858f --- /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() ; + } +}