Subversion Repositories SmartDukaan

Rev

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