001package io.jstach.opt.spring.boot.webmvc;
002
003import java.util.List;
004
005import org.springframework.beans.BeansException;
006import org.springframework.beans.factory.annotation.Autowired;
007import org.springframework.boot.autoconfigure.AutoConfiguration;
008import org.springframework.boot.autoconfigure.AutoConfigureAfter;
009import org.springframework.context.ApplicationContext;
010import org.springframework.context.ApplicationContextAware;
011import org.springframework.http.converter.HttpMessageConverter;
012import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
013import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
014
015import io.jstach.jstachio.JStachio;
016import io.jstach.opt.spring.web.JStachioHttpMessageConverter;
017import io.jstach.opt.spring.webmvc.ViewResolvingHandlerInterceptor;
018import io.jstach.opt.spring.webmvc.ViewSetupHandlerInterceptor;
019
020/**
021 * MVC AutoConfiguration for JStachio runtime.
022 *
023 * @see JStachioHttpMessageConverter
024 * @author agentgt
025 * @author dsyer
026 */
027@AutoConfiguration
028@AutoConfigureAfter(value = { JStachioAutoConfiguration.class })
029public class JStachioWebMvcAutoConfiguration implements WebMvcConfigurer, ApplicationContextAware {
030
031        private final JStachioHttpMessageConverter messageConverter;
032
033        private final JStachio jstachio;
034
035        private ApplicationContext context;
036
037        /**
038         * Configures based on the jstachio found by spring
039         * @param messageConverter jstachio powered message converter
040         * @param jstachio jstachio instance created by Spring
041         */
042        @Autowired
043        public JStachioWebMvcAutoConfiguration(JStachioHttpMessageConverter messageConverter, JStachio jstachio) {
044                this.messageConverter = messageConverter;
045                this.jstachio = jstachio;
046        }
047
048        @Override
049        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
050                converters.add(0, this.messageConverter);
051        }
052
053        @Override
054        @SuppressWarnings("exports")
055        public void addInterceptors(InterceptorRegistry registry) {
056                registry.addInterceptor(new ViewSetupHandlerInterceptor(context));
057                registry.addInterceptor(new ViewResolvingHandlerInterceptor(jstachio));
058        }
059
060        @Override
061        @SuppressWarnings("exports")
062        public void setApplicationContext(ApplicationContext context) throws BeansException {
063                this.context = context;
064        }
065
066}