Subversion Repositories SmartDukaan

Rev

Rev 26558 | Rev 30039 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.config;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.spice.profitmandi.web.interceptor.AuthenticationInterceptor;
import com.spice.profitmandi.web.interceptor.RoleInterceptor;

@EnableWebMvc
@Configuration
@ComponentScan({ "com.spice.profitmandi.*" })
public class WebConfig extends WebMvcConfigurerAdapter {

        private static final String RESOURCES_PATTERN = "/resources/**";
        private static final String RESOURCES_LOCATION = "/resources/";

        @Autowired
        AuthenticationInterceptor authenticationInterceptor;

        @Autowired
        RoleInterceptor roleInterceptor;

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler(RESOURCES_PATTERN).addResourceLocations(RESOURCES_LOCATION);
        }

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
                // registry.addInterceptor()
                registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**").excludePathPatterns("/hdfctest/**",
                                "/hdfc/**", "/spicemoney/callback", "/login", "/login/", "/forgetPassword", "/forgetPassword/", "/",
                                "/checkplans", "/12dashboard34");
                registry.addInterceptor(roleInterceptor).excludePathPatterns("/hdfctest/**", "/hdfc/**", "/spicemoney/callback",
                                "/login", "/login/", "/register", "/register/", "/forgetPassword", "/forgetPassword/", "/", "",
                                "/12dashboard34");
        }

        @Override
        public void addFormatters(FormatterRegistry registry) {
                DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
                registrar.setUseIsoFormat(true);
                registrar.registerFormatters(registry);
        }

        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
        }

        @Bean
        public ObjectMapper objectMapper() {
                DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive()
                                .append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd()
                                .appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
                DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive()
                                .append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME)
                                .toFormatter();
                LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
                LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
                JavaTimeModule jtm = new JavaTimeModule();
                jtm.addSerializer(LocalDateTime.class, serializer);
                jtm.addDeserializer(LocalDateTime.class, deserializer);
                ObjectMapper mapper = new ObjectMapper().registerModule(new ParameterNamesModule())
                                .registerModule(new Jdk8Module()).registerModule(jtm); // new module, NOT JSR310Module
                return mapper;
        }

        @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
        }

}