SHARE
TWEET

Untitled

a guest Aug 20th, 2019 176 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
  2.  
  3. <context:component-scan base-package="com.erodrigo.confianza" />
  4.  
  5. <!-- Anotaciones en tareas programadas -->
  6. <task:annotation-driven/>
  7.  
  8. <!-- Enables the Spring MVC @Controller programming model -->
  9. <context:annotation-config />
  10. <annotation-driven>
  11.     <message-converters>
  12.         <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
  13.         <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
  14.     </message-converters>
  15. </annotation-driven>
  16.      
  17. @Service
  18. public interface EmailTaskService {
  19.  
  20. /**
  21.  *
  22.  * @param usuario
  23.  * @throws Exception
  24.  */
  25. @Async
  26. public void sendMailNewUser(UsuarioDto usuario) throws Exception;
  27.  
  28. /**
  29.  *
  30.  * @param contratista
  31.  * @param tipoDocumento
  32.  * @param nombreDocumento
  33.  * @throws Exception
  34.  */
  35. @Async
  36. public void sendMailDocumentoCaducado(EmpresasDto contratista, String nombreDocumento, String fechaCaducidad) throws Exception;
  37.  
  38. /**
  39.  *
  40.  * @param usuario
  41.  * @throws Exception
  42.  */
  43. @Async
  44. public void sendMailResetPass(UsuarioDto usuario) throws Exception;
  45.  
  46. /**
  47.  *
  48.  * @throws Exception
  49.  */
  50. @Async
  51. public void sendMailTest() throws Exception;
  52.  
  53. /**
  54.  *
  55.  * @return
  56.  * @throws Exception
  57.  */
  58. public MimeMessage createMimeMessage() throws Exception;
  59.  
  60. /**
  61.  *
  62.  * @param message
  63.  * @throws Exception
  64.  */
  65. public void send(MimeMessagePreparator message) throws Exception;
  66.  
  67. /**
  68.  *
  69.  * @param message
  70.  * @throws Exception
  71.  */
  72. public void send(MimeMessage message) throws Exception;
  73.  
  74. /**
  75.  *
  76.  * @param message
  77.  * @throws Exception
  78.  */
  79. public void send(SimpleMailMessage message) throws Exception;
  80. }
  81.      
  82. @Service
  83. public class EmailTaskServiceImpl implements EmailTaskService {
  84.  
  85. private PropertiesConfiguration properties;
  86. private MailSender mailSender;
  87. private JavaMailSender javaMailSender;
  88. private VelocityEngine velocityEngine;
  89.  
  90. @Override
  91. @Async
  92. public void sendMailNewUser(final UsuarioDto usuario) throws Exception {
  93.     MimeMessagePreparator preparator = new MimeMessagePreparator() {
  94.         public void prepare(MimeMessage mimeMessage) throws Exception {
  95.             MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
  96.             message.setTo(usuario.getEmail());
  97.             message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
  98.  
  99.             Map<String, Object> map = new HashMap<String, Object>();
  100.             System.out.println(usuario.getNombreCompleto());
  101.             map.put("nombreUsuario", usuario.getNombreCompleto());
  102.             map.put("user", usuario.getEmail());
  103.             map.put("userPass", usuario.getPass());
  104.             map.put("enlaceApp", properties.getString(EmailsConstants.URL_APP));
  105.  
  106.             @SuppressWarnings("deprecation")
  107.             String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
  108.                     EmailsConstants.VM_NUEVO_USUARIO, map);
  109.  
  110.             message.setSubject(EmailsConstants.SUBJECT_NEW_USER);
  111.             message.setText(text, true);
  112.         }
  113.     };
  114.  
  115.     send(preparator);
  116. }
  117.  
  118. @Override
  119. @Async
  120. public void sendMailDocumentoCaducado(final EmpresasDto contratista, final String nombreDocumento, final String fechaCaducidad) throws Exception {
  121.     MimeMessagePreparator preparator = new MimeMessagePreparator() {
  122.         public void prepare(MimeMessage mimeMessage) throws Exception {
  123.             MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
  124.             message.setTo(contratista.getEmail());
  125.             message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
  126.  
  127.             Map<String, Object> map = new HashMap<String, Object>();
  128.             map.put("nombreContratista", contratista.getRazonSocial());
  129.             map.put("nombreDocumento", nombreDocumento);
  130.             map.put("fechaCaducidad", fechaCaducidad);
  131.             map.put("enlaceApp", properties.getString(EmailsConstants.URL_APP));
  132.  
  133.             @SuppressWarnings("deprecation")
  134.             String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
  135.                     EmailsConstants.VM_DOCUMENTO_CADUCADO, map);
  136.  
  137.             message.setSubject(EmailsConstants.SUBJECT_DOCUMENTO_CADUCADO);
  138.             message.setText(text, true);
  139.         }
  140.     };
  141.  
  142.     send(preparator);
  143. }
  144.  
  145. @Override
  146. @Async
  147. public void sendMailResetPass(final UsuarioDto usuario) throws Exception {
  148.     MimeMessagePreparator preparator = new MimeMessagePreparator() {
  149.         public void prepare(MimeMessage mimeMessage) throws Exception {
  150.             MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
  151.             message.setTo(usuario.getEmail());
  152.             message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
  153.  
  154.             Map<String, Object> map = new HashMap<String, Object>();
  155.             System.out.println(usuario.getNombreCompleto());
  156.             map.put("nombreUsuario", usuario.getNombreCompleto());
  157.             map.put("user", usuario.getEmail());
  158.             map.put("userPass", usuario.getPass());
  159.             map.put("enlaceApp", properties.getString(EmailsConstants.URL_APP));
  160.  
  161.             @SuppressWarnings("deprecation")
  162.             String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
  163.                     EmailsConstants.VM_RESET_PASS, map);
  164.  
  165.             message.setSubject(EmailsConstants.SUBJECT_RESET_PASS);
  166.             message.setText(text, true);
  167.         }
  168.     };
  169.  
  170.     send(preparator);
  171. }
  172.  
  173. @Override
  174. @Async
  175. public void sendMailTest() throws Exception {
  176.     System.out.println("Entro en sendMailTest()");
  177.     MimeMessagePreparator preparator = new MimeMessagePreparator() {
  178.         public void prepare(MimeMessage mimeMessage) throws Exception {
  179.             System.out.println("Entro prepare()");
  180.             MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
  181.             message.setTo("rafelcity@msn.com");
  182.             message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
  183.  
  184.             Map<String, Object> map = new HashMap<String, Object>();
  185.  
  186.             @SuppressWarnings("deprecation")
  187.             String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
  188.                     "/velocity/test.vm", map);
  189.  
  190.             message.setSubject("Test");
  191.             message.setText(text, true);
  192.         }
  193.     };
  194.  
  195.     System.out.println("Envio e correo");
  196.     send(preparator);
  197.     System.out.println("FIN envio correo");
  198. }
  199.  
  200. @Override
  201. public MimeMessage createMimeMessage() throws Exception {
  202.     return javaMailSender.createMimeMessage();
  203. }
  204.  
  205. @Override
  206. public void send(MimeMessagePreparator message) throws Exception {
  207.     javaMailSender.send(message);
  208. }
  209.  
  210. @Override
  211. public void send(MimeMessage message) throws Exception {
  212.     javaMailSender.send(message);
  213. }
  214.  
  215. @Override
  216. public void send(SimpleMailMessage message) throws Exception {
  217.     mailSender.send(message);
  218. }
  219.  
  220. // GETTERS and SETTERS
  221. public PropertiesConfiguration getProperties() {
  222.     return properties;
  223. }
  224.  
  225. public void setProperties(PropertiesConfiguration properties) {
  226.     this.properties = properties;
  227. }
  228.  
  229. public VelocityEngine getVelocityEngine() {
  230.     return velocityEngine;
  231. }
  232.  
  233. public void setVelocityEngine(VelocityEngine velocityEngine) {
  234.     this.velocityEngine = velocityEngine;
  235. }
  236.  
  237. public MailSender getMailSender() {
  238.     return mailSender;
  239. }
  240.  
  241. public void setMailSender(MailSender mailSender) {
  242.     this.mailSender = mailSender;
  243. }
  244.  
  245. public JavaMailSender getJavaMailSender() {
  246.     return javaMailSender;
  247. }
  248.  
  249. public void setJavaMailSender(JavaMailSender javaMailSender) {
  250.     this.javaMailSender = javaMailSender;
  251. }
  252. }
  253.      
  254. @Configuration
  255. @EnableAsync
  256. @EnableScheduling
  257.  
  258. public class AsyncConfiguration implements AsyncConfigurer {
  259.  
  260.     private final Logger log = LoggerFactory.getLogger(AsyncConfiguration.class);
  261.  
  262.     @Inject
  263.     private JHipsterProperties jHipsterProperties;
  264.  
  265.     @Override
  266.     @Bean(name = "taskExecutor")
  267.     public Executor getAsyncExecutor() {
  268.         log.debug("Creating Async Task Executor");
  269.         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  270.         executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
  271.         executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
  272.         executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
  273.         executor.setThreadNamePrefix("codelogs-Executor-");
  274.         return new ExceptionHandlingAsyncTaskExecutor(executor);
  275.     }
  276.  
  277.     @Override
  278.     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
  279.         return new SimpleAsyncUncaughtExceptionHandler();
  280.     }
  281. }
  282.      
  283. @Service
  284.      
  285. private final Logger log = LoggerFactory.getLogger(MailService.class);
  286.  
  287. private static final String USER = "user";
  288.  
  289. private static final String BASE_URL = "baseUrl";
  290.  
  291. @Inject
  292. private JHipsterProperties jHipsterProperties;
  293.  
  294. @Inject
  295. private JavaMailSenderImpl javaMailSender;
  296.  
  297. @Inject
  298. private MessageSource messageSource;
  299.  
  300. @Inject
  301. private SpringTemplateEngine templateEngine;
  302.  
  303. @Async
  304. public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
  305.     log.debug("Send e-mail[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
  306.         isMultipart, isHtml, to, subject, content);
  307.  
  308.     // Prepare message using a Spring helper
  309.     MimeMessage mimeMessage = javaMailSender.createMimeMessage();
  310.     try {
  311.         MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
  312.         message.setTo(to);
  313.         message.setFrom(jHipsterProperties.getMail().getFrom());
  314.         message.setSubject(subject);
  315.         message.setText(content, isHtml);
  316.         javaMailSender.send(mimeMessage);
  317.         log.debug("Sent e-mail to User '{}'", to);
  318.     } catch (Exception e) {
  319.         log.warn("E-mail could not be sent to user '{}'", to, e);
  320.     }
  321. }
  322.  
  323. @Async
  324. public void sendActivationEmail(User user) {
  325.     log.debug("Sending activation e-mail to '{}'", user.getEmail());
  326.     Locale locale = Locale.forLanguageTag(user.getLangKey());
  327.     Context context = new Context(locale);
  328.     context.setVariable(USER, user);
  329.     context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
  330.     String content = templateEngine.process("activationEmail", context);
  331.     String subject = messageSource.getMessage("email.activation.title", null, locale);
  332.     sendEmail(user.getEmail(), subject, content, false, true);
  333. }
  334.  
  335. @Async
  336. public void sendCreationEmail(User user) {
  337.     log.debug("Sending creation e-mail to '{}'", user.getEmail());
  338.     Locale locale = Locale.forLanguageTag(user.getLangKey());
  339.     Context context = new Context(locale);
  340.     context.setVariable(USER, user);
  341.     context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
  342.     String content = templateEngine.process("creationEmail", context);
  343.     String subject = messageSource.getMessage("email.activation.title", null, locale);
  344.     sendEmail(user.getEmail(), subject, content, false, true);
  345. }
  346.  
  347. @Async
  348. public void sendPasswordResetMail(User user) {
  349.     log.debug("Sending password reset e-mail to '{}'", user.getEmail());
  350.     Locale locale = Locale.forLanguageTag(user.getLangKey());
  351.     Context context = new Context(locale);
  352.     context.setVariable(USER, user);
  353.     context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
  354.     String content = templateEngine.process("passwordResetEmail", context);
  355.     String subject = messageSource.getMessage("email.reset.title", null, locale);
  356.     sendEmail(user.getEmail(), subject, content, false, true);
  357. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top