Subversion Repositories SmartDukaan

Rev

Rev 21749 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.config;

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.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.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 WebConfig extends WebMvcConfigurerAdapter{
        
        private static final String RESOURCES_PATTERN="/resources/**";
        private static final String RESOURCES_LOCATION="/resources/";
        
        @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("/**");
                registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**").excludePathPatterns("/login")
                .excludePathPatterns("/**/swagger*/**").excludePathPatterns("/v2/**");
        }
        
        @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;
        }
}