Subversion Repositories SmartDukaan

Rev

Rev 23947 | Rev 23954 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21165 ashik.ali 1
package com.spice.profitmandi.web.config;
2
 
23949 amit.gupta 3
import java.time.LocalDateTime;
23691 tejbeer 4
import java.time.format.DateTimeFormatter;
23949 amit.gupta 5
import java.time.format.DateTimeFormatterBuilder;
23947 tejbeer 6
import java.util.List;
23691 tejbeer 7
 
21220 ashik.ali 8
import org.springframework.beans.factory.annotation.Autowired;
21272 kshitij.so 9
import org.springframework.context.annotation.Bean;
21165 ashik.ali 10
import org.springframework.context.annotation.ComponentScan;
11
import org.springframework.context.annotation.Configuration;
23947 tejbeer 12
import org.springframework.http.converter.HttpMessageConverter;
13
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
21285 kshitij.so 14
import org.springframework.web.servlet.config.annotation.CorsRegistry;
21165 ashik.ali 15
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
16
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
17
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
18
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
19
 
23947 tejbeer 20
import com.fasterxml.jackson.databind.ObjectMapper;
21
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
22
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
23949 amit.gupta 23
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
24
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
23947 tejbeer 25
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
21236 ashik.ali 26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21743 ashik.ali 27
import com.spice.profitmandi.common.web.interceptor.SimpleCORSInterceptor;
21812 amit.gupta 28
import com.spice.profitmandi.web.interceptor.AuthenticationInterceptor;
21220 ashik.ali 29
 
21272 kshitij.so 30
import springfox.documentation.builders.PathSelectors;
31
import springfox.documentation.builders.RequestHandlerSelectors;
32
import springfox.documentation.service.ApiInfo;
33
import springfox.documentation.spi.DocumentationType;
34
import springfox.documentation.spring.web.plugins.Docket;
35
import springfox.documentation.swagger2.annotations.EnableSwagger2;
36
 
21165 ashik.ali 37
@Configuration
38
@EnableWebMvc
21272 kshitij.so 39
@EnableSwagger2
21220 ashik.ali 40
@ComponentScan("com.spice.profitmandi.*")
21165 ashik.ali 41
public class WebMVCConfig extends WebMvcConfigurerAdapter{
42
 
43
	private static final String RESOURCES_PATTERN="/resources/**";
44
	private static final String RESOURCES_LOCATION="/resources/";
23691 tejbeer 45
	public static final DateTimeFormatter FORMATTER =DateTimeFormatter.ofPattern("dd::MM::yyyy");
21165 ashik.ali 46
 
21220 ashik.ali 47
	@Autowired
21285 kshitij.so 48
	SimpleCORSInterceptor simpleCORSInterceptor;
21452 amit.gupta 49
 
50
	@Autowired
51
	AuthenticationInterceptor authenticationInterceptor;
21302 ashik.ali 52
 
21272 kshitij.so 53
	@Bean
54
	public Docket api() {
55
		return new Docket(DocumentationType.SWAGGER_2).select()
56
				.apis(RequestHandlerSelectors.any())
57
				.paths(PathSelectors.any()).build()
58
				.apiInfo(apiInfo());
59
	}
60
 
21165 ashik.ali 61
	@Override
21272 kshitij.so 62
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
63
	    registry.addResourceHandler("swagger-ui.html")
64
	      .addResourceLocations("classpath:/META-INF/resources/");
65
	    registry.addResourceHandler("/webjars/**")
66
	      .addResourceLocations("classpath:/META-INF/resources/webjars/");
67
	    registry.addResourceHandler(RESOURCES_PATTERN).addResourceLocations(RESOURCES_LOCATION);
68
	}
21165 ashik.ali 69
 
70
	@Override
71
	public void addInterceptors(InterceptorRegistry registry) {
21473 amit.gupta 72
		registry.addInterceptor(simpleCORSInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-ui.html", ProfitMandiConstants.URL_PAYU_PAY_RESPONSE, ProfitMandiConstants.URL_PAYU_PAY_CANCELLED);
21469 amit.gupta 73
		registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**").excludePathPatterns(ProfitMandiConstants.URL_ADMIN_TOKEN)
22812 amit.gupta 74
		.excludePathPatterns("/**/swagger*/**").excludePathPatterns("/v2/**").excludePathPatterns("/document")
21471 amit.gupta 75
		.excludePathPatterns(ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, ProfitMandiConstants.URL_USER_GOOGLE_LOGIN + "/", ProfitMandiConstants.URL_VERIFY_OTP);
21469 amit.gupta 76
		//registry to check api access on basis of UserInfo
21165 ashik.ali 77
	}
21272 kshitij.so 78
 
21285 kshitij.so 79
	@Override
80
	public void addCorsMappings(CorsRegistry registry) {
81
		registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").
82
		allowedHeaders("*").allowCredentials(false);
83
	}
84
 
21272 kshitij.so 85
	private ApiInfo apiInfo() {
86
	    @SuppressWarnings("deprecation")
87
		ApiInfo apiInfo = new ApiInfo(
88
	      "ProfitMandi API",
89
	      "Api's for profitmandi app",
90
	      "API TOS",
91
	      "Terms of service",
92
	      "New Spice Solutions Pvt. Ltd.",
93
	      "License of API",
94
	      "API license URL");
95
	    return apiInfo;
96
	}
23947 tejbeer 97
 
98
	@Override
23949 amit.gupta 99
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
23947 tejbeer 100
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
101
    }
102
 
23949 amit.gupta 103
	@Bean
104
	public ObjectMapper objectMapper() {
105
		DateTimeFormatter f = new DateTimeFormatterBuilder()
106
                .parseCaseInsensitive()
107
                .append(DateTimeFormatter.ISO_LOCAL_DATE)
108
                .appendLiteral('T')
109
                .append(DateTimeFormatter.ISO_LOCAL_TIME)
110
                .toFormatter();
111
		LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(f);
112
		LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(f);
113
		JavaTimeModule jtm = new JavaTimeModule();
114
		jtm.addSerializer(LocalDateTime.class, serializer);
115
		jtm.addDeserializer(LocalDateTime.class, deserializer);
23947 tejbeer 116
		ObjectMapper mapper = new ObjectMapper()
117
				   .registerModule(new ParameterNamesModule())
118
				   .registerModule(new   Jdk8Module())
23949 amit.gupta 119
				   .registerModule(jtm);
23947 tejbeer 120
		return mapper;
121
	}
21220 ashik.ali 122
}