Subversion Repositories SmartDukaan

Rev

Rev 35257 | 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 javax.xml.transform.Source;

import com.spice.profitmandi.web.interceptor.PostInterceptor;
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.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
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
        PostInterceptor postInterceptor;

        @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(postInterceptor).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,
                                ProfitMandiConstants.URL_PINELABS_PAY_RESPONSE, ProfitMandiConstants.URL_LOAN_CCAVENUE_PAY_CANCELLED,
                                ProfitMandiConstants.URL_LOAN_CCAVENUE_PAY_RESPONSE,
                                ProfitMandiConstants.URL_POST_OFFICE,
                                ProfitMandiConstants.HOOK_URL_CALLER_ID,
                                ProfitMandiConstants.URL_NEW_LEAD);

                registry.addInterceptor(simpleCORSInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-ui.html","/clickToCall/**",
                                ProfitMandiConstants.URL_PAYU_PAY_RESPONSE, ProfitMandiConstants.URL_PAYU_PAY_CANCELLED,
                                ProfitMandiConstants.URL_CCAVENUE_PAY_RESPONSE, ProfitMandiConstants.URL_CCAVENUE_PAY_CANCELLED,
                                ProfitMandiConstants.URL_PINELABS_PAY_RESPONSE, ProfitMandiConstants.URL_LOAN_CCAVENUE_PAY_CANCELLED,
                                ProfitMandiConstants.URL_LOAN_CCAVENUE_PAY_RESPONSE,
                                ProfitMandiConstants.URL_POST_OFFICE,
                                ProfitMandiConstants.HOOK_URL_CALLER_ID,
                                ProfitMandiConstants.URL_NEW_LEAD,
                                ProfitMandiConstants.URL_SHOPIFY, "/user/token/unregistered");

                registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/user/token/unregistered")
                                .excludePathPatterns(ProfitMandiConstants.URL_ADMIN_TOKEN).excludePathPatterns("/**/swagger*/**")
                                .excludePathPatterns("/v2/**").excludePathPatterns("/document")
                                .excludePathPatterns(ProfitMandiConstants.HOOK_URL_CALLER_ID)
                                .excludePathPatterns(ProfitMandiConstants.URL_SHOPIFY)
                                .excludePathPatterns("/store/generateInvoice")
                                .excludePathPatterns(ProfitMandiConstants.URL_USER_GOOGLE_LOGIN,
                                                ProfitMandiConstants.URL_USER_GOOGLE_LOGIN + "/", ProfitMandiConstants.URL_VERIFY_OTP)
                                .excludePathPatterns("/store/token").excludePathPatterns("/gc/**").excludePathPatterns("/stores/**");

                // 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<?>> messageConverters) {

                messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
                  messageConverters.add(new ByteArrayHttpMessageConverter());
                  messageConverters.add(new ResourceHttpMessageConverter());
                  messageConverters.add(new SourceHttpMessageConverter<Source>());
                  messageConverters.add(new AllEncompassingFormHttpMessageConverter());
                super.configureMessageConverters(messageConverters);
        }

        @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);
        }

}