Skip to content

Commit

Permalink
Add a way to compute asset's url from the "routes" thymeleaf object (#13
Browse files Browse the repository at this point in the history
)

Signed-off-by: Clement Escoffier <[email protected]>
  • Loading branch information
cescoffier committed Jul 1, 2014
1 parent 9539a8f commit c0701a4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.slf4j.LoggerFactory;
import org.thymeleaf.messageresolver.IMessageResolver;
import org.thymeleaf.templateresolver.TemplateResolver;
import org.wisdom.api.asset.Assets;
import org.wisdom.api.configuration.ApplicationConfiguration;
import org.wisdom.api.router.Router;
import org.wisdom.api.templates.Template;
Expand Down Expand Up @@ -77,6 +78,9 @@ public class ThymeleafTemplateCollector implements TemplateEngine {
@Requires
private Router router;

@Requires(optional = true)
private Assets assets;

/**
* Creates the collector.
*
Expand Down Expand Up @@ -176,7 +180,7 @@ public ThymeLeafTemplateImplementation addTemplate(URL templateURL) {
// Already existing.
return template;
}
template = new ThymeLeafTemplateImplementation(engine, templateURL, router);
template = new ThymeLeafTemplateImplementation(engine, templateURL, router, assets);
ServiceRegistration<Template> reg = context.registerService(Template.class, template,
template.getServiceProperties());
registrations.put(template, reg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.google.common.collect.ImmutableMap;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.wisdom.api.Controller;
import org.wisdom.api.asset.Asset;
import org.wisdom.api.asset.Assets;
import org.wisdom.api.router.Router;

/**
Expand All @@ -47,7 +49,7 @@ public class Routes {
/**
* The error message when the route cannot be found.
*/
public static final String ERR_FIND = "Cannot find the reverse route for ";
public static final String ERR_FIND_ROUTE = "Cannot find the reverse route for ";

/**
* Suffix of the error message when using paramters.
Expand All @@ -59,6 +61,11 @@ public class Routes {
*/
private final Router router;

/**
* The asset control point.
*/
private final Assets assets;

/**
* The current controller.
*/
Expand All @@ -70,9 +77,10 @@ public class Routes {
* @param router the router
* @param controller the controller
*/
public Routes(Router router, Controller controller) {
public Routes(Router router, Assets assets, Controller controller) {
this.router = router;
this.controller = controller;
this.assets = assets;
}

/**
Expand All @@ -86,7 +94,7 @@ public Routes(Router router, Controller controller) {
public String route(String controllerClass, String method) {
String route = router.getReverseRouteFor(controllerClass, method);
if (route == null) {
throw new TemplateProcessingException(ERR_FIND + controllerClass + "#" +
throw new TemplateProcessingException(ERR_FIND_ROUTE + controllerClass + "#" +
method);
}
return route;
Expand All @@ -102,7 +110,7 @@ public String route(String controllerClass, String method) {
public String route(String method) {
String route = router.getReverseRouteFor(controller, method);
if (route == null) {
throw new TemplateProcessingException(ERR_FIND + controller.getClass().getName() +
throw new TemplateProcessingException(ERR_FIND_ROUTE + controller.getClass().getName() +
"#" + method);
}
return route;
Expand All @@ -122,7 +130,7 @@ public String route(String controllerClass, String method, String var1, Object v
ImmutableMap<String, Object> params = ImmutableMap.<String, Object>of(var1, value1);
String route = router.getReverseRouteFor(controllerClass, method, params);
if (route == null) {
throw new TemplateProcessingException(ERR_FIND + controller.getClass().getName() +
throw new TemplateProcessingException(ERR_FIND_ROUTE + controller.getClass().getName() +
"#" + method + WITH_PARAM + params);
}
return route;
Expand All @@ -148,7 +156,7 @@ public String route(String controllerClass, String method,
var2, value2);
String route = router.getReverseRouteFor(controllerClass, method, params);
if (route == null) {
throw new TemplateProcessingException(ERR_FIND + controller.getClass().getName() +
throw new TemplateProcessingException(ERR_FIND_ROUTE + controller.getClass().getName() +
"#" + method + WITH_PARAM + params);
}
return route;
Expand Down Expand Up @@ -178,7 +186,7 @@ public String route(String controllerClass, String method,
var3, value3);
String route = router.getReverseRouteFor(controllerClass, method, params);
if (route == null) {
throw new TemplateProcessingException(ERR_FIND + controller.getClass().getName() +
throw new TemplateProcessingException(ERR_FIND_ROUTE + controller.getClass().getName() +
"#" + method + " with params : " + params);
}
return route;
Expand Down Expand Up @@ -212,7 +220,7 @@ public String route(String controllerClass, String method,
var4, value4);
String route = router.getReverseRouteFor(controllerClass, method, params);
if (route == null) {
throw new TemplateProcessingException(ERR_FIND + controller.getClass().getName() +
throw new TemplateProcessingException(ERR_FIND_ROUTE + controller.getClass().getName() +
"#" + method + " with params : " + params);
}
return route;
Expand Down Expand Up @@ -250,7 +258,7 @@ public String route(String controllerClass, String method,
var5, value5);
String route = router.getReverseRouteFor(controllerClass, method, params);
if (route == null) {
throw new TemplateProcessingException(ERR_FIND + controller.getClass().getName() +
throw new TemplateProcessingException(ERR_FIND_ROUTE + controller.getClass().getName() +
"#" + method + " with params : " + params);
}
return route;
Expand Down Expand Up @@ -358,4 +366,18 @@ public String route(String method,
var1, value1, var2, value2, var3, value3, var4, value4, var5, value5
);
}

/**
* Gets the url of the given asset. Throws an exception if the asset cannot be found.
*
* @param path the asset's path
* @return the url
*/
public String asset(String path) {
Asset asset = assets.assetAt(path);
if (asset == null) {
throw new TemplateProcessingException("Cannot find the URL of the asset " + path);
}
return asset.getPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.commons.io.FilenameUtils;
import org.wisdom.api.Controller;
import org.wisdom.api.asset.Assets;
import org.wisdom.api.http.MimeTypes;
import org.wisdom.api.http.Renderable;
import org.wisdom.api.router.Router;
Expand All @@ -44,14 +45,17 @@ public class ThymeLeafTemplateImplementation implements Template {
private final URL url;
private final String name;
private final Router router;
private final Assets assets;
private WisdomTemplateEngine templateEngine;

public ThymeLeafTemplateImplementation(WisdomTemplateEngine templateEngine, File templateFile, Router router
) throws MalformedURLException {
this(templateEngine, templateFile.toURI().toURL(), router);
public ThymeLeafTemplateImplementation(WisdomTemplateEngine templateEngine, File templateFile, Router router,
Assets assets
) throws MalformedURLException {
this(templateEngine, templateFile.toURI().toURL(), router, assets);
}

public ThymeLeafTemplateImplementation(WisdomTemplateEngine templateEngine, URL templateURL, Router router) {
public ThymeLeafTemplateImplementation(WisdomTemplateEngine templateEngine, URL templateURL, Router router,
Assets assets) {
this.templateEngine = templateEngine;
this.url = templateURL;
// The name of the template is its relative path against its template root
Expand All @@ -65,6 +69,7 @@ public ThymeLeafTemplateImplementation(WisdomTemplateEngine templateEngine, URL
externalForm.length() - (ThymeleafTemplateCollector.THYMELEAF_TEMPLATE_EXTENSION.length() + 1));
}
this.router = router;
this.assets = assets;
}

public URL getURL() {
Expand Down Expand Up @@ -111,12 +116,12 @@ public String mimetype() {
*/
@Override
public Renderable<?> render(Controller controller, Map<String, Object> variables) {
return templateEngine.process(this, controller, router, variables);
return templateEngine.process(this, controller, router, assets, variables);
}

@Override
public Renderable<?> render(Controller controller) {
return templateEngine.process(this, controller, router, Collections.<String, Object>emptyMap());
return templateEngine.process(this, controller, router, assets, Collections.<String, Object>emptyMap());
}

public Dictionary<String, ?> getServiceProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.wisdom.api.Controller;
import org.wisdom.api.asset.Assets;
import org.wisdom.api.bodies.RenderableString;
import org.wisdom.api.http.MimeTypes;
import org.wisdom.api.router.Router;
Expand Down Expand Up @@ -58,7 +59,7 @@ public WisdomTemplateEngine() {
* @param variables the template parameters
* @return the rendered HTML page
*/
public RenderableString process(Template template, Controller controller, Router router, Map<String,
public RenderableString process(Template template, Controller controller, Router router, Assets assets, Map<String,
Object> variables) {
Context ctx = new Context();
// Add session
Expand All @@ -79,7 +80,7 @@ public RenderableString process(Template template, Controller controller, Router

// Add variable.
ctx.setVariables(variables);
ctx.setVariable(Routes.ROUTES_VAR, new Routes(router, controller));
ctx.setVariable(Routes.ROUTES_VAR, new Routes(router, assets, controller));

StringWriter writer = new StringWriter();
this.process(template.fullName(), ctx, writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public class ThymeLeafTemplateImplementationTest {
@Test
public void testNameExtractionFromURL() throws MalformedURLException {
URL url = new URL("file://38.0:0/templates/footer.thl.html");
ThymeLeafTemplateImplementation template = new ThymeLeafTemplateImplementation(null, url, null);
ThymeLeafTemplateImplementation template = new ThymeLeafTemplateImplementation(null, url, null, null);
assertThat(template.fullName()).isEqualTo(url.toExternalForm());
assertThat(template.name()).isEqualTo("footer");
}

@Test
public void testNameExtractionFromURLUsingSubFolder() throws MalformedURLException {
URL url = new URL("file://38.0:0/templates/hello/footer.thl.html");
ThymeLeafTemplateImplementation template = new ThymeLeafTemplateImplementation(null, url, null);
ThymeLeafTemplateImplementation template = new ThymeLeafTemplateImplementation(null, url, null, null);
assertThat(template.fullName()).isEqualTo(url.toExternalForm());
assertThat(template.name()).isEqualTo("hello/footer");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.wisdom.api.Controller;
import org.wisdom.api.asset.Assets;
import org.wisdom.api.http.Result;
import org.wisdom.api.templates.Template;
import org.wisdom.template.thymeleaf.dialect.Routes;
Expand Down Expand Up @@ -56,7 +57,9 @@ public void testJavaScript() {
Controller controller = new FakeController();
router.addController(controller);

context.setVariable(Routes.ROUTES_VAR, new Routes(router, controller));
Assets assets = mock(Assets.class);

context.setVariable(Routes.ROUTES_VAR, new Routes(router, assets, controller));

String processed = engine.process("templates/javascript.thl.html", context);
assertThat(processed).containsIgnoringCase("var t = 'test';");
Expand All @@ -73,12 +76,14 @@ public void testSessionScope() {
final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);
final Assets assets = mock(Assets.class);


Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
return ok(engine.process(template, controller, router, ImmutableMap.<String, Object>of("key", "test")));
return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of("key",
"test")));
}
}).with(new FakeContext().addToSession("key2", "session")).invoke();

Expand All @@ -97,13 +102,14 @@ public void testFlashScope() {
final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);
final Assets assets = mock(Assets.class);


Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
context().flash().put("key2", "ongoing");
return ok(engine.process(template, controller, router, ImmutableMap.<String, Object>of()));
return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of()));
}
}).with(new FakeContext().addToFlash("key", "incoming")).invoke();

Expand All @@ -122,13 +128,14 @@ public void testParameter() {
final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);
final Assets assets = mock(Assets.class);


Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
context().session().put("key2", "ongoing");
return ok(engine.process(template, controller, router, ImmutableMap.<String, Object>of()));
return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of()));
}
}).parameter("key", "param").invoke();

Expand Down

0 comments on commit c0701a4

Please sign in to comment.