2

I would like to load Freemarker templates from one or more URLs so I subclassed the URLTemplate loader class and overrode the getURL(..) method to return the target URL (see below). I then added a couple of instances of this class to a multi template loader and added that to the Freemarker config. This works fine when the first URL returns a template but when it doesn't none of the other template loaders are called. What have I done wrong? I'm using v2.3 of Freemarker via the Restlet framework.

:    :     :    :     :    :     :    :     :    :     
TemplateLoader[] loaders = new TemplateLoader[] {
    new MyTemplateLoader(new URL(request.getRootRef() + app.getRoot())),
    new MyTemplateLoader(new URL(request.getRootRef() + "/"))
};

freemarkerConfig.setTemplateLoader(new MultiTemplateLoader(loaders));  

:    :     :    :     :    :     :    :     :    :     

public class MyTemplateLoader extends URLTemplateLoader {
    private URL root;

    public MyTemplateLoader(URL root) {
        super();
        this.root = root;
    }

    @Override
    protected URL getURL(String template) {
        try {
            URL tu = new URL(root,  "./" + template);
            return tu;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

1 Answer 1

2

A template is considered to be missing if TemplateLoader.findTemplateSource returns null for it. If it returns a non-null object, then MultiTemplateLoader assumes that it has found the template. In the case of URLTemplateLoader, findTemplateSource just returns what getURL does. So you have to check if the target exists, and then return null as URL if it doesn't. This works well for ClassTemplateLoader because getResource returns null URL for missing resources. But in general (if you don't know what kind of URL do you have) you will have to open an URLConnection and then connect() to see if the target exists. Or at least I guess that most URLSrteamHandler-s will check if the target exists at that point.

3
  • Excellent, thanks for taking the time to explain - Tim.
    – Tim
    May 27, 2014 at 8:23
  • Just to follow up on your answer, does that mean that every time a request is made for a template from a URL it first has to open a connection to the server to check the validity of the URL and then again to access the content (assuming it's not in the cache)? Is there any way I can avoid this so that the URL is checked once? TIA
    – Tim
    May 27, 2014 at 15:56
  • When the update delay was expired, it would connect for two or three times: once for checking the URL, once more to get the modification time, and if the template was modified then once more for loading the template content. So it's not for through-network URL-s for sure. To prevent this, the template source returned by findTemplateSource should encapsulate the connected URLConnection (and then be careful with the equals method, which should still compare the URL-s, also don't forget closeTemplateSource). But then it's not an URLTemplateLoader of course.
    – ddekany
    May 29, 2014 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.