Skip to content

Commit

Permalink
ep 4
Browse files Browse the repository at this point in the history
  • Loading branch information
aware-natthaponp committed Feb 20, 2021
1 parent 58eebc8 commit 5a289bc
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class BackendApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.iamnbty.training.backend.business;

import com.iamnbty.training.backend.exception.BaseException;
import com.iamnbty.training.backend.exception.EmailException;
import com.iamnbty.training.backend.service.EmailService;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

@Service
public class EmailBusiness {

private final EmailService emailService;

public EmailBusiness(EmailService emailService) {
this.emailService = emailService;
}

public void sendActivateUserEmail(String email, String name, String token) throws BaseException {
// prepare content (HTML)
String html;
try {
html = readEmailTemplate("email-activate-user.html");
} catch (IOException e) {
throw EmailException.templateNotFound();
}

String finalLink = "http://localhost:4200/activate/" + token;
html = html.replace("${P_NAME}", name);
html = html.replace("${LINK}", finalLink);

// prepare subject
String subject = "Please activate your account";


emailService.send(email, subject, html);
}

private String readEmailTemplate(String filename) throws IOException {
File file = ResourceUtils.getFile("classpath:email/" + filename);
return FileCopyUtils.copyToString(new FileReader(file));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
protected void configure(HttpSecurity http) throws Exception {
http.cors().disable().csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().authorizeRequests().antMatchers("/user/register", "/user/login").anonymous()
.and().authorizeRequests().antMatchers("/actuator/**", "/user/register", "/user/login").anonymous()
.anyRequest().authenticated()
.and().apply(new TokenFilterConfiguerer(tokenService));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.iamnbty.training.backend.exception;

public class EmailException extends BaseException {

public EmailException(String code) {
super("email." + code);
}

public static EmailException templateNotFound() {
return new EmailException("template.not.found");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.iamnbty.training.backend.schedule;

import com.iamnbty.training.backend.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class UserSchedule {

private final UserService userService;

public UserSchedule(UserService userService) {
this.userService = userService;
}

// Schedule Note
// 1 => second
// 2 => minute
// 3 => hour
// 4 => day
// 5 => month
// 6 => year

/**
* Every minute (UTC Time)
*/
@Scheduled(cron = "0 * * * * *")
public void testEveryMinute() {
log.info("Hello, What's up?");
}

/**
* Everyday at 00:00 (UTC Time)
*/
@Scheduled(cron = "0 0 0 * * *")
public void testEveryMidNight() {

}

/**
* Everyday at 10:50 AM (Thai Time)
*/
@Scheduled(cron = "0 50 10 * * *", zone = "Asia/Bangkok")
public void testEverydayNineAM() {
log.info("Hey Hoo!!!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.iamnbty.training.backend.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

private final JavaMailSender mailSender;
@Value("${app.email.from}")
private String from;

public EmailService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}

public void send(String to, String subject, String html) {
MimeMessagePreparator message = mimeMessage -> {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(html, true);
};
mailSender.send(message);
}

}
13 changes: 13 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
spring:
mail:
host: smtp.gmail.com
port: 587
username: YOUR_USERNAME
password: YOUR_PASSWORD
properties:
mail:
smtp:
auth: true
starttls:
enable: true
jpa:
hibernate:
ddl-auto: update
Expand All @@ -16,4 +27,6 @@ app:
token:
secret: m#MySecretGGWP2021
issuer: BackendService
email:
from: [email protected]

14 changes: 14 additions & 0 deletions src/main/resources/email/email-activate-user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to Backend</title>
</head>
<body>

<h1>Hello, ${P_NAME}</h1>
<h2>Please activate your account</h2>
<p>Click <a href="${LINK}">here</a></p>

</body>
</html>
39 changes: 39 additions & 0 deletions src/test/java/com/iamnbty/training/backend/TestEmailBusiness.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.iamnbty.training.backend;

import com.iamnbty.training.backend.business.EmailBusiness;
import com.iamnbty.training.backend.exception.BaseException;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class TestEmailBusiness {

@Autowired
private EmailBusiness emailBusiness;

@Order(1)
@Test
void testSendActivateEmail() throws BaseException {
emailBusiness.sendActivateUserEmail(
TestData.email,
TestData.name,
TestData.token
);
}

interface TestData {

String email = "[email protected]";

String name = "Natthapon Pinyo";

String token = "m#@:LSDIFDISIDFO99020kkddd";

}

}

0 comments on commit 5a289bc

Please sign in to comment.