Subversion Repositories SmartDukaan

Rev

Rev 23701 | Rev 23703 | 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.time.format.DateTimeFormatter;
23702 amit.gupta 4
import java.util.List;
23691 tejbeer 5
 
21220 ashik.ali 6
import org.springframework.beans.factory.annotation.Autowired;
21272 kshitij.so 7
import org.springframework.context.annotation.Bean;
21165 ashik.ali 8
import org.springframework.context.annotation.ComponentScan;
9
import org.springframework.context.annotation.Configuration;
23702 amit.gupta 10
import org.springframework.http.converter.HttpMessageConverter;
11
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
21285 kshitij.so 12
import org.springframework.web.servlet.config.annotation.CorsRegistry;
21165 ashik.ali 13
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
14
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
15
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
16
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
17
 
23691 tejbeer 18
import com.fasterxml.jackson.databind.ObjectMapper;
19
import com.fasterxml.jackson.databind.SerializationFeature;
23702 amit.gupta 20
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
21236 ashik.ali 21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21743 ashik.ali 22
import com.spice.profitmandi.common.web.interceptor.SimpleCORSInterceptor;
21812 amit.gupta 23
import com.spice.profitmandi.web.interceptor.AuthenticationInterceptor;
21220 ashik.ali 24
 
21272 kshitij.so 25
import springfox.documentation.builders.PathSelectors;
26
import springfox.documentation.builders.RequestHandlerSelectors;
27
import springfox.documentation.service.ApiInfo;
28
import springfox.documentation.spi.DocumentationType;
29
import springfox.documentation.spring.web.plugins.Docket;
30
import springfox.documentation.swagger2.annotations.EnableSwagger2;
31
 
21165 ashik.ali 32
@Configuration
33
@EnableWebMvc
21272 kshitij.so 34
@EnableSwagger2
21220 ashik.ali 35
@ComponentScan("com.spice.profitmandi.*")
21165 ashik.ali 36
public class WebMVCConfig extends WebMvcConfigurerAdapter{
37
 
38
	private static final String RESOURCES_PATTERN="/resources/**";
39
	private static final String RESOURCES_LOCATION="/resources/";
23691 tejbeer 40
	public static final DateTimeFormatter FORMATTER =DateTimeFormatter.ofPattern("dd::MM::yyyy");
21165 ashik.ali 41
 
21220 ashik.ali 42
	@Autowired
21285 kshitij.so 43
	SimpleCORSInterceptor simpleCORSInterceptor;
21452 amit.gupta 44
 
45
	@Autowired
46
	AuthenticationInterceptor authenticationInterceptor;
21302 ashik.ali 47
 
21272 kshitij.so 48
	@Bean
49
	public Docket api() {
50
		return new Docket(DocumentationType.SWAGGER_2).select()
51
				.apis(RequestHandlerSelectors.any())
52
				.paths(PathSelectors.any()).build()
53
				.apiInfo(apiInfo());
54
	}
55
 
21165 ashik.ali 56
	@Override
21272 kshitij.so 57
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
58
	    registry.addResourceHandler("swagger-ui.html")
59
	      .addResourceLocations("classpath:/META-INF/resources/");
60
	    registry.addResourceHandler("/webjars/**")
61
	      .addResourceLocations("classpath:/META-INF/resources/webjars/");
62
	    registry.addResourceHandler(RESOURCES_PATTERN).addResourceLocations(RESOURCES_LOCATION);
63
	}
21165 ashik.ali 64
 
65
	@Override
66
	public void addInterceptors(InterceptorRegistry registry) {
21473 amit.gupta 67
		registry.addInterceptor(simpleCORSInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-ui.html", ProfitMandiConstants.URL_PAYU_PAY_RESPONSE, ProfitMandiConstants.URL_PAYU_PAY_CANCELLED);
21469 amit.gupta 68
		registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**").excludePathPatterns(ProfitMandiConstants.URL_ADMIN_TOKEN)
22812 amit.gupta 69
		.excludePathPatterns("/**/swagger*/**").excludePathPatterns("/v2/**").excludePathPatterns("/document")
21471 amit.gupta 70
		.excludePathPatterns(ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, ProfitMandiConstants.URL_USER_GOOGLE_LOGIN + "/", ProfitMandiConstants.URL_VERIFY_OTP);
21469 amit.gupta 71
		//registry to check api access on basis of UserInfo
21165 ashik.ali 72
	}
21272 kshitij.so 73
 
21285 kshitij.so 74
	@Override
75
	public void addCorsMappings(CorsRegistry registry) {
76
		registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").
77
		allowedHeaders("*").allowCredentials(false);
78
	}
79
 
21272 kshitij.so 80
	private ApiInfo apiInfo() {
81
	    @SuppressWarnings("deprecation")
82
		ApiInfo apiInfo = new ApiInfo(
83
	      "ProfitMandi API",
84
	      "Api's for profitmandi app",
85
	      "API TOS",
86
	      "Terms of service",
87
	      "New Spice Solutions Pvt. Ltd.",
88
	      "License of API",
89
	      "API license URL");
90
	    return apiInfo;
91
	}
23691 tejbeer 92
 
23702 amit.gupta 93
	@Override
23691 tejbeer 94
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
23702 amit.gupta 95
        MappingJackson2HttpMessageConverter jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
96
        ObjectMapper objectMapper = jacksonMessageConverter.getObjectMapper();
23691 tejbeer 97
 
23702 amit.gupta 98
        objectMapper.registerModule(new JavaTimeModule());
99
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
100
        converters.add(jacksonMessageConverter);
23691 tejbeer 101
    }
102
 
23702 amit.gupta 103
/*	private ObjectMapper objectMapper() {
23691 tejbeer 104
	    ObjectMapper objectMapper = new ObjectMapper();
105
	    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
106
	    JavaTimeModule javaTimeModule = new JavaTimeModule();
107
	    javaTimeModule.addSerializer(java.sql.Date.class, new DateSerializer());
23700 amit.gupta 108
	    //javaTimeModule.addDeserializer(Timestamp.class, new LocalDateDeserializer());
23691 tejbeer 109
	    objectMapper.registerModule(javaTimeModule);
110
	    return objectMapper;
111
	}
112
 
113
	public class LocalDateSerializer extends JsonSerializer<Timestamp> {
114
 
115
	    @Override
116
	    public void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
117
 
118
	    	 ZonedDateTime z = value.toInstant().atZone(ZoneId.systemDefault());
119
	         String str = FORMATTER.format(z);
120
	         gen.writeString(str); 	
121
	    }	
122
	}
23702 amit.gupta 123
*/	
23700 amit.gupta 124
/*	public class LocalDateDeserializer extends JsonDeserializer<Timestamp> {
23691 tejbeer 125
 
126
	    @Override
127
	    public Timestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
128
	    	LocalDateTime dt = LocalDateTime.parse(p.getText(), FORMATTER);
129
	        // the date/time is in the default timezone
130
	        return Timestamp.from(dt.atZone(ZoneId.systemDefault()).toInstant());
131
	    }
23700 amit.gupta 132
	}*/
23691 tejbeer 133
 
134
 
21220 ashik.ali 135
}