From 64c6252145e4b602ec8a8376d6101334e8f0d8aa Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 3 Jan 2018 17:19:26 +0900 Subject: [PATCH] RMI Crawler->JMX Crawler --- pom.xml | 14 ++- .../loafle/overflow/commons/model/Mail.java | 108 ++++++++++++++++++ .../overflow/commons/utils/EmailSender.java | 39 +++++-- .../email/service/EmailAuthService.java | 18 ++- .../module/member/service/MemberService.java | 3 +- .../overflow/spring/MailConfiguration.java | 12 ++ src/main/resources/local/init.sql | 3 + src/main/resources/local/velocity.properties | 6 + src/main/resources/signup_email.vm | 13 +++ 9 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/loafle/overflow/commons/model/Mail.java create mode 100644 src/main/resources/local/velocity.properties create mode 100644 src/main/resources/signup_email.vm diff --git a/pom.xml b/pom.xml index a87c19c..861f558 100644 --- a/pom.xml +++ b/pom.xml @@ -27,8 +27,9 @@ 4.2.3.RELEASE 5.2.10.Final 1.4.7 + 1.6.0 1.9.13 - + 1.7 docker.loafle.net/overflow @@ -128,6 +129,17 @@ ${javax.mail.version} + + javax.mail + javax.mail-api + ${javax.mail-api.version} + + + + org.apache.velocity + velocity + ${apache.velocity.version} + commons-codec diff --git a/src/main/java/com/loafle/overflow/commons/model/Mail.java b/src/main/java/com/loafle/overflow/commons/model/Mail.java new file mode 100644 index 0000000..4c9fb5d --- /dev/null +++ b/src/main/java/com/loafle/overflow/commons/model/Mail.java @@ -0,0 +1,108 @@ +package com.loafle.overflow.commons.model; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +/** + * Created by geek on 18. 1. 3. + */ +public class Mail { + private String mailFrom; + + private String mailTo; + + private String mailCc; + + private String mailBcc; + + private String mailSubject; + + private String mailContent; + + private String contentType; + + private List< Object > attachments; + + private Map< String, Object > model; + + public Mail() { + contentType = "text/plain"; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public String getMailBcc() { + return mailBcc; + } + + public void setMailBcc(String mailBcc) { + this.mailBcc = mailBcc; + } + + public String getMailCc() { + return mailCc; + } + + public void setMailCc(String mailCc) { + this.mailCc = mailCc; + } + + public String getMailFrom() { + return mailFrom; + } + + public void setMailFrom(String mailFrom) { + this.mailFrom = mailFrom; + } + + public String getMailSubject() { + return mailSubject; + } + + public void setMailSubject(String mailSubject) { + this.mailSubject = mailSubject; + } + + public String getMailTo() { + return mailTo; + } + + public void setMailTo(String mailTo) { + this.mailTo = mailTo; + } + + public Date getMailSendDate() { + return new Date(); + } + + public String getMailContent() { + return mailContent; + } + + public void setMailContent(String mailContent) { + this.mailContent = mailContent; + } + + public List< Object > getAttachments() { + return attachments; + } + + public void setAttachments(List < Object > attachments) { + this.attachments = attachments; + } + + public Map< String, Object > getModel() { + return model; + } + + public void setModel(Map < String, Object > model) { + this.model = model; + } +} diff --git a/src/main/java/com/loafle/overflow/commons/utils/EmailSender.java b/src/main/java/com/loafle/overflow/commons/utils/EmailSender.java index c6796e8..a9100d2 100644 --- a/src/main/java/com/loafle/overflow/commons/utils/EmailSender.java +++ b/src/main/java/com/loafle/overflow/commons/utils/EmailSender.java @@ -1,13 +1,15 @@ package com.loafle.overflow.commons.utils; +import com.loafle.overflow.commons.model.Mail; import org.apache.commons.codec.binary.Base64; +import org.apache.velocity.app.VelocityEngine; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.FileSystemResource; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; +import org.springframework.ui.velocity.VelocityEngineUtils; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; @@ -15,6 +17,7 @@ import javax.crypto.spec.SecretKeySpec; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; +import java.util.Map; /** * Created by geek on 17. 8. 30. @@ -25,32 +28,35 @@ public class EmailSender { @Autowired private JavaMailSender mailSender; + @Autowired + private VelocityEngine velocityEngine; + private String key = "loafle@RandomKey"; private String initVector = "loafleInitVector"; - public void sendSimpleEmail(String to, String sub, String message) throws MailException { + public void sendSimpleEmail(Mail mail) throws MailException { SimpleMailMessage message1 = new SimpleMailMessage(); - message1.setTo(to); - message1.setSubject(sub); - message1.setText(message); + message1.setTo(mail.getMailTo()); + message1.setSubject(mail.getMailSubject()); + message1.setText(getContentFromTemplate(mail.getModel())); message1.setFrom("geek@loafle.com"); mailSender.send(message1); } - public void sendMailWithAttachment(String to, String sub, String text, String path) throws MessagingException { + public void sendMailWithAttachment(Mail mail) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); // pass 'true' to the constructor to create a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); - helper.setTo(to); - helper.setSubject(sub); - helper.setText(text); + helper.setTo(mail.getMailTo()); + helper.setSubject(mail.getMailSubject()); + helper.setText(mail.getMailContent()); helper.setFrom("geek@loafle.com"); - FileSystemResource file = new FileSystemResource(new File(path)); - helper.addAttachment("Invoice", file); +// FileSystemResource file = new FileSystemResource(new File(mail.getAttachments())); +// helper.addAttachment("Invoice", file); mailSender.send(message); } @@ -93,4 +99,15 @@ public class EmailSender { return null; } + + private String getContentFromTemplate(Map model) { + StringBuffer content = new StringBuffer(); + try { + content.append(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,"/templates/email-template.vm", "UTF-8", model)); + } catch (Exception e) { + e.printStackTrace(); + } + return content.toString(); + } + } diff --git a/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java b/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java index 8451e1f..2357ee4 100644 --- a/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java +++ b/src/main/java/com/loafle/overflow/module/email/service/EmailAuthService.java @@ -1,5 +1,6 @@ package com.loafle.overflow.module.email.service; +import com.loafle.overflow.commons.model.Mail; import com.loafle.overflow.commons.utils.EmailSender; import com.loafle.overflow.module.domain.dao.DomainDAO; import com.loafle.overflow.module.domain.dao.DomainMemberDAO; @@ -20,9 +21,7 @@ import org.springframework.stereotype.Service; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; -import java.util.Calendar; -import java.util.Date; -import java.util.List; +import java.util.*; /** * Created by geek on 17. 6. 28. @@ -58,7 +57,18 @@ public class EmailAuthService { String encode = URLEncoder.encode(en, "UTF-8"); // System.out.println("encode = [" + encode + "]"); - emailSender.sendSimpleEmail(memberEmail, "Confirm Email", "http://127.0.0.1:19080/account/check_email?key="+ encode +"\r\nConfirm Email"); + Mail mail = new Mail(); + mail.setMailTo(memberEmail); + mail.setMailSubject("Confirm Email"); + mail.setMailContent("http://127.0.0.1:19080/account/check_email?key="+ encode +"\r\nConfirm Email"); + Map model = new HashMap<>(); + model.put("firstName", auth.getMember().getName()); + model.put("lastName", auth.getMember().getCompanyName()); + model.put("location", "Seoul"); + model.put("signature", "www.loafle.com"); + + mail.setModel(model); + emailSender.sendSimpleEmail(mail); this.emailAuthDAO.save(auth); diff --git a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java index c96992d..827499a 100644 --- a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java +++ b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java @@ -70,7 +70,8 @@ public class MemberService { } m.setSigninFailCount(m.getSigninFailCount()+1); this.modify(m); - throw new SignInPwNotMatchException(); +// throw new SignInPwNotMatchException(); + return null; } m.setSigninFailCount(0); diff --git a/src/main/java/com/loafle/overflow/spring/MailConfiguration.java b/src/main/java/com/loafle/overflow/spring/MailConfiguration.java index 2781511..66cda61 100644 --- a/src/main/java/com/loafle/overflow/spring/MailConfiguration.java +++ b/src/main/java/com/loafle/overflow/spring/MailConfiguration.java @@ -1,11 +1,15 @@ package com.loafle.overflow.spring; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.exception.VelocityException; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.ui.velocity.VelocityEngineFactory; +import java.io.IOException; import java.util.Properties; /** @@ -54,6 +58,14 @@ public class MailConfiguration { return javaMailProperties; } + + @Bean + public VelocityEngine getVelocityEngine() throws VelocityException, IOException { + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("/velocity.properties")); + return new VelocityEngine(properties); + } + } // diff --git a/src/main/resources/local/init.sql b/src/main/resources/local/init.sql index 800f903..7e9a220 100644 --- a/src/main/resources/local/init.sql +++ b/src/main/resources/local/init.sql @@ -864,6 +864,9 @@ INSERT INTO public."member" (company_name,create_date,email,"name",phone,pw,stat INSERT INTO public.email_auth (auth_confirm_date,create_date,email_auth_key,member_id) VALUES ( NULL,'2017-06-26 15:28:48.895','dbseogns1234',1); +INSERT INTO public.email_auth (auth_confirm_date,create_date,email_auth_key,member_id) VALUES ( +NULL,'2017-11-22 12:28:48.895','dbseogns1234',1); + INSERT INTO public."domain" (create_date,"name") VALUES ( '2017-06-26 11:25:44.866','overFlow''s domain'); diff --git a/src/main/resources/local/velocity.properties b/src/main/resources/local/velocity.properties new file mode 100644 index 0000000..c298824 --- /dev/null +++ b/src/main/resources/local/velocity.properties @@ -0,0 +1,6 @@ +resource.loader=jar +jar.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader +jar.runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem +jar.runtime.log.logsystem.log4j.category=velocity +jar.resource.loader.cache=true +input.encoding=UTF-8 \ No newline at end of file diff --git a/src/main/resources/signup_email.vm b/src/main/resources/signup_email.vm new file mode 100644 index 0000000..ac6dcf3 --- /dev/null +++ b/src/main/resources/signup_email.vm @@ -0,0 +1,13 @@ + + + + + +

Dear ${firstName} ${lastName},

+

Sending Email Velocity Template Test !!!

+

Thanks

+

${signature}

+

${location}

+ + + \ No newline at end of file