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

add static page handling and root page template

parent 8671b8fa
No related branches found
No related tags found
No related merge requests found
...@@ -4,12 +4,15 @@ import jakarta.ws.rs.GET; ...@@ -4,12 +4,15 @@ import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path; import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam; import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter;
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.Files;
...@@ -32,20 +35,31 @@ public class Static { ...@@ -32,20 +35,31 @@ public class Static {
@GET @GET
public Response getStatic(@PathParam("staticFile") String fileName) { public Response getStatic(@PathParam("staticFile") String fileName) {
logger.info("Loading file " + fileName); logger.info("Loading file " + fileName);
// Get file from resource // // Get file from resource
URL resource = this.getClass().getClassLoader().getResource("static/"+ fileName); // URL resource = this.getClass().getClassLoader().getResource("static/"+ fileName);
if (resource == null) { // if (resource == null) {
logger.info("Resource is null"); // logger.info("Resource is null");
return Response.status(404,"Invalid static file").build(); // return Response.status(404,"Invalid static file").build();
} // }
else { // else {
// try {
// logger.info("Resource is " + resource.toURI());
// File staticFile = new File(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();
// }
// }
StringWriter result = new StringWriter();
VelocityContext context = new VelocityContext();
// Velocity.mergeTemplate("static/" + fileName, "UTF-8",context,result) ;
// return Response.ok(result.toString()).build() ;
//return Response.ok(Velocity.getTemplate("static/" + fileName).toString()).build();
try { try {
File staticFile = new File(resource.toURI()); return Response.ok(this.getClass().getModule().getResourceAsStream("static/" + fileName).readAllBytes()).build();
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();
} }
catch (IOException e) {
return Response.status(500, "Error loading resource").build();
} }
} }
} }
...@@ -12,8 +12,13 @@ import org.apache.velocity.Template; ...@@ -12,8 +12,13 @@ import org.apache.velocity.Template;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
/** /**
* /** * /**
...@@ -35,14 +40,28 @@ public class WebRoot { ...@@ -35,14 +40,28 @@ public class WebRoot {
@GET @GET
@Produces(MediaType.TEXT_HTML) @Produces(MediaType.TEXT_HTML)
public Response getRoot() { public Response getRoot() {
ClassLoader cl = this.getClass().getClassLoader(); try {
URL res = this.getClass().getClassLoader().getResource("templates/root.vm"); //putStream template = this.getClass().getModule().getResourceAsStream("templates/root.vm");
if (res == null) // logger.info("Test: " + new String(this.getClass().getModule().getResourceAsStream("templates/root.vm")
return Response.status(500,"Template not found").build(); // .readAllBytes()) + " # ");
StringWriter result = new StringWriter(); StringWriter result = new StringWriter();
VelocityContext context = new VelocityContext(); VelocityContext context = new VelocityContext();
context.put("functions", new ListCorpusFunctions().listFunctions()); context.put("functions", new ListCorpusFunctions().listFunctions());
Velocity.mergeTemplate("templates/root.vm", "UTF-8",context,result) ; Velocity.evaluate(context,result,"webroot",new String(this.getClass().getModule()
.getResourceAsStream("templates/root.vm").readAllBytes()));
return Response.ok(result.toString()).build() ; return Response.ok(result.toString()).build() ;
} catch (IOException e) {
logger.info("Got exception " + e);
return Response.status(500,"Template not found").build();
}
// URL res = this.getClass().getClassLoader().getResource("templates/root.vm");
//URL res = this.getClass().getResource("template/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() ;
} }
} }
<html>
<head>
<script src="static/bootstrap.js"></script>
<script src="static/jquery-3.6.0.min.js"></script>
<script src="static/main.js"></script>
<link rel="stylesheet" href="static/main.css" as="style" type="text/css">
<link rel="stylesheet" href="static/bootstrap.css" as="style" type="text/css">
</head>
<body>
<h1>Corpus services</h1>
<div class="col lg">
<div class="row">
<input type="file" class="form-control" id="corpus-files" webkitdirectory mozdirectory msdirectory odirectory
directory>
</div>
<div class="row">
<input type="text" id="file-exts" value="txt">
</div>
<div class="row">
<a href="#" class="btn btn-primary" id="send-files">Send files and check data</a>
</div>
<form id="corpus_checker_form" method="post" action="">
<div class="accordion" id="corpus_functions">
#foreach ($function in $functions)
#if ($function.available)
<div class="accordion-item">
<h2 class="accordion-header" id="heading_$function.name">
<div class="row">
<div class="col">
<button
class="accordion-button collapsed"
type="button"
data-bs-toggle="collapse"
data-bs-target="#collapse_$function.name"
aria-expanded="false" aria-controls="collapse_$function.name">
$function.name
</button>
</div>
<div class="col">
<input type="checkbox" class="form-check-input" id="validator_$function.name" value="$function.name">
</input>
</div>
</div>
</h2>
<div id="collapse_$function.name" class="accordion-collapse collapse" aria-labelledby="heading_$function.name"
data-bs-parent="#corpus_functions">
<div class="accordion-body">
Description: $function.description<br>
Usable for: $function.usableForString<br>
#if($function.paramsString)
Parameters:<br>
$function.paramsString
#end
</div>
</div>
</div>
#end
#end
</div>
</form>
</div>
<div class="modal" tabindex="-1" id="check_in_progress_dialogue">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Check in progress</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Check in progress. Results will soon be available here: <a href="report">Report</a></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment