Subversion Repositories SmartDukaan

Rev

Rev 35385 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24472 amit.gupta 1
 
21165 ashik.ali 2
package com.spice.profitmandi.web.config;
3
 
21343 kshitij.so 4
import java.io.IOException;
29637 amit.gupta 5
import java.time.LocalDate;
25300 tejbeer 6
import java.time.LocalDateTime;
21343 kshitij.so 7
import java.util.Properties;
8
 
24472 amit.gupta 9
import org.apache.logging.log4j.LogManager;
23568 govind 10
import org.apache.logging.log4j.Logger;
27028 tejbeer 11
import org.apache.velocity.app.VelocityEngine;
12
import org.apache.velocity.exception.VelocityException;
21165 ashik.ali 13
import org.springframework.context.annotation.Bean;
14
import org.springframework.context.annotation.ComponentScan;
15
import org.springframework.context.annotation.Configuration;
21304 kshitij.so 16
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
21165 ashik.ali 17
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
21304 kshitij.so 18
import org.springframework.core.io.ClassPathResource;
19
import org.springframework.core.io.Resource;
22398 amit.gupta 20
import org.springframework.mail.javamail.JavaMailSender;
21
import org.springframework.mail.javamail.JavaMailSenderImpl;
27028 tejbeer 22
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21165 ashik.ali 23
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
24
 
25300 tejbeer 25
import com.google.gson.Gson;
26
import com.google.gson.GsonBuilder;
29637 amit.gupta 27
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
25300 tejbeer 28
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
29
 
21165 ashik.ali 30
@Configuration
31
@ComponentScan("com.spice.profitmandi.*")
32
public class AppConfig {
21304 kshitij.so 33
 
25300 tejbeer 34
	private static final String PATH_PREFIX = "/WEB-INF/views/";
35
	private static final String PATH_SUFFIX = ".jsp";
36
	private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
37
	private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
21362 kshitij.so 38
	private static Resource resource;
35832 amit 39
	private static Resource sharedResource;
25300 tejbeer 40
 
21362 kshitij.so 41
	public static Resource getResource() {
42
		return resource;
43
	}
44
 
35832 amit 45
	public static Resource getSharedResource() {
46
		return sharedResource;
47
	}
48
 
21362 kshitij.so 49
	public void setResource(Resource resource) {
50
		AppConfig.resource = resource;
51
	}
52
 
27028 tejbeer 53
	/*
54
	 * @Bean(name = "viewResolver") public InternalResourceViewResolver
55
	 * getViewResolver() { LOGGER.debug("creating view resolver bean with prefix : "
56
	 * + PATH_PREFIX + " and suffix : " + PATH_SUFFIX); InternalResourceViewResolver
57
	 * viewResolver = new InternalResourceViewResolver();
58
	 * viewResolver.setPrefix(PATH_PREFIX); viewResolver.setSuffix(PATH_SUFFIX);
59
	 * return viewResolver; }
60
	 */
21304 kshitij.so 61
 
25300 tejbeer 62
	@Bean(name = "messageSource")
21165 ashik.ali 63
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
25300 tejbeer 64
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
65
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21165 ashik.ali 66
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
67
		return messageSource;
68
	}
21304 kshitij.so 69
 
25300 tejbeer 70
	@Bean(name = "multipartResolver")
21165 ashik.ali 71
	public CommonsMultipartResolver getCommonsMultipartResolver() {
72
		LOGGER.info("creating common multipart resolver bean");
24472 amit.gupta 73
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
74
		multipartResolver.setMaxUploadSizePerFile(5000000);
75
		multipartResolver.setMaxUploadSize(5000000);
76
		return multipartResolver;
21304 kshitij.so 77
	}
78
 
79
	@Bean
21643 ashik.ali 80
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
21304 kshitij.so 81
		String activeProfile;
82
 
25300 tejbeer 83
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
84
 
21343 kshitij.so 85
		Properties properties = new Properties();
86
		try {
87
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
88
		} catch (IOException e) {
25300 tejbeer 89
			LOGGER.error("Error in reading env property file.Adding default property" + e);
21343 kshitij.so 90
			properties.put("profile", "dev");
91
		}
92
		activeProfile = (String) properties.get("profile");
21304 kshitij.so 93
 
35385 amit 94
		Resource appResource;
95
		Resource sharedResource;
96
 
21304 kshitij.so 97
		if ("prod".equals(activeProfile)) {
35385 amit 98
			appResource = new ClassPathResource("/META-INF/prod.properties");
99
			sharedResource = new ClassPathResource("/shared-prod.properties");
21304 kshitij.so 100
		} else if ("staging".equals(activeProfile)) {
35385 amit 101
			appResource = new ClassPathResource("/META-INF/staging.properties");
102
			sharedResource = new ClassPathResource("/shared-staging.properties");
21304 kshitij.so 103
		} else {
35385 amit 104
			appResource = new ClassPathResource("/META-INF/dev.properties");
105
			sharedResource = new ClassPathResource("/shared-dev.properties");
21304 kshitij.so 106
		}
