Subversion Repositories SmartDukaan

Rev

Rev 26863 | Rev 27113 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.config;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.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.CorsRegistry;
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.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.interceptor.SimpleCORSInterceptor;
import com.spice.profitmandi.web.interceptor.AuthenticationInterceptor;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan("com.spice.profitmandi.*")
public class WebMVCConfig extends WebMvcConfigurerAdapter{
        
        private static final String RESOURCES_PATTERN="/resources/**";
        private static final String RESOURCES_LOCATION="/resources/";
        
        
        private static final Logger log = LogManager.getLogger(WebMVCConfig.class);
        
        @Autowired
        SimpleCORSInterceptor simpleCORSInterceptor;

        @Autowired
        AuthenticationInterceptor authenticationInterceptor;
        
        @Bean
        public Docket api() {
                return new Docket(DocumentationType.SWAGGER_2).select()
                                .apis(RequestHandlerSelectors.any())
                                .paths(PathSelectors.any()).build()
                                .apiInfo(apiInfo());
        }
        
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html")
              .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
              .addResourceLocations("classpath:/META-INF/resources/webjars/");
            registry.addResourceHandler(RESOURCES_PATTERN).addResourceLocations(RESOURCES_LOCATION);
        }
        
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(simpleCORSInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/swagger-ui.html", 
                                        ProfitMandiConstants.URL_PAYU_PAY_RESPONSE, 
                                        ProfitMandiConstants.URL_PAYU_PAY_CANCELLED, 
                                        ProfitMandiConstants.URL_CCAVENUE_PAY_RESPONSE,
                                        ProfitMandiConstants.URL_CCAVENUE_PAY_CANCELLED);
                
                registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**")
                .excludePathPatterns(ProfitMandiConstants.URL_ADMIN_TOKEN)
                .excludePathPatterns("/**/swagger*/**").excludePathPatterns("/v2/**").excludePathPatterns("/document")
                .excludePathPatterns(ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, ProfitMandiConstants.URL_USER_GOOGLE_LOGIN + "/", ProfitMandiConstants.URL_VERIFY_OTP)
                .excludePathPatterns("/store/token")
                .excludePathPatterns("/gc/**");
                
                //registry to check api access on basis of UserInfo
        }
        
        @Override
        public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").
                allowedHeaders("*").allowCredentials(false);
        }
        
        private ApiInfo apiInfo() {
            @SuppressWarnings("deprecation")
                ApiInfo apiInfo = new ApiInfo(
              "ProfitMandi API",
              "Api's for profitmandi app",
              "API TOS",
              "Terms of service",
              "New Spice Solutions Pvt. Ltd.",
              "License of API",
              "API license URL");
            return apiInfo;
        }
        
        
        @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
        super.configureMessageConverters(converters);
    }
        @Bean
        public ObjectMapper objectMapper() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            objectMapper.registerModule(javaTimeModule);
            return objectMapper;
        }
        

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

}