Subversion Repositories SmartDukaan

Rev

Rev 34711 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed


package com.spice.profitmandi.web.config;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;

@Configuration
@ComponentScan("com.spice.profitmandi.*")
public class AppConfig {

        private static final String PATH_PREFIX = "/WEB-INF/views/";
        private static final String PATH_SUFFIX = ".jsp";
        private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
        private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
        private static Resource resource;

        public static Resource getResource() {
                return resource;
        }

        public void setResource(Resource resource) {
                AppConfig.resource = resource;
        }

        /*
         * @Bean(name = "viewResolver") public InternalResourceViewResolver
         * getViewResolver() { LOGGER.debug("creating view resolver bean with prefix : "
         * + PATH_PREFIX + " and suffix : " + PATH_SUFFIX); InternalResourceViewResolver
         * viewResolver = new InternalResourceViewResolver();
         * viewResolver.setPrefix(PATH_PREFIX); viewResolver.setSuffix(PATH_SUFFIX);
         * return viewResolver; }
         */

        @Bean(name = "messageSource")
        public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
                LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
                ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
                messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
                return messageSource;
        }

        @Bean(name = "multipartResolver")
        public CommonsMultipartResolver getCommonsMultipartResolver() {
                LOGGER.info("creating common multipart resolver bean");
                CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
                multipartResolver.setMaxUploadSizePerFile(5000000);
                multipartResolver.setMaxUploadSize(5000000);
                return multipartResolver;
        }

        @Bean
        public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
                String activeProfile;

                PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();

                Properties properties = new Properties();
                try {
                        properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
                } catch (IOException e) {
                        LOGGER.error("Error in reading env property file.Adding default property" + e);
                        properties.put("profile", "dev");
                }
                activeProfile = (String) properties.get("profile");

                Resource appResource;
                Resource sharedResource;

                if ("prod".equals(activeProfile)) {
                        appResource = new ClassPathResource("/META-INF/prod.properties");
                        sharedResource = new ClassPathResource("/shared-prod.properties");
                } else if ("staging".equals(activeProfile)) {
                        appResource = new ClassPathResource("/META-INF/staging.properties");
                        sharedResource = new ClassPathResource("/shared-staging.properties");
                } else {
                        appResource = new ClassPathResource("/META-INF/dev.properties");
                        sharedResource = new ClassPathResource("/shared-dev.properties");
                }

                resource = appResource;

                // Load shared properties from dao first, then app-specific (app-specific can override)
                propertySourcesPlaceholderConfigurer.setLocations(sharedResource, appResource);
                propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);

                return propertySourcesPlaceholderConfigurer;
        }

        @Bean
        public JavaMailSender googleMailSender() {
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
                mailSender.setHost("smtp.gmail.com");
                mailSender.setPort(465);
                mailSender.setUsername("sdtech@smartdukaan.com");
                mailSender.setPassword("gpdschroalhhirox"); // App Password

                Properties props = mailSender.getJavaMailProperties();
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.ssl.enable", "true");
                props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
                props.put("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.debug", "true");

                return mailSender;
        }

        /*@Bean
        public JavaMailSender googleMailSender() {
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
                // Using gmail
                mailSender.setHost("smtp.gmail.com");
                mailSender.setPort(587);
                mailSender.setUsername("build@shop2020.in");
                mailSender.setPassword("cafe@nes");

                Properties javaMailProperties = new Properties();
                javaMailProperties.put("mail.smtp.starttls.enable", "true");
                javaMailProperties.put("mail.smtp.auth", "true");
                javaMailProperties.put("mail.transport.protocol", "smtp");
                javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
                mailSender.setJavaMailProperties(javaMailProperties);
                return mailSender;
        }*/

        /*@Bean(name = "mailSender")
        public JavaMailSender getSendgridMailSender() {
                return googleMailSender();
        }*/

        @Bean(name = "mailSender")
        public JavaMailSender getSendgridMailSender() {
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

                // Using gmail
                mailSender.setHost("smtp.sendgrid.net");
                mailSender.setPort(587);
                mailSender.setUsername("apikey");
                mailSender.setPassword("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");

                Properties javaMailProperties = new Properties();
                javaMailProperties.put("mail.smtp.starttls.enable", "false");
                javaMailProperties.put("mail.smtp.auth", "true");
                javaMailProperties.put("mail.transport.protocol", "smtp");
                javaMailProperties.put("mail.debug", "true");// Prints out everything on screen

                mailSender.setJavaMailProperties(javaMailProperties);
                return mailSender;
        }

        @Bean(name = "gson")
        public Gson gson() {

                Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
                                .registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
                                .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter())
                                .create();

                return gson;

        }

        @Bean(name = "veloctyEngine")
        public VelocityEngine velocityEngine() throws VelocityException, IOException {
                VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
                // Properties props = new Properties();
                // props.put("resource.loader", "file");

                // props.put("file.resource.loader.description", "Velocity File Resource
                // Loader");
                // props.put("file.resource.loader.class",
                // "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
                // props.put("file.resource.loader.cache", true);
                // props.put("file.resource.loader.path", ".");

                Properties velocityProperties = new Properties();
                velocityProperties.put("resource.loader", "class");
                velocityProperties.put("class.resource.loader.class",
                                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
                velocityProperties.put("file.resource.loader.cache", true);
                velocityProperties.put("file.resource.loader.unicode", true);
                velocityProperties.put("input.encoding", "UTF-8");
                velocityProperties.put("output.encoding", "UTF-8");
                velocityProperties.put("overrideLogging", true);

        //      velocityProperties.put("file.resource.loader.path", ".");

                factory.setVelocityProperties(velocityProperties);

                return factory.createVelocityEngine();

        }
}