下面列出了freemarker.template.Version#freemarker.core.TemplateClassResolver 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Configuring freemarker template file path.
*
* @return new FreeMarkerConfigurer
*/
@Bean
public FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties) throws IOException, TemplateException {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPaths(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/", "classpath:/templates/");
configurer.setDefaultEncoding("UTF-8");
Properties properties = new Properties();
properties.setProperty("auto_import", "/common/macro/common_macro.ftl as common,/common/macro/global_macro.ftl as global");
configurer.setFreemarkerSettings(properties);
// Predefine configuration
freemarker.template.Configuration configuration = configurer.createConfiguration();
configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
if (haloProperties.isProductionEnv()) {
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
}
// Set predefined freemarker configuration
configurer.setConfiguration(configuration);
return configurer;
}
@Override
protected Configuration getFreemarkerConfiguration(RepoCtx ctx)
{
if (useRemoteCallbacks)
{
// as per 3.0, 3.1
return super.getFreemarkerConfiguration(ctx);
}
else
{
Configuration cfg = new Configuration();
cfg.setObjectWrapper(new DefaultObjectWrapper());
cfg.setTemplateLoader(new ClassPathRepoTemplateLoader(nodeService, contentService, defaultEncoding));
// TODO review i18n
cfg.setLocalizedLookup(false);
cfg.setIncompatibleImprovements(new Version(2, 3, 20));
cfg.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
return cfg;
}
}
@Bean
public freemarker.template.Configuration getConfiguration() {
final freemarker.template.Configuration configuration =
new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22);
configuration.setLocalizedLookup(false);
configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
TemplateConfiguration tcHTML = new TemplateConfiguration();
tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
configuration.setTemplateConfigurations(
new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher(HTML_TEMPLATE_EXTENSION), tcHTML));
try {
TemplateLoader[] templateLoaders = { overrideTemplateLoader(), new FileTemplateLoader(new File(templatesPath)) };
configuration.setTemplateLoader(new MultiTemplateLoader(templateLoaders));
} catch (final IOException e) {
LOGGER.warn("Error occurred while trying to read email templates", e);
}
return configuration;
}
@Bean
public freemarker.template.Configuration getConfiguration() {
final freemarker.template.Configuration configuration =
new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22);
TemplateConfiguration tcHTML = new TemplateConfiguration();
tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
configuration.setTemplateConfigurations(
new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher(HTML_TEMPLATE_EXTENSION), tcHTML));
try {
configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
configuration.setTemplateLoader(new FileTemplateLoader(new File(templatesPath)));
} catch (final IOException e) {
LOGGER.warn("Error occurred while trying to read email templates directory", e);
}
return configuration;
}
@Bean
public freemarker.template.Configuration getConfiguration() {
final freemarker.template.Configuration configuration =
new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22);
TemplateConfiguration tcHTML = new TemplateConfiguration();
tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
configuration.setTemplateConfigurations(
new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher(HTML_TEMPLATE_EXTENSION), tcHTML));
try {
configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
configuration.setTemplateLoader(new FileTemplateLoader(new File(templatesPath)));
} catch (final IOException e) {
LOGGER.warn("Error occurred while trying to read email templates directory", e);
}
return configuration;
}
protected Configuration getFreemarkerConfiguration(RepoCtx ctx)
{
Configuration cfg = new Configuration();
cfg.setObjectWrapper(new DefaultObjectWrapper());
// custom template loader
cfg.setTemplateLoader(new TemplateWebScriptLoader(ctx.getRepoEndPoint(), ctx.getTicket()));
// TODO review i18n
cfg.setLocalizedLookup(false);
cfg.setIncompatibleImprovements(new Version(2, 3, 20));
cfg.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
return cfg;
}
/**
* FreeMarker configuration for loading the specified template directly from a String
*
* @param path Pseudo Path to the template
* @param template Template content
*
* @return FreeMarker configuration
*/
protected Configuration getStringConfig(String path, String template)
{
Configuration config = new Configuration();
// setup template cache
config.setCacheStorage(new MruCacheStorage(2, 0));
// use our custom loader to load a template directly from a String
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate(path, template);
config.setTemplateLoader(stringTemplateLoader);
// use our custom object wrapper that can deal with QNameMap objects directly
config.setObjectWrapper(qnameObjectWrapper);
// rethrow any exception so we can deal with them
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
// set default template encoding
if (defaultEncoding != null)
{
config.setDefaultEncoding(defaultEncoding);
}
config.setIncompatibleImprovements(new Version(2, 3, 20));
config.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
return config;
}
private FreeMarkerService(Builder bulder) {
maxOutputLength = bulder.getMaxOutputLength();
maxThreads = bulder.getMaxThreads();
maxQueueLength = bulder.getMaxQueueLength();
maxTemplateExecutionTime = bulder.getMaxTemplateExecutionTime();
int actualMaxQueueLength = maxQueueLength != null
? maxQueueLength
: Math.max(
MIN_DEFAULT_MAX_QUEUE_LENGTH,
(int) (MAX_DEFAULT_MAX_QUEUE_LENGTH_MILLISECONDS / maxTemplateExecutionTime));
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
maxThreads, maxThreads,
THREAD_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
new BlockingArrayQueue<Runnable>(actualMaxQueueLength));
threadPoolExecutor.allowCoreThreadTimeOut(true);
templateExecutor = threadPoolExecutor;
// Avoid ERROR log for using the actual current version. This application is special in that regard.
Version latestVersion = new Version(Configuration.getVersion().toString());
freeMarkerConfig = new Configuration(latestVersion);
freeMarkerConfig.setNewBuiltinClassResolver(TemplateClassResolver.ALLOWS_NOTHING_RESOLVER);
freeMarkerConfig.setObjectWrapper(new SimpleObjectWrapperWithXmlSupport(latestVersion));
freeMarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
freeMarkerConfig.setLogTemplateExceptions(false);
freeMarkerConfig.setAttemptExceptionReporter(new AttemptExceptionReporter() {
@Override
public void report(TemplateException te, Environment env) {
// Suppress it
}
});
freeMarkerConfig.setLocale(AllowedSettingValues.DEFAULT_LOCALE);
freeMarkerConfig.setTimeZone(AllowedSettingValues.DEFAULT_TIME_ZONE);
freeMarkerConfig.setOutputFormat(AllowedSettingValues.DEFAULT_OUTPUT_FORMAT);
freeMarkerConfig.setOutputEncoding("UTF-8");
}
@Bean
public freemarker.template.Configuration getConfiguration() {
final freemarker.template.Configuration configuration =
new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22);
configuration.setLocalizedLookup(false);
configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
try {
TemplateLoader[] templateLoaders = { overrideTemplateLoader(), new FileTemplateLoader(new File(templatesPath)) };
configuration.setTemplateLoader(new MultiTemplateLoader(templateLoaders));
} catch (final IOException e) {
LOGGER.warn("Error occurred while trying to read email templates", e);
}
return configuration;
}