Skip to content

Commit

Permalink
Configure the recommended freemarker settings for new projects.
Browse files Browse the repository at this point in the history
The defaults are unsuitable for production use and disclose information about the code.
  • Loading branch information
gtudan committed Oct 20, 2022
1 parent f44df98 commit 61ed9be
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.eclipse.krazo.ext.freemarker;

import freemarker.template.TemplateExceptionHandler;
import org.eclipse.krazo.engine.ViewEngineConfig;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
Expand Down Expand Up @@ -45,12 +46,20 @@ public class DefaultConfigurationProducer {
@ViewEngineConfig
public Configuration getConfiguration() {

Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);

// use the recommended settings from
// https://freemarker.apache.org/docs/pgui_quickstart_createconfiguration.html
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateLoader(new TemplateLoader() {
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
configuration.setLogTemplateExceptions(false);
configuration.setWrapUncheckedExceptions(true);
configuration.setFallbackOnNullLoopVariable(false);

configuration.setTemplateLoader(new TemplateLoader() {

@Override
public Object findTemplateSource(String s) throws IOException {
public Object findTemplateSource(String s) {
return servletContext.getResourceAsStream("/" + s); // Freemarker drops "/"
}

Expand All @@ -60,7 +69,7 @@ public long getLastModified(Object o) {
}

@Override
public Reader getReader(Object o, String s) throws IOException {
public Reader getReader(Object o, String s) {
return new InputStreamReader((InputStream) o);
}

Expand Down

0 comments on commit 61ed9be

Please sign in to comment.