Subversion Repositories SmartDukaan

Rev

Rev 23700 | Rev 23702 | 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
 
23691 tejbeer 3
import java.io.IOException;
4
import java.sql.Timestamp;
5
import java.time.ZoneId;
6
import java.time.ZonedDateTime;
7
import java.time.format.DateTimeFormatter;
8
 
21220 ashik.ali 9
import org.springframework.beans.factory.annotation.Autowired;
21272 kshitij.so 10
import org.springframework.context.annotation.Bean;
21165 ashik.ali 11
import org.springframework.context.annotation.ComponentScan;
12
import org.springframework.context.annotation.Configuration;
23701 amit.gupta 13
import org.springframework.context.annotation.Primary;
14
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
21285 kshitij.so 15
import org.springframework.web.servlet.config.annotation.CorsRegistry;
21165 ashik.ali 16
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
17
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
18
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
19
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
20
 
23691 tejbeer 21
import com.fasterxml.jackson.core.JsonGenerator;
22
import com.fasterxml.jackson.databind.JsonSerializer;
23
import com.fasterxml.jackson.databind.ObjectMapper;
24
import com.fasterxml.jackson.databind.SerializationFeature;
25
import com.fasterxml.jackson.databind.SerializerProvider;
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
	}
23691 tejbeer 97
 
23701 amit.gupta 98
/*	@Override
23691 tejbeer 99
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
100
 
101
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
102
        super.configureMessageConverters(converters);
103
    }
104
 
105
	private ObjectMapper objectMapper() {
106
	    ObjectMapper objectMapper = new ObjectMapper();
107
	    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
108
	    JavaTimeModule javaTimeModule = new JavaTimeModule();
109
	    javaTimeModule.addSerializer(java.sql.Date.class, new DateSerializer());
23700 amit.gupta 110
	    //javaTimeModule.addDeserializer(Timestamp.class, new LocalDateDeserializer());
23691 tejbeer 111
	    objectMapper.registerModule(javaTimeModule);
112
	    return objectMapper;
23701 amit.gupta 113
	}*/
114
 
115
	@Bean
116
	@Primary
117
	public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
118
	    ObjectMapper objectMapper = builder.build();
119
	    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
120
	    return objectMapper;
23691 tejbeer 121
	}
122
 
123
	public class LocalDateSerializer extends JsonSerializer<Timestamp> {
124
 
125
	    @Override
126
	    public void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
127
 
128
	    	 ZonedDateTime z = value.toInstant().atZone(ZoneId.systemDefault());
129
	         String str = FORMATTER.format(z);
130
	         gen.writeString(str); 	
131
	    }	
132
	}
133
 
23700 amit.gupta 134
/*	public class LocalDateDeserializer extends JsonDeserializer<Timestamp> {
23691 tejbeer 135
 
136
	    @Override
137
	    public Timestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
138
	    	LocalDateTime dt = LocalDateTime.parse(p.getText(), FORMATTER);
139
	        // the date/time is in the default timezone
140
	        return Timestamp.from(dt.atZone(ZoneId.systemDefault()).toInstant());
141
	    }
23700 amit.gupta 142
	}*/
23691 tejbeer 143
 
144
 
21220 ashik.ali 145
}