Commit 9f2f0807 authored by acoburn's avatar acoburn
Browse files

Merge branch 'template-default' into 'master'

Changes that allow a custom template to be passed in.

Addresses: #12

See merge request !70
parents 808f92ac 1b7b937d
......@@ -8,6 +8,10 @@ in order to retrieve a MODS version of the resource `a/b/c`:
curl localhost:9103/template/a/b/c
One can have the module use a customized Mustache template by doing the following:
curl localhost:9103/template/a/b/c?templateUri=/path/to/template/template2.mustache
Building
--------
......@@ -54,7 +58,7 @@ The fedora baseUrl value
The location of the mustache template
mustache.template=edu/amherst/acdc/exts/template/mustache/template.mustache
mustache.templateUri=edu/amherst/acdc/exts/template/mustache/template.mustache
The content-type of the rendered template
......
......@@ -13,7 +13,7 @@ fcrepo.baseUrl=localhost:8080/fcrepo/rest
mustache.contentType=text/html
# The mustache template
mustache.contentType=edu/amherst/acdc/template/template.mustache
mustache.templateUri=edu/amherst/acdc/template/template.mustache
# The json+ld context file
jsonld.context=https://acdc.amherst.edu/jsonld/models.json
......@@ -15,12 +15,12 @@
*/
package edu.amherst.acdc.exts.template;
import static org.apache.camel.component.mustache.MustacheConstants.MUSTACHE_RESOURCE_URI;
import static org.apache.camel.Exchange.CONTENT_TYPE;
import static org.apache.camel.Exchange.HTTP_METHOD;
import static org.apache.camel.Exchange.HTTP_PATH;
import static org.apache.camel.Exchange.HTTP_RESPONSE_CODE;
import static org.fcrepo.camel.FcrepoHeaders.FCREPO_IDENTIFIER;
import java.util.Map;
import org.apache.camel.model.dataformat.JsonLibrary;
......@@ -43,7 +43,7 @@ public class EventRouter extends RouteBuilder {
.maximumRedeliveries("{{error.maxRedeliveries}}")
.log("Event Routing Error: ${routeId}");
from("jetty:http://0.0.0.0:{{rest.port}}/template?" +
from("jetty:http://{{rest.host}}:{{rest.port}}/template?" +
"matchOnUriPrefix=true&httpMethodRestrict=GET,OPTIONS&sendServerVersion=false")
.routeId("TemplateTransformation")
.choice()
......@@ -66,11 +66,22 @@ public class EventRouter extends RouteBuilder {
.to("direct:compact")
.to("direct:template");
from("direct:template")
.routeId("TemplateRoute")
.setHeader(CONTENT_TYPE).simple("{{mustache.contentType}}")
.unmarshal().json(JsonLibrary.Jackson, Map.class)
.to("mustache:{{mustache.template}}");
.choice()
.when(header("templateUri").isNull())
.to("direct:defaultTemplate")
.otherwise()
.setHeader(MUSTACHE_RESOURCE_URI).simple("${headers.templateUri}")
/* the MUSTACHE_RESOURCE_URI in header overrides the passed in value below */
.to("mustache:dummy");
from("direct:defaultTemplate")
.routeId("DefaultTemplateRoute")
.to("mustache:{{mustache.templateUri}}");
}
}
......@@ -14,10 +14,10 @@
<cm:property name="rest.host" value="localhost"/>
<cm:property name="rest.port" value="9103"/>
<cm:property name="rest.prefix" value="/template"/>
<cm:property name="mustache.template" value="edu/amherst/acdc/exts/template/template.mustache"/>
<cm:property name="fcrepo.baseUrl" value="localhost:8080/fcrepo/rest"/>
<cm:property name="mustache.templateUri" value="edu/amherst/acdc/exts/template/template.mustache"/>
<cm:property name="mustache.contentType" value="text/html"/>
<cm:property name="jsonld.context" value="https://acdc.amherst.edu/jsonld/models.json"/>
<cm:property name="fcrepo.baseUrl" value="localhost:8080/fcrepo/rest"/>
</cm:default-properties>
</cm:property-placeholder>
......
......@@ -85,4 +85,30 @@ public class RouteTest extends CamelBlueprintTestSupport {
// assert expectations
assertMockEndpointsSatisfied();
}
@Test
public void testRouteCustomTemplate() throws Exception {
context.getRouteDefinition("TemplateRoute").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveAddLast().to("mock:result");
}
});
context.start();
resultEndpoint.expectedMinimumMessageCount(1);
resultEndpoint.expectedHeaderReceived("Content-Type", "text/html");
resultEndpoint.allMessages().body().contains("Fedora Template Service: Foo");
resultEndpoint.allMessages().body().contains("Custom Template: Sometime 2016");
resultEndpoint.allMessages().body().contains("<p>sample description</p>");
template.sendBodyAndHeader(
"{\"title\" : \"Foo\", \"description\" : \"sample description\", \"date\" : \"Sometime 2016\"}",
"templateUri", "/edu/amherst/acdc/exts/template/template2.mustache");
// assert expectations
assertMockEndpointsSatisfied();
}
}
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns#">
<head>
<title>{{body.title}}</title>
</head>
<body>
<h1>Fedora Template Service: {{body.title}}</h1>
<h2>Custom Template: {{body.date}}</h2>
<p>{{body.description}}</p>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment