Posts Tagged spring

Default HTML-escape using Freemarker

Most java developers have at least heard of Freemarker.

FreeMarker is a “template engine”; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It’s a Java package, a class library for Java programmers. It’s not an application for end-users in itself, but something that programmers can embed into their products.

It is the “generic” nature of Freemarker that trips up java web developers. Freemarker by default does not provide any facilities to allow default HTML-escaping of content – a necessity if you want to attempt to prevent Cross-Site Scripting attacks on your web applications. Yes I know that it has the ?html built-in, and that you can wrap blocks of text in <#escape x as x?html> directives, but you have to remember to do that on each page.

What if there was another way?

The class below is a Freemarker TemplateLoader that automatically wraps each loaded template with the HTML-escape directive. Now there is no need to remember to do that in your templates. You can find it being used in my example project on GitHub.

import freemarker.cache.TemplateLoader;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class HtmlTemplateLoader implements TemplateLoader {

    public static final String ESCAPE_PREFIX = "<#ftl strip_whitespace=true><#escape x as x?html>";
    public static final String ESCAPE_SUFFIX = "</#escape>";

    private final TemplateLoader delegate;

    public HtmlTemplateLoader(TemplateLoader delegate) {
        this.delegate = delegate;
    }

    @Override
    public Object findTemplateSource(String name) throws IOException {
        return delegate.findTemplateSource(name);
    }

    @Override
    public long getLastModified(Object templateSource) {
        return delegate.getLastModified(templateSource);
    }

    @Override
    public Reader getReader(Object templateSource, String encoding) throws IOException {
        Reader reader = delegate.getReader(templateSource, encoding);
        try {
            String templateText = IOUtils.toString(reader);
            return new StringReader(ESCAPE_PREFIX + templateText + ESCAPE_SUFFIX);
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }

    @Override
    public void closeTemplateSource(Object templateSource) throws IOException {
        delegate.closeTemplateSource(templateSource);
    }
}

To wire this up using SpringFramework’s Freemarker support you do have to take another step and extend its FreeMarkerConfigurer to register the HtmlTemplateLoader as the one to use for view resolution and rendering. If on the other hand you don’t use Spring then you have one less bit of code to maintain.

import freemarker.cache.TemplateLoader;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.List;

public class HtmlFreeMarkerConfigurer extends FreeMarkerConfigurer {

    @Override
    protected TemplateLoader getAggregateTemplateLoader(List<TemplateLoader> templateLoaders) {
        logger.info("Using HtmlTemplateLoader to enforce HTML-safe content");
        return new HtmlTemplateLoader(super.getAggregateTemplateLoader(templateLoaders));
    }
}

Tags: , , , , , ,

Conversations with Spring

Not that long ago I gave a ThoughtWorks geek night presentation on Post-Redirect-Get and how to mess with the Spring Framework to make it happen. I’ve put up the presentation and the code that I used for it on GitHub. Feedback is always welcome.

Tags: , , ,

StringTemplate views for Spring

StringTemplate is a great templating engine. It’s powerful, simple and quite opinionated. I’ve come really appreciate its simple purpose: render data. No assignment, no arbitrary method invocation. It is not Turing-complete and it would make a lousy rules engine.

SiteMesh is a web-page layout and decoration framework that is my current “golden hammer” when I need to provide a consistent layout across a java web-application. It seems to fit with the way that I think about web pages a whole lot more than something like Tiles – I really prefer decoration over composition as a means of layout control.

I’ve used StringTemplate it in the past on java projects and for a while have wanted to create a way to integrate it with Spring Framework’s MVC so I can stop using Freemarker and Velocity in Spring-heavy projects. Not that Freemarker is that bad, it’s just that I really don’t need or want all of the bells and whistles that come with it and allow it to be so frequently misused. All I really want is a templating engine that renders the model and does not get in my way. It just so happens that SiteMesh also makes an appearance on these projects and I’ve been wanting to use StringTemplate to provide layout decorators as well as Spring views.

Today I’ve released version 1.0 of my spring-stringtemplate integration library as a GitHub project. It provides an implementation of a Spring MVC View and ViewResolver for StringTemplate, and a decorator servlet for SiteMesh.

Feedback is always welcome.

Tags: , , , ,