Skip to content
Snippets Groups Projects
Commit dddbf2bf authored by Andreas Schärtl's avatar Andreas Schärtl
Browse files

uoapi: set up html rendering

I'll only need it for a demo, probably. But it's nice to have.
parent 77259ea0
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@
<configuration>
<archive>
<manifest>
<mainClass>info.mathhub.uloapi.rest.Main</mainClass>
<mainClass>info.mathhub.uloapi.html.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
......@@ -41,7 +41,7 @@
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>info.mathhub.uloapi.rest.Main</mainClass>
<mainClass>info.mathhub.uloapi.html.Main</mainClass>
</configuration>
</plugin>
</plugins>
......@@ -78,5 +78,10 @@
<artifactId>graphdb-free-runtime</artifactId>
<version>9.2.0</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-freemarker</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package info.mathhub.uloapi.html;
import org.eclipse.jetty.http.HttpStatus;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.template.freemarker.FreeMarkerEngine;
import java.util.HashMap;
import java.util.Map;
public class Errors {
private Errors() {};
/**
* Singleton instance of the Freemarker engine for use with errors.
* Used in {@link Errors#createError}.
*/
private static final FreeMarkerEngine freemarker = new FreeMarkerEngine();
public static final Route notFound = (Request request, Response response) -> {
return createError(HttpStatus.NOT_FOUND_404).handle(request, response);
};
public static final Route notAcceptable = (Request request, Response response) -> {
return createError(HttpStatus.NOT_ACCEPTABLE_406).handle(request, response);
};
public static final Route notImplemented = (Request request, Response response) -> {
return createError(HttpStatus.NOT_IMPLEMENTED_501).handle(request, response);
};
public static final Route internalServerError = (Request request, Response response) -> {
return createError(HttpStatus.INTERNAL_SERVER_ERROR_500).handle(request, response);
};
private static Route createError(int status) {
return (Request request, Response response) -> {
// set content type
response.type("text/html");
// prepare model
final Map<String, Object> model = new HashMap<>();
model.put("status", status);
model.put("description", HttpStatus.getMessage(status));
// render
final ModelAndView modelAndView = new ModelAndView(model, "error.flt");
synchronized (Errors.freemarker) {
return Errors.freemarker.render(modelAndView);
}
};
}
}
package info.mathhub.uloapi.html;
import spark.Filter;
import spark.Request;
import spark.Response;
public class Filters {
private Filters() {};
public static final Filter setHtmlContentType = (Request request, Response response) -> {
response.type("text/html");
};
}
package info.mathhub.uloapi.html;
import spark.template.freemarker.FreeMarkerEngine;
import static spark.Spark.*;
/**
* Entry point of the JSON/REST API for the underlying ULO/RDF store.
*/
public class Main {
private Main() {};
public static void main(String[] args) {
get("/", Routes.index, new FreeMarkerEngine());
notFound(Errors.notFound);
internalServerError(Errors.internalServerError);
after(Filters.setHtmlContentType);
}
}
package info.mathhub.uloapi.html;
import info.mathhub.uloapi.query.Query;
import spark.*;
import java.util.HashMap;
import java.util.Map;
/**
* This class contains all routes of our application.
*/
public class Routes {
private Routes() {};
public static final TemplateViewRoute index = (Request request, Response response) -> {
return new ModelAndView(null, "index.flt");
};
}
<#macro page_title>
Page Title
</#macro>
<#macro page_main>
<h1>Page Title</h1>
<p>Page Body</p>
</#macro>
<#macro display_page>
<!DOCTYPE html>
<html>
<head>
<title><@page_title/></title>
<style>
* {
font-family: "Open Sans", Helvetica, Arial;
font-size: 12pt;
}
a {
color: blue;
}
</style>
</head>
<body>
<main>
<@page_main/>
</main>
</body>
</html>
</#macro>
\ No newline at end of file
<#include "base.flt">
<#macro page_title>
Error ${status}
</#macro>
<#macro page_main>
<h1>Error ${status}: ${description}</h1>
<p>Grow old or die trying.</p>
</#macro>
<@display_page/>
<#include "base.flt">
<#macro page_title>
ULO/RDF Endpoint
</#macro>
<#macro page_main>
<h1>ULO/RDF Endpoint</h1>
<p>This is a technology demo of an ULO/RDF Endpoint.</p>
<h2>Introduction</h2>
<p>
Working with RDF triplets is somewhat easy with the <a href="https://rdf4j.org">RDF4J</a> library.
This demo uses RDF4J to illustrate that (1) a connection to an existing database can be established
and (2) this database is filled with some sample data from <a href="https://mathhub.info/">MathHub</a>
repositories.
</p>
<p>
It is no incident that this demo is hacked together with a JVM-based stack. Potential integration
into MMT should be easier with lessons learned from this demo.
</p>
<h2>Demos</h2>
<ul>
<li><a href="/statistics">Show (not every interesting) Statistics</a> about the imported ULO/RDF dataset.</li>
</ul>
</#macro>
<@display_page/>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment