Subversion Repositories SmartDukaan

Rev

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