107
 
35385 amit 108
		resource = appResource;
35832 amit 109
		AppConfig.sharedResource = sharedResource;
21304 kshitij.so 110
 
35385 amit 111
		// Load shared properties from dao first, then app-specific (app-specific can override)
112
		propertySourcesPlaceholderConfigurer.setLocations(sharedResource, appResource);
113
		propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
114
 
21304 kshitij.so 115
		return propertySourcesPlaceholderConfigurer;
116
	}
25300 tejbeer 117
 
32978 amit.gupta 118
	@Bean
31514 amit.gupta 119
	public JavaMailSender googleMailSender() {
25300 tejbeer 120
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
34635 ranu 121
		mailSender.setHost("smtp.gmail.com");
122
		mailSender.setPort(465);
123
		mailSender.setUsername("sdtech@smartdukaan.com");
124
		mailSender.setPassword("gpdschroalhhirox"); // App Password
125
 
126
		Properties props = mailSender.getJavaMailProperties();
127
		props.put("mail.smtp.auth", "true");
128
		props.put("mail.smtp.ssl.enable", "true");
129
		props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
130
		props.put("mail.smtp.socketFactory.port", "465");
131
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
132
		props.put("mail.debug", "true");
133
 
134
		return mailSender;
135
	}
136
 
137
	/*@Bean
138
	public JavaMailSender googleMailSender() {
139
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
31514 amit.gupta 140
		// Using gmail
141
		mailSender.setHost("smtp.gmail.com");
142
		mailSender.setPort(587);
143
		mailSender.setUsername("build@shop2020.in");
144
		mailSender.setPassword("cafe@nes");
25300 tejbeer 145
 
31514 amit.gupta 146
		Properties javaMailProperties = new Properties();
147
		javaMailProperties.put("mail.smtp.starttls.enable", "true");
148
		javaMailProperties.put("mail.smtp.auth", "true");
149
		javaMailProperties.put("mail.transport.protocol", "smtp");
150
		javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
151
		mailSender.setJavaMailProperties(javaMailProperties);
152
		return mailSender;
34635 ranu 153
	}*/
31514 amit.gupta 154
 
34711 amit.gupta 155
	/*@Bean(name = "mailSender")
31514 amit.gupta 156
	public JavaMailSender getSendgridMailSender() {
34634 ranu 157
		return googleMailSender();
34711 amit.gupta 158
	}*/
34634 ranu 159
 
34711 amit.gupta 160
	@Bean(name = "mailSender")
34634 ranu 161
	public JavaMailSender getSendgridMailSender() {
31514 amit.gupta 162
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
163
 
25300 tejbeer 164
		// Using gmail
165
		mailSender.setHost("smtp.sendgrid.net");
166
		mailSender.setPort(587);
27626 amit.gupta 167
		mailSender.setUsername("apikey");
34711 amit.gupta 168
		mailSender.setPassword("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");
25300 tejbeer 169
 
170
		Properties javaMailProperties = new Properties();
171
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
172
		javaMailProperties.put("mail.smtp.auth", "true");
173
		javaMailProperties.put("mail.transport.protocol", "smtp");
174
		javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
175
 
176
		mailSender.setJavaMailProperties(javaMailProperties);
177
		return mailSender;
34711 amit.gupta 178
	}
25300 tejbeer 179
 
180
	@Bean(name = "gson")
181
	public Gson gson() {
182
 
183
		Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
29637 amit.gupta 184
				.registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
185
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter())
186
				.create();
25300 tejbeer 187
 
188
		return gson;
189
 
190
	}
191
 
27028 tejbeer 192
	@Bean(name = "veloctyEngine")
193
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
194
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
195
		// Properties props = new Properties();
196
		// props.put("resource.loader", "file");
197
 
198
		// props.put("file.resource.loader.description", "Velocity File Resource
199
		// Loader");
200
		// props.put("file.resource.loader.class",
201
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
202
		// props.put("file.resource.loader.cache", true);
203
		// props.put("file.resource.loader.path", ".");
204
 
205
		Properties velocityProperties = new Properties();
206
		velocityProperties.put("resource.loader", "class");
207
		velocityProperties.put("class.resource.loader.class",
208
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
209
		velocityProperties.put("file.resource.loader.cache", true);
210
		velocityProperties.put("file.resource.loader.unicode", true);
211
		velocityProperties.put("input.encoding", "UTF-8");
212
		velocityProperties.put("output.encoding", "UTF-8");
213
		velocityProperties.put("overrideLogging", true);
214
 
215
	//	velocityProperties.put("file.resource.loader.path", ".");
216
 
217
		factory.setVelocityProperties(velocityProperties);
218
 
219
		return factory.createVelocityEngine();
220
 
221
	}
21165 ashik.ali 222
}