001package io.jstach.opt.spring.boot.webmvc;
002
003import java.util.List;
004
005import org.springframework.beans.factory.annotation.Autowired;
006import org.springframework.boot.autoconfigure.AutoConfiguration;
007import org.springframework.boot.autoconfigure.AutoConfigureAfter;
008import org.springframework.context.ApplicationContext;
009import org.springframework.context.annotation.Bean;
010import org.springframework.http.converter.HttpMessageConverter;
011import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
012
013import io.jstach.opt.spring.web.JStachioHttpMessageConverter;
014import io.jstach.opt.spring.webmvc.JStachioModelViewConfigurer;
015import io.jstach.opt.spring.webmvc.ViewSetupHandlerInterceptor;
016
017/**
018 * MVC AutoConfiguration for JStachio runtime.
019 *
020 * @see JStachioHttpMessageConverter
021 * @author agentgt
022 * @author dsyer
023 */
024@AutoConfiguration
025@AutoConfigureAfter(value = { JStachioAutoConfiguration.class })
026public class JStachioWebMvcAutoConfiguration implements WebMvcConfigurer {
027
028        private final JStachioHttpMessageConverter messageConverter;
029
030        /**
031         * Configures based on the jstachio found by spring
032         * @param messageConverter jstachio powered message converter
033         */
034        @Autowired
035        public JStachioWebMvcAutoConfiguration(JStachioHttpMessageConverter messageConverter) {
036                this.messageConverter = messageConverter;
037        }
038
039        @Override
040        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
041                converters.add(0, this.messageConverter);
042        }
043
044        /**
045         * Configures an interceptor for before rendering logic useful for adding additional
046         * data to JStache models.
047         * @param context searched for {@link JStachioModelViewConfigurer}s.
048         * @return interceptor that will automatically be added to the web context.
049         */
050        @Bean
051        @SuppressWarnings("exports")
052        public ViewSetupHandlerInterceptor viewSetupHandlerInterceptor(ApplicationContext context) {
053                return new ViewSetupHandlerInterceptor(context);
054        }
055
056}