Subversion Repositories SmartDukaan

Rev

Rev 37479 | Go to most recent revision | 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;
37475 amit 16
import org.springframework.context.annotation.Primary;
21304 kshitij.so 17
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
21165 ashik.ali 18
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
21304 kshitij.so 19
import org.springframework.core.io.ClassPathResource;
20
import org.springframework.core.io.Resource;
22398 amit.gupta 21
import org.springframework.mail.javamail.JavaMailSender;
37454 amit 22
import com.spice.profitmandi.common.services.AuthenticatedIdentityMailSender;
22398 amit.gupta 23
import org.springframework.mail.javamail.JavaMailSenderImpl;
27028 tejbeer 24
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21165 ashik.ali 25
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
26
 
25300 tejbeer 27
import com.google.gson.Gson;
28
import com.google.gson.GsonBuilder;
29637 amit.gupta 29
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
25300 tejbeer 30
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
31
 
21165 ashik.ali 32
@Configuration
33
@ComponentScan("com.spice.profitmandi.*")
34
public class AppConfig {
21304 kshitij.so 35
 
25300 tejbeer 36
	private static final String PATH_PREFIX = "/WEB-INF/views/";
37
	private static final String PATH_SUFFIX = ".jsp";
38
	private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
39
	private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
21362 kshitij.so 40
	private static Resource resource;
35832 amit 41
	private static Resource sharedResource;
25300 tejbeer 42
 
21362 kshitij.so 43
	public static Resource getResource() {
44
		return resource;
45
	}
46
 
35832 amit 47
	public static Resource getSharedResource() {
48
		return sharedResource;
49
	}
50
 
21362 kshitij.so 51
	public void setResource(Resource resource) {
52
		AppConfig.resource = resource;
53
	}
54
 
27028 tejbeer 55
	/*
56
	 * @Bean(name = "viewResolver") public InternalResourceViewResolver
57
	 * getViewResolver() { LOGGER.debug("creating view resolver bean with prefix : "
58
	 * + PATH_PREFIX + " and suffix : " + PATH_SUFFIX); InternalResourceViewResolver
59
	 * viewResolver = new InternalResourceViewResolver();
60
	 * viewResolver.setPrefix(PATH_PREFIX); viewResolver.setSuffix(PATH_SUFFIX);
61
	 * return viewResolver; }
62
	 */
21304 kshitij.so 63
 
25300 tejbeer 64
	@Bean(name = "messageSource")
21165 ashik.ali 65
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
25300 tejbeer 66
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
67
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21165 ashik.ali 68
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
69
		return messageSource;
70
	}
21304 kshitij.so 71
 
25300 tejbeer 72
	@Bean(name = "multipartResolver")
21165 ashik.ali 73
	public CommonsMultipartResolver getCommonsMultipartResolver() {
74
		LOGGER.info("creating common multipart resolver bean");
24472 amit.gupta 75
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
76
		multipartResolver.setMaxUploadSizePerFile(5000000);
77
		multipartResolver.setMaxUploadSize(5000000);
78
		return multipartResolver;
21304 kshitij.so 79
	}
80
 
81
	@Bean
21643 ashik.ali 82
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
21304 kshitij.so 83
		String activeProfile;
84
 
25300 tejbeer 85
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
86
 
21343 kshitij.so 87
		Properties properties = new Properties();
88
		try {
89
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
90
		} catch (IOException e) {
25300 tejbeer 91
			LOGGER.error("Error in reading env property file.Adding default property" + e);
21343 kshitij.so 92
			properties.put("profile", "dev");
93
		}
94
		activeProfile = (String) properties.get("profile");
21304 kshitij.so 95
 
35385 amit 96
		Resource appResource;
97
		Resource sharedResource;
98
 
21304 kshitij.so 99
		if ("prod".equals(activeProfile)) {
35385 amit 100
			appResource = new ClassPathResource("/META-INF/prod.properties");
101
			sharedResource = new ClassPathResource("/shared-prod.properties");
21304 kshitij.so 102
		} else if ("staging".equals(activeProfile)) {
35385 amit 103
			appResource = new ClassPathResource("/META-INF/staging.properties");
104
			sharedResource = new ClassPathResource("/shared-staging.properties");
21304 kshitij.so 105
		} else {
35385 amit 106
			appResource = new ClassPathResource("/META-INF/dev.properties");
107
			sharedResource = new ClassPathResource("/shared-dev.properties");
21304 kshitij.so 108
		}
109
 
35385 amit 110
		resource = appResource;
35832 amit 111
		AppConfig.sharedResource = sharedResource;
21304 kshitij.so 112
 
35385 amit 113
		// Load shared properties from dao first, then app-specific (app-specific can override)
114
		propertySourcesPlaceholderConfigurer.setLocations(sharedResource, appResource);
115
		propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
116
 
21304 kshitij.so 117
		return propertySourcesPlaceholderConfigurer;
118
	}
25300 tejbeer 119
 
