freemarker.core.TemplateClassResolver Java Examples

The following examples show how to use freemarker.core.TemplateClassResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebMvcAutoConfiguration.java    From halo with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: LocalFeedTaskProcessor.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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;
    }
}
 
Example #3
Source File: EmailConfiguration.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #4
Source File: EmailConfiguration.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #5
Source File: EmailConfiguration.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #6
Source File: FeedTaskProcessor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #7
Source File: FreeMarkerProcessor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: FreeMarkerService.java    From freemarker-online-tester with Apache License 2.0 5 votes vote down vote up
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");
 }
 
Example #9
Source File: FreeMarkerConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@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;
}