Search in sources :

Example 1 with IPostProcessor

use of org.thymeleaf.postprocessor.IPostProcessor in project cas by apereo.

the class CasThemesConfiguration method registeredServiceViewResolver.

@Bean
public ViewResolver registeredServiceViewResolver() {
    final RegisteredServiceThemeBasedViewResolver r = new RegisteredServiceThemeBasedViewResolver(servicesManager, argumentExtractors, thymeleafProperties.getPrefix(), thymeleafProperties.getSuffix());
    r.setApplicationContext(this.thymeleafViewResolver.getApplicationContext());
    r.setCache(this.thymeleafProperties.isCache());
    if (!r.isCache()) {
        r.setCacheLimit(0);
    }
    r.setCacheUnresolved(this.thymeleafViewResolver.isCacheUnresolved());
    r.setCharacterEncoding(this.thymeleafViewResolver.getCharacterEncoding());
    r.setContentType(this.thymeleafViewResolver.getContentType());
    r.setExcludedViewNames(this.thymeleafViewResolver.getExcludedViewNames());
    r.setOrder(this.thymeleafViewResolver.getOrder());
    r.setRedirectContextRelative(this.thymeleafViewResolver.isRedirectContextRelative());
    r.setRedirectHttp10Compatible(this.thymeleafViewResolver.isRedirectHttp10Compatible());
    r.setStaticVariables(this.thymeleafViewResolver.getStaticVariables());
    final SpringTemplateEngine engine = SpringTemplateEngine.class.cast(this.thymeleafViewResolver.getTemplateEngine());
    engine.addDialect(new IPostProcessorDialect() {

        @Override
        public int getDialectPostProcessorPrecedence() {
            return Integer.MAX_VALUE;
        }

        @Override
        public Set<IPostProcessor> getPostProcessors() {
            return Collections.singleton(new PostProcessor(TemplateMode.parse(thymeleafProperties.getMode()), CasThymeleafOutputTemplateHandler.class, Integer.MAX_VALUE));
        }

        @Override
        public String getName() {
            return CasThymeleafOutputTemplateHandler.class.getSimpleName();
        }
    });
    r.setTemplateEngine(engine);
    r.setViewNames(this.thymeleafViewResolver.getViewNames());
    return r;
}
Also used : SpringTemplateEngine(org.thymeleaf.spring4.SpringTemplateEngine) Set(java.util.Set) RegisteredServiceThemeBasedViewResolver(org.apereo.cas.services.web.RegisteredServiceThemeBasedViewResolver) IPostProcessorDialect(org.thymeleaf.dialect.IPostProcessorDialect) PostProcessor(org.thymeleaf.postprocessor.PostProcessor) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with IPostProcessor

use of org.thymeleaf.postprocessor.IPostProcessor in project sling by apache.

the class TemplateManager method createTemplateProcessingHandlerChain.

private static ITemplateHandler createTemplateProcessingHandlerChain(final IEngineContext context, final boolean setPreProcessors, final boolean setPostProcessors, final ITemplateHandler handler, final Writer writer) {
    final IEngineConfiguration configuration = context.getConfiguration();
    /*
         * Declare the pair of pointers that will allow us to build the chain of template handlers
         */
    ITemplateHandler firstHandler = null;
    ITemplateHandler lastHandler = null;
    /*
         * First type of handlers to be added: pre-processors (if any)
         */
    if (setPreProcessors) {
        final Set<IPreProcessor> preProcessors = configuration.getPreProcessors(context.getTemplateMode());
        if (preProcessors != null && preProcessors.size() > 0) {
            for (final IPreProcessor preProcessor : preProcessors) {
                final Class<? extends ITemplateHandler> preProcessorClass = preProcessor.getHandlerClass();
                final ITemplateHandler preProcessorHandler;
                try {
                    preProcessorHandler = preProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of pre-processor " + preProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                preProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = preProcessorHandler;
                    lastHandler = preProcessorHandler;
                } else {
                    lastHandler.setNext(preProcessorHandler);
                    lastHandler = preProcessorHandler;
                }
            }
        }
    }
    /*
         * Initialize and add to the chain te Processor Handler itself, the central piece of the chain
         */
    handler.setContext(context);
    if (firstHandler == null) {
        firstHandler = handler;
        lastHandler = handler;
    } else {
        lastHandler.setNext(handler);
        lastHandler = handler;
    }
    /*
         * After the Processor Handler, we now must add the post-processors (if any)
         */
    if (setPostProcessors) {
        final Set<IPostProcessor> postProcessors = configuration.getPostProcessors(context.getTemplateMode());
        if (postProcessors != null && postProcessors.size() > 0) {
            for (final IPostProcessor postProcessor : postProcessors) {
                final Class<? extends ITemplateHandler> postProcessorClass = postProcessor.getHandlerClass();
                final ITemplateHandler postProcessorHandler;
                try {
                    postProcessorHandler = postProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of post-processor " + postProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                postProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = postProcessorHandler;
                    lastHandler = postProcessorHandler;
                } else {
                    lastHandler.setNext(postProcessorHandler);
                    lastHandler = postProcessorHandler;
                }
            }
        }
    }
    /*
         * Last step: the OUTPUT HANDLER
         */
    if (writer != null) {
        final OutputTemplateHandler outputHandler = new OutputTemplateHandler(writer);
        outputHandler.setContext(context);
        if (firstHandler == null) {
            firstHandler = outputHandler;
        } else {
            lastHandler.setNext(outputHandler);
        }
    }
    return firstHandler;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Aggregations

IPostProcessor (org.thymeleaf.postprocessor.IPostProcessor)2 Set (java.util.Set)1 RegisteredServiceThemeBasedViewResolver (org.apereo.cas.services.web.RegisteredServiceThemeBasedViewResolver)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)1 IPostProcessorDialect (org.thymeleaf.dialect.IPostProcessorDialect)1 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)1 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)1 PostProcessor (org.thymeleaf.postprocessor.PostProcessor)1 IPreProcessor (org.thymeleaf.preprocessor.IPreProcessor)1 SpringTemplateEngine (org.thymeleaf.spring4.SpringTemplateEngine)1