Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21561 ashik.ali 1
package com.spice.profitmandi.web.config;
21555 kshitij.so 2
 
32569 amit.gupta 3
import com.fasterxml.jackson.core.JsonParser;
37161 ranu 4
import com.fasterxml.jackson.databind.*;
37072 amit 5
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
32569 amit.gupta 6
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
7
import com.fasterxml.jackson.databind.module.SimpleModule;
31282 amit.gupta 8
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
9
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
33618 tejus.loha 10
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
31282 amit.gupta 11
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
33618 tejus.loha 12
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
31282 amit.gupta 13
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
14
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
29993 amit.gupta 15
import com.google.gson.Gson;
16
import com.google.gson.GsonBuilder;
17
import com.spice.profitmandi.common.util.Utils;
18
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
23869 amit.gupta 19
import org.apache.logging.log4j.LogManager;
23568 govind 20
import org.apache.logging.log4j.Logger;
28339 tejbeer 21
import org.apache.velocity.app.VelocityEngine;
22
import org.apache.velocity.exception.VelocityException;
21555 kshitij.so 23
import org.springframework.context.annotation.Bean;
24
import org.springframework.context.annotation.ComponentScan;
25
import org.springframework.context.annotation.Configuration;
26
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
27
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
28
import org.springframework.core.io.ClassPathResource;
29
import org.springframework.core.io.Resource;
23985 tejbeer 30
import org.springframework.mail.javamail.JavaMailSender;
31
import org.springframework.mail.javamail.JavaMailSenderImpl;
28339 tejbeer 32
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21555 kshitij.so 33
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
21625 kshitij.so 34
import org.springframework.web.servlet.ViewResolver;
24507 amit.gupta 35
import org.springframework.web.servlet.view.BeanNameViewResolver;
21625 kshitij.so 36
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
31513 amit.gupta 37
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
21555 kshitij.so 38
 
29993 amit.gupta 39
import java.io.IOException;
40
import java.math.RoundingMode;
41
import java.text.DecimalFormat;
42
import java.text.NumberFormat;
33618 tejus.loha 43
import java.time.LocalDate;
29993 amit.gupta 44
import java.time.LocalDateTime;
45
import java.time.format.DateTimeFormatter;
31282 amit.gupta 46
import java.time.format.DateTimeFormatterBuilder;
29993 amit.gupta 47
import java.util.HashMap;
48
import java.util.Locale;
49
import java.util.Map;
50
import java.util.Properties;
25300 tejbeer 51
 
21625 kshitij.so 52
@SuppressWarnings("deprecation")
21555 kshitij.so 53
@Configuration
54
@ComponentScan("com.spice.profitmandi.*")
31511 amit.gupta 55
//@Import(RepositoryRestMvcConfiguration.class)
21555 kshitij.so 56
public class AppConfig {
57
 
32054 tejbeer 58
    private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
59
    private static final String VELOCITY_PATH_SUFFIX = ".vm";
32773 raveendra. 60
    private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:messages";
32054 tejbeer 61
    private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
62
    private static Resource resource;
35832 amit 63
    private static Resource sharedResource;
24639 tejbeer 64
 
32054 tejbeer 65
    public static Resource getResource() {
66
        return resource;
67
    }
21555 kshitij.so 68
 
35832 amit 69
    public static Resource getSharedResource() {
70
        return sharedResource;
71
    }
72
 
32054 tejbeer 73
    public void setResource(Resource resource) {
74
        AppConfig.resource = resource;
75
    }
21625 kshitij.so 76
 
32054 tejbeer 77
    @Bean(value = "velocityAttributesMap")
78
    public Map<String, Object> velocityAttributesMap() {
79
        Map<String, Object> attributesMap = new HashMap<>();
80
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
81
        DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
82
        DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
83
        DateTimeFormatter dateMonthYearFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
84
        DateTimeFormatter dateYearMonthFormatter = DateTimeFormatter.ofPattern("MMM''uu");
31218 tejbeer 85
 
32054 tejbeer 86
        DateTimeFormatter dateMonthChYear = DateTimeFormatter.ofPattern("d''MMM''uu");
87
        DecimalFormat decimalFormatter = new DecimalFormat("0.#");
30859 tejbeer 88
 
32054 tejbeer 89
        NumberFormat numberformat = NumberFormat.getNumberInstance(new Locale("en", "IN"));
30859 tejbeer 90
 
32054 tejbeer 91
        numberformat.setRoundingMode(RoundingMode.HALF_DOWN);
92
        numberformat.setMaximumFractionDigits(2);
93
        numberformat.setMinimumFractionDigits(0);
94
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
95
        attributesMap.put("dateFormatter", dateFormatter);
96
        attributesMap.put("nf", numberformat);
97
        attributesMap.put("dateMonthFormatter", dateMonthFormatter);
98
        attributesMap.put("datehiphenFormatter", datehiphenFormatter);
99
        attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
31218 tejbeer 100
 
32054 tejbeer 101
        attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
102
        attributesMap.put("dateMonthChYear", dateMonthChYear);
103
        attributesMap.put("decimalFormatter", decimalFormatter);
104
        attributesMap.put("noData", "<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
105
        attributesMap.put("dateTimeFormatter", dateTimeFormatter);
37161 ranu 106
        attributesMap.put("version", "386");
37003 ranu 107
        attributesMap.put("cssVersion", "52");
32054 tejbeer 108
        attributesMap.put("isDev", getActiveProfile().equals("dev"));
109
        attributesMap.put("vmUtils", new Utils());
32200 amit.gupta 110
        //attributesMap.put("esc", new EscapeTool());
32054 tejbeer 111
        attributesMap.put("ru", RoundingMode.HALF_UP);
112
        return attributesMap;
113
    }
30470 amit.gupta 114
 
32054 tejbeer 115
    @Bean
116
    public ViewResolver viewResolver() {
117
        VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
118
        bean.setPrefix("");
119
        bean.setSuffix(VELOCITY_PATH_SUFFIX);
35587 ranu 120
        bean.setContentType("text/html;charset=UTF-8");
32054 tejbeer 121
        bean.setRequestContextAttribute("rc");
32773 raveendra. 122
        bean.setExposeSpringMacroHelpers(true);
32054 tejbeer 123
        bean.setAttributesMap(this.velocityAttributesMap());
124
        return bean;
125
    }
21625 kshitij.so 126
 
32054 tejbeer 127
    @Bean
128
    public ViewResolver beanNameViewResolver() {
129
        BeanNameViewResolver resolver = new BeanNameViewResolver();
130
        return resolver;
131
    }
24507 amit.gupta 132
 
32054 tejbeer 133
    @Bean
134
    public VelocityConfigurer velocityConfig() {
135
        VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
136
        Properties velocityProperties = new Properties();
35294 ranu 137
       /* velocityProperties.put("velocimacro.library", "macros.vm");
35289 amit 138
        velocityProperties.put("velocimacro.permissions.allow.inline", "true");
35294 ranu 139
        velocityProperties.put("velocimacro.permissions.allow.inline.to.replace.global", "true");*/
32054 tejbeer 140
        velocityProperties.put("directive.set.null.allowed", "true");
36686 ranu 141
        // Read .vm files as UTF-8 and emit UTF-8 — otherwise non-ASCII chars
142
        // (emoji, accents) in templates render as mojibake (e.g. 📷 → ð).
143
        velocityProperties.put("input.encoding", "UTF-8");
144
        velocityProperties.put("output.encoding", "UTF-8");
32054 tejbeer 145
        velocityConfigurer.setVelocityProperties(velocityProperties);
146
        velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
147
        return velocityConfigurer;
148
    }
21555 kshitij.so 149
 
32788 amit.gupta 150
    @Bean(name = "messageSource")
151
    public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
32054 tejbeer 152
        LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
153
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
154
        messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
32773 raveendra. 155
        messageSource.setDefaultEncoding("UTF-8");
156
 
32054 tejbeer 157
        return messageSource;
158
    }
21555 kshitij.so 159
 
32054 tejbeer 160
    @Bean(name = "multipartResolver")
161
    public CommonsMultipartResolver getCommonsMultipartResolver() {
162
        LOGGER.info("creating common multipart resolver bean");
163
        return new CommonsMultipartResolver();
164
    }
21555 kshitij.so 165
 
32054 tejbeer 166
    @Bean
167
    public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
168
        String activeProfile = getActiveProfile();
21555 kshitij.so 169
 
32054 tejbeer 170
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 171
 
35381 amit 172
        Resource appResource;
173
        Resource sharedResource;
174
 
32054 tejbeer 175
        if ("prod".equals(activeProfile)) {
35381 amit 176
            appResource = new ClassPathResource("/META-INF/prod.properties");
177
            sharedResource = new ClassPathResource("/shared-prod.properties");
32054 tejbeer 178
        } else if ("staging".equals(activeProfile)) {
35381 amit 179
            appResource = new ClassPathResource("/META-INF/staging.properties");
180
            sharedResource = new ClassPathResource("/shared-staging.properties");
32054 tejbeer 181
        } else {
35381 amit 182
            appResource = new ClassPathResource("/META-INF/dev.properties");
183
            sharedResource = new ClassPathResource("/shared-dev.properties");
32054 tejbeer 184
        }
21555 kshitij.so 185
 
35381 amit 186
        resource = appResource;
35832 amit 187
        AppConfig.sharedResource = sharedResource;
21555 kshitij.so 188
 
35381 amit 189
        // Load shared properties from dao first, then app-specific (app-specific can override)
190
        propertySourcesPlaceholderConfigurer.setLocations(sharedResource, appResource);
191
        propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
192
 
32054 tejbeer 193
        return propertySourcesPlaceholderConfigurer;
194
    }
21625 kshitij.so 195
 
32054 tejbeer 196
    private String getActiveProfile() {
197
        Properties properties = new Properties();
198
        try {
199
            properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
200
        } catch (IOException e) {
201
            LOGGER.error("Error in reading env property file.Adding default property" + e);
202
            properties.put("profile", "dev");
203
        }
204
        LOGGER.info("Profile is [{}]", properties.get("profile"));
205
        return (String) properties.get("profile");
206
    }
25811 amit.gupta 207
 
34667 amit.gupta 208
    @Bean(name = "mailSender")
32054 tejbeer 209
    public JavaMailSender getSendgridMailSender() {
210
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
24639 tejbeer 211
 
32054 tejbeer 212
        // Using gmail
213
        mailSender.setHost("smtp.sendgrid.net");
214
        mailSender.setPort(587);
215
        mailSender.setUsername("apikey");
34667 amit.gupta 216
        mailSender.setPassword("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");
24639 tejbeer 217
 
32054 tejbeer 218
        Properties javaMailProperties = new Properties();
219
        javaMailProperties.put("mail.smtp.starttls.enable", "false");
220
        javaMailProperties.put("mail.smtp.auth", "true");
221
        javaMailProperties.put("mail.transport.protocol", "smtp");
222
        javaMailProperties.put("mail.debug", "true");// Prints out everything on
223
        // screen
36062 amit 224
        javaMailProperties.put("mail.smtp.connectiontimeout", "10000");
225
        javaMailProperties.put("mail.smtp.timeout", "10000");
226
        javaMailProperties.put("mail.smtp.writetimeout", "10000");
24639 tejbeer 227
 
32054 tejbeer 228
        mailSender.setJavaMailProperties(javaMailProperties);
229
        return mailSender;
230
    }
24639 tejbeer 231
 
36247 amit 232
    @Bean(name = "gmailRelaySender")
233
    public JavaMailSender getGmailRelaySender() {
234
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
235
        mailSender.setHost("smtp-relay.gmail.com");
236
        mailSender.setPort(587);
34667 amit.gupta 237
 
36247 amit 238
        Properties props = mailSender.getJavaMailProperties();
239
        props.put("mail.smtp.starttls.enable", "true");
240
        props.put("mail.smtp.auth", "false");
241
        props.put("mail.smtp.localhost", "smartdukaan.com");
242
        props.put("mail.smtp.ssl.protocols", "TLSv1.2");
243
        props.put("mail.smtp.ssl.trust", "smtp-relay.gmail.com");
244
        props.put("mail.transport.protocol", "smtp");
245
        props.put("mail.smtp.connectiontimeout", "10000");
246
        props.put("mail.smtp.timeout", "10000");
247
        props.put("mail.smtp.writetimeout", "10000");
248
 
249
        return mailSender;
250
    }
251
 
252
 
32399 amit.gupta 253
    @Bean
32054 tejbeer 254
    public JavaMailSender googleMailSender() {
255
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
34636 ranu 256
        mailSender.setHost("smtp.gmail.com");
257
        mailSender.setPort(465);
258
        mailSender.setUsername("sdtech@smartdukaan.com");
37003 ranu 259
        mailSender.setPassword("gpdschroalhhirox"); // App Password
34636 ranu 260
 
261
        Properties props = mailSender.getJavaMailProperties();
262
        props.put("mail.smtp.auth", "true");
263
        props.put("mail.smtp.ssl.enable", "true");
264
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
265
        props.put("mail.smtp.socketFactory.port", "465");
266
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
36062 amit 267
        props.put("mail.smtp.connectiontimeout", "10000");
268
        props.put("mail.smtp.timeout", "10000");
269
        props.put("mail.smtp.writetimeout", "10000");
34636 ranu 270
 
271
        return mailSender;
272
    }
273
 
