Alexandr
Alexandr

Reputation: 9505

FreeMarker template loader returns 302 Found

FreeMarker returns the following content of the template:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://someOtherUrl">here</a>.</p>
</body></html>

It can not happen at all. How does FreeMaker gets it? FreeMarker configuration is created and used:

    Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
    ...
    @PostConstruct
    void init()
    {
        configuration.setDefaultEncoding(StandardCharsets.UTF_8.toString());
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        configuration.setTemplateLoader(new TemplateLoader());
        configuration.setLocale(Locale.ENGLISH);
    }
...
   Template temp = configuration.getTemplate(sourceTemplateUrl);

Can I get rid of such "smart" behaviour?

EDIT:

public class TemplateLoader extends URLTemplateLoader
{
    private static final String LOCAL_PREFIX = "_" + Locale.ENGLISH;

    @Override
    protected URL getURL(final String url)
    {
        try
        {
            //get rid of "_en" in the url, FreeMarker set it based on Locale
            return new URL(url.replace(LOCAL_PREFIX, ""));
        }
        catch (MalformedURLException e)
        {
            throw new RuntimeException(e);
        }
    }
}

Upvotes: 0

Views: 231

Answers (1)

ddekany
ddekany

Reputation: 31112

That page isn't generated by FreeMarker. FreeMarker does no HTTP magic, it just generates text. (Of course, if you cause FreeMarker to load the template from an URL that returns this text, then it technically comes from FreeMarker.)

Also, your TemplateLoader doesn't look functional to me, as you just create URL-s from relative paths (update: you are passing in full URL-s, so it can work, but you will have to handle HTTP response codes too). But you aren't supposed to do such name changes there anyway. If you don't want localized lookup, just turn it off:

configuration.setLocalizedLookup(false);

Or if you need something trickier, check out configuration.setTemplateLookupStrategy.

Upvotes: 1

Related Questions