37475 amit 120
	/**
121
	 * The default sender. Authenticates as sdtech@smartdukaan.com and rewrites every
122
	 * From to that identity — Workspace binds an authenticated session to one address
123
	 * and rejects any other. Also answers to "mailSender" so unqualified
124
	 * JavaMailSender injections resolve here.
125
	 */
37479 amit 126
	@Bean(name = "googleMailSender")
31514 amit.gupta 127
	public JavaMailSender googleMailSender() {
37454 amit 128
		JavaMailSenderImpl mailSender = new AuthenticatedIdentityMailSender();
34635 ranu 129
		mailSender.setHost("smtp.gmail.com");
130
		mailSender.setPort(465);
131
		mailSender.setUsername("sdtech@smartdukaan.com");
37369 amit 132
		mailSender.setPassword("wbdwyqrcgosrkwdn"); // App Password
34635 ranu 133
 
134
		Properties props = mailSender.getJavaMailProperties();
135
		props.put("mail.smtp.auth", "true");
136
		props.put("mail.smtp.ssl.enable", "true");
137
		props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
138
		props.put("mail.smtp.socketFactory.port", "465");
139
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
37367 amit 140
		props.put("mail.smtp.connectiontimeout", "30000");
141
		props.put("mail.smtp.timeout", "120000");
142
		props.put("mail.smtp.writetimeout", "120000");
34635 ranu 143
 
144
		return mailSender;
145
	}
146
 
37479 amit 147
	/**
148
	 * The default sender.
149
	 *
150
	 * Verified working 2026-08-31: MAIL FROM:<noreply@smartdukaan.com> returns 250
151
	 * over IPv4. It does not authenticate -- Workspace authorises it by allowlisted
152
	 * source IP -- so it may legitimately send as noreply@.
153
	 *
37480 amit 154
	 * googleMailSender is the alternative and its credentials are valid -- verified
155
	 * 2026-08-31, AUTH OK on both 465 and 587. Use it by qualifier where a message
156
	 * must come from the authenticated identity rather than noreply@.
37479 amit 157
	 *
37480 amit 158
	 * SMTP on this host works over IPv4 only, and that is not specific to the relay:
159
	 * over IPv6 the relay answers "550 5.7.1 Invalid credentials for relay" and
160
	 * smtp.gmail.com rejects a valid app password with "535 5.7.8 Username and
161
	 * Password not accepted". The JVM prefers IPv4, which is the only reason mail
162
	 * leaves this box at all. Anything that prefers IPv6 will fail on both paths.
37479 amit 163
	 */
164
	@Bean(name = {"gmailRelaySender", "mailSender"})
165
	@Primary
36246 amit 166
	public JavaMailSender getGmailRelaySender() {
167
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
168
		mailSender.setHost("smtp-relay.gmail.com");
169
		mailSender.setPort(587);
170
 
171
		Properties props = mailSender.getJavaMailProperties();
172
		props.put("mail.smtp.starttls.enable", "true");
173
		props.put("mail.smtp.auth", "false");
174
		props.put("mail.smtp.localhost", "smartdukaan.com");
175
		props.put("mail.smtp.ssl.protocols", "TLSv1.2");
176
		props.put("mail.smtp.ssl.trust", "smtp-relay.gmail.com");
177
		props.put("mail.transport.protocol", "smtp");
37367 amit 178
		props.put("mail.smtp.connectiontimeout", "30000");
179
		props.put("mail.smtp.timeout", "120000");
180
		props.put("mail.smtp.writetimeout", "120000");
36246 amit 181
 
182
		return mailSender;
183
	}
184
 
25300 tejbeer 185
	@Bean(name = "gson")
186
	public Gson gson() {
187
 
188
		Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
29637 amit.gupta 189
				.registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
190
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter())
191
				.create();
25300 tejbeer 192
 
193
		return gson;
194
 
195
	}
196
 
27028 tejbeer 197
	@Bean(name = "veloctyEngine")
198
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
199
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
200
		// Properties props = new Properties();
201
		// props.put("resource.loader", "file");
202
 
203
		// props.put("file.resource.loader.description", "Velocity File Resource
204
		// Loader");
205
		// props.put("file.resource.loader.class",
206
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
207
		// props.put("file.resource.loader.cache", true);
208
		// props.put("file.resource.loader.path", ".");
209
 
210
		Properties velocityProperties = new Properties();
211
		velocityProperties.put("resource.loader", "class");
212
		velocityProperties.put("class.resource.loader.class",
213
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
214
		velocityProperties.put("file.resource.loader.cache", true);
215
		velocityProperties.put("file.resource.loader.unicode", true);
216
		velocityProperties.put("input.encoding", "UTF-8");
217
		velocityProperties.put("output.encoding", "UTF-8");
218
		velocityProperties.put("overrideLogging", true);
219
 
220
	//	velocityProperties.put("file.resource.loader.path", ".");
221
 
222
		factory.setVelocityProperties(velocityProperties);
223
 
224
		return factory.createVelocityEngine();
225
 
226
	}
21165 ashik.ali 227
}