32054 tejbeer 274
    @Bean(name = "gson")
275
    public Gson gson() {
25300 tejbeer 276
 
32054 tejbeer 277
        Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
25300 tejbeer 278
 
32054 tejbeer 279
        return gson;
25300 tejbeer 280
 
32054 tejbeer 281
    }
25300 tejbeer 282
 
32054 tejbeer 283
    @Bean(name = "veloctyEngine")
284
    public VelocityEngine velocityEngine() throws VelocityException, IOException {
285
        VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
286
        // Properties props = new Properties();
287
        // props.put("resource.loader", "file");
28339 tejbeer 288
 
32054 tejbeer 289
        // props.put("file.resource.loader.description", "Velocity File Resource
290
        // Loader");
291
        // props.put("file.resource.loader.class",
292
        // "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
293
        // props.put("file.resource.loader.cache", true);
294
        // props.put("file.resource.loader.path", ".");
32773 raveendra. 295
        factory.setPreferFileSystemAccess(false);
296
        // factory.setResourceLoaderPath("classpath:/templates/");
28339 tejbeer 297
 
32773 raveendra. 298
 
32054 tejbeer 299
        Properties velocityProperties = new Properties();
300
        velocityProperties.put("resource.loader", "class");
301
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
302
        velocityProperties.put("file.resource.loader.cache", true);
303
        velocityProperties.put("file.resource.loader.unicode", true);
304
        velocityProperties.put("input.encoding", "UTF-8");
305
        velocityProperties.put("output.encoding", "UTF-8");
306
        velocityProperties.put("overrideLogging", true);
32773 raveendra. 307
        velocityProperties.setProperty("input.encoding", "UTF-8");
308
        velocityProperties.setProperty("output.encoding", "UTF-8");
309
        factory.setVelocityProperties(velocityProperties);
28339 tejbeer 310
 
32054 tejbeer 311
        // velocityProperties.put("file.resource.loader.path", ".");
28339 tejbeer 312
 
32054 tejbeer 313
        factory.setVelocityProperties(velocityProperties);
314
        return factory.createVelocityEngine();
28339 tejbeer 315
 
32054 tejbeer 316
    }
28339 tejbeer 317
 
32054 tejbeer 318
    @Bean
319
    public ObjectMapper objectMapper() {
320
        DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd().appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
321
        DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
33618 tejus.loha 322
 
323
        DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).toFormatter();
32054 tejbeer 324
        JavaTimeModule jtm = new JavaTimeModule();
33618 tejus.loha 325
        jtm.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(sf));
326
        jtm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(df));
327
        jtm.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
328
        jtm.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
32569 amit.gupta 329
        SimpleModule stringModule = new SimpleModule("String trimmer deserialize module");
330
        stringModule.addDeserializer(String.class, new CustomStringDeserializer());
32186 amit.gupta 331
        ObjectMapper mapper = new ObjectMapper()
32187 amit.gupta 332
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
32569 amit.gupta 333
                .registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(jtm)
334
                .registerModule(stringModule); // new module, NOT JSR310Module
32054 tejbeer 335
        return mapper;
336
    }
31282 amit.gupta 337
 
37072 amit 338
    class CustomStringDeserializer extends StringDeserializer implements ContextualDeserializer {
339
 
340
        // Fields whose exact bytes matter — never normalize (credentials, tokens, payloads).
341
        private final java.util.regex.Pattern SENSITIVE_FIELD =
342
                java.util.regex.Pattern.compile("(?i)(password|passwd|pwd|otp|pin|token|secret|signature|hash|base64|apikey|api_key)");
343
 
344
        private final boolean normalize;
345
 
346
        CustomStringDeserializer() {
347
            this(true);
348
        }
349
 
350
        CustomStringDeserializer(boolean normalize) {
351
            this.normalize = normalize;
352
        }
353
 
32569 amit.gupta 354
        @Override
37072 amit 355
        public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
356
            if (property != null && SENSITIVE_FIELD.matcher(property.getName()).find()) {
357
                return new CustomStringDeserializer(false);
358
            }
359
            return this;
360
        }
361
 
362
        @Override
32569 amit.gupta 363
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
364
            String text = super.deserialize(p, ctxt);
37072 amit 365
            if (!normalize) {
366
                return text;
367
            }
368
            // trim + NBSP/tab/zero-width -> single space + collapse doubles
369
            return com.spice.profitmandi.common.util.StringUtils.normalizeWhitespace(text);
32569 amit.gupta 370
        }
371
    }
372
 
373
 
21555 kshitij.so 374
}