001package io.jstach.opt.spring.webflux.example;
002
003import org.springframework.beans.factory.annotation.Autowired;
004import org.springframework.context.ApplicationContext;
005import org.springframework.context.annotation.Bean;
006import org.springframework.context.annotation.Configuration;
007import org.springframework.http.codec.ServerCodecConfigurer;
008import org.springframework.web.reactive.config.WebFluxConfigurer;
009
010import io.jstach.jstachio.JStachio;
011import io.jstach.opt.spring.webflux.JStachioEncoder;
012import io.jstach.opt.spring.webflux.JStachioModelViewConfigurer;
013import io.jstach.opt.spring.webflux.ViewSetupBeanPostProcessor;
014
015/**
016 * Configures WebFlux using {@link JStachioEncoder} to allow returning models which will
017 * be rendered using JStachio runtime.
018 *
019 * @author agentgt
020 * @author dsyer
021 * @see JStachioEncoder
022 */
023@SuppressWarnings("exports")
024@Configuration
025public class WebConfig implements WebFluxConfigurer {
026
027        private final JStachio jstachio;
028
029        /**
030         * Configures based on the jstachio found by spring
031         * @param jstachio the found jstachio
032         */
033        @Autowired
034        public WebConfig(JStachio jstachio) {
035                this.jstachio = jstachio;
036        }
037
038        @Override
039        public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
040                /*
041                 * TODO consider using HttpMessageWriter on minor release
042                 */
043                configurer.customCodecs().register(new JStachioEncoder(jstachio));
044        }
045
046        /**
047         * Factory to create a bean post processor to register
048         * {@link JStachioModelViewConfigurer}s.
049         * @param context supplied by spring
050         * @return a post processor that will configure JStachio Model Views before being
051         * rendered.
052         */
053        @Bean
054        public ViewSetupBeanPostProcessor viewSetupHandlerInterceptor(ApplicationContext context) {
055                return new ViewSetupBeanPostProcessor(context);
056        }
057
058}