Skip to content

Commit 49351bd

Browse files
committed
Improve constants class
1 parent e80c0bd commit 49351bd

File tree

25 files changed

+176
-162
lines changed

25 files changed

+176
-162
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,22 @@ Creating database migration files
113113

114114
## Environment variables
115115

116-
| **Descrição** | **parâmetro** | **Valor padrão** |
117-
| ------------------------------------------- | -------------------------------------- | ------------------------- |
118-
| Server port | `SERVER_PORT` | 8080 |
119-
| database url | `DB_URL` | localhost:5432/common_app |
120-
| username (database) | `DB_USERNAME` | root |
121-
| user password (database) | `DB_PASSWORD` | root |
122-
| displays the generated sql in the logger | `DB_SHOW_SQL` | false |
123-
| set maximum database connections | `DB_MAX_CONNECTIONS` | 5 |
124-
| secret value in token generation | `TOKEN_SECRET` | secret |
125-
| token expiration time in hours | `TOKEN_EXPIRATION_IN_HOURS` | 24 |
126-
| refresh token expiry time in days | `REFRESH_TOKEN_EXPIRATION_IN_DAYS` | 7 |
127-
| SMTP server address | `SMTP_HOST` | smtp.gmail.com |
128-
| SMTP server port | `SMTP_PORT` | 587 |
129-
| SMTP username | `SMTP_USERNAME` | user |
130-
| SMTP server password | `SMTP_PASSWORD` | secret |
116+
| **Descrição** | **Parameter** | **Valor padrão** |
117+
|------------------------------------------|------------------------------------|---------------------------|
118+
| Server port | `SERVER_PORT` | 8080 |
119+
| database url | `DB_URL` | localhost:5432/common_app |
120+
| username (database) | `DB_USERNAME` | root |
121+
| user password (database) | `DB_PASSWORD` | root |
122+
| displays the generated sql in the logger | `DB_SHOW_SQL` | false |
123+
| set maximum database connections | `DB_MAX_CONNECTIONS` | 5 |
124+
| secret value in token generation | `TOKEN_SECRET` | secret |
125+
| token expiration time in hours | `TOKEN_EXPIRATION_IN_HOURS` | 24 |
126+
| refresh token expiry time in days | `REFRESH_TOKEN_EXPIRATION_IN_DAYS` | 7 |
127+
| SMTP server address | `SMTP_HOST` | smtp.gmail.com |
128+
| SMTP server port | `SMTP_PORT` | 587 |
129+
| SMTP username | `SMTP_USERNAME` | user |
130+
| SMTP server password | `SMTP_PASSWORD` | secret |
131+
| time for recovery email to expire | `MINUTES_TO_EXPIRE_RECOVERY_CODE` | 20 |
131132

132133
> these variables are defined in: [**application.properties**](./src/main/resources/application.properties)
133134
>

pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,4 @@
250250
</plugin>
251251
</plugins>
252252
</build>
253-
<reporting>
254-
<plugins>
255-
<plugin>
256-
<groupId>org.apache.maven.plugins</groupId>
257-
<artifactId>maven-checkstyle-plugin</artifactId>
258-
<configuration>
259-
<configLocation>config/sun_checks.xml</configLocation>
260-
</configuration>
261-
</plugin>
262-
</plugins>
263-
</reporting>
264253
</project>

src/main/java/com/github/throyer/common/springboot/CommonApiApplication.java renamed to src/main/java/com/github/throyer/common/springboot/Application.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

66
@SpringBootApplication
7-
public class CommonApiApplication {
7+
public class Application {
88

99
public static void main(String... args) {
10-
SpringApplication.run(CommonApiApplication.class, args);
10+
SpringApplication.run(Application.class, args);
1111
}
1212
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.throyer.common.springboot.domain.mail.model;
2+
3+
public interface Addressable {
4+
String getEmail();
5+
}

src/main/java/com/github/throyer/common/springboot/domain/mail/service/MailService.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.http.HttpStatus;
109
import org.springframework.mail.MailException;
1110
import org.springframework.mail.javamail.JavaMailSender;
1211
import org.springframework.mail.javamail.MimeMessageHelper;
1312
import org.springframework.stereotype.Service;
1413
import org.springframework.web.server.ResponseStatusException;
1514
import org.thymeleaf.TemplateEngine;
1615

16+
import static com.github.throyer.common.springboot.utils.Constants.MAIL.*;
17+
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
18+
1719
@Service
1820
public class MailService {
19-
20-
@Autowired
21-
private TemplateEngine engine;
21+
22+
private final TemplateEngine engine;
23+
private final JavaMailSender sender;
2224

2325
@Autowired
24-
private JavaMailSender sender;
26+
public MailService(TemplateEngine engine, JavaMailSender sender) {
27+
this.engine = engine;
28+
this.sender = sender;
29+
}
2530

26-
private static Logger LOGGER = LoggerFactory.getLogger(MailService.class);
27-
private static String ERROR_MESSAGE = "Error sending email.";
28-
29-
private static final Boolean CONTENT_IS_HTML = true;
31+
private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class);
3032

3133
public void send(Email email) {
3234
try {
@@ -36,10 +38,10 @@ public void send(Email email) {
3638
helper.setSubject(email.getSubject());
3739
helper.setText(engine.process(email.getTemplate(), email.getContext()), CONTENT_IS_HTML);
3840
sender.send(message);
39-
LOGGER.info("Email successfully sent to: {}", email.getDestination());
41+
LOGGER.info(EMAIL_SUCCESSFULLY_SENT_TO, email.getDestination());
4042
} catch (MessagingException | MailException exception) {
41-
LOGGER.error(ERROR_MESSAGE, exception);
42-
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, ERROR_MESSAGE);
43+
LOGGER.error(ERROR_SENDING_EMAIL_MESSAGE, exception);
44+
throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ERROR_SENDING_EMAIL_MESSAGE);
4345
}
4446
}
4547
}

src/main/java/com/github/throyer/common/springboot/domain/mail/exceptions/EmailValidations.java renamed to src/main/java/com/github/throyer/common/springboot/domain/mail/validation/EmailValidations.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
package com.github.throyer.common.springboot.domain.mail.exceptions;
2-
3-
import java.util.List;
1+
package com.github.throyer.common.springboot.domain.mail.validation;
42

3+
import com.github.throyer.common.springboot.domain.mail.exceptions.EmailNotUniqueException;
4+
import com.github.throyer.common.springboot.domain.mail.model.Addressable;
55
import com.github.throyer.common.springboot.domain.user.repository.UserRepository;
6-
76
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.stereotype.Component;
98
import org.springframework.validation.BindingResult;
109
import org.springframework.validation.ObjectError;
11-
import com.github.throyer.common.springboot.domain.management.model.Addressable;
12-
import com.github.throyer.common.springboot.errors.Error;
10+
11+
import static com.github.throyer.common.springboot.utils.Constants.MAIL.*;
1312

1413
@Component
1514
public class EmailValidations {
1615

1716
private static UserRepository repository;
1817

19-
private static String FIELD = "email";
20-
private static String MESSAGE = "This email has already been used by another user. Please use a different email.";
21-
22-
private static final List<Error> EMAIL_ERROR = List.of(new Error(FIELD, MESSAGE));
23-
2418
@Autowired
2519
public EmailValidations(UserRepository repository) {
2620
EmailValidations.repository = repository;
@@ -34,7 +28,7 @@ public static void validateEmailUniqueness(Addressable entity) {
3428

3529
public static void validateEmailUniqueness(Addressable entity, BindingResult result) {
3630
if (repository.existsByEmail(entity.getEmail())) {
37-
result.addError(new ObjectError(FIELD, MESSAGE));
31+
result.addError(new ObjectError(EMAIL_FIELD, EMAIL_ALREADY_USED_MESSAGE));
3832
}
3933
}
4034

@@ -66,7 +60,7 @@ public static void validateEmailUniquenessOnModify(
6660
var emailAlreadyUsed = repository.existsByEmail(newEmail);
6761

6862
if (changedEmail && emailAlreadyUsed) {
69-
result.addError(new ObjectError(FIELD, MESSAGE));
63+
result.addError(new ObjectError(EMAIL_FIELD, EMAIL_ALREADY_USED_MESSAGE));
7064
}
7165
}
7266
}

src/main/java/com/github/throyer/common/springboot/domain/management/model/Addressable.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,69 @@
11
package com.github.throyer.common.springboot.domain.recovery.service;
22

3-
import com.github.throyer.common.springboot.domain.recovery.model.RecoveryEmail;
3+
import com.github.throyer.common.springboot.domain.mail.service.MailService;
44
import com.github.throyer.common.springboot.domain.recovery.entity.Recovery;
5+
import com.github.throyer.common.springboot.domain.recovery.model.RecoveryEmail;
56
import com.github.throyer.common.springboot.domain.recovery.repository.RecoveryRepository;
67
import com.github.throyer.common.springboot.domain.user.repository.UserRepository;
7-
import com.github.throyer.common.springboot.domain.mail.service.MailService;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.stereotype.Service;
1010

11+
import java.util.logging.Logger;
12+
13+
import static com.github.throyer.common.springboot.utils.Constants.MAIL.*;
14+
import static java.lang.String.format;
15+
import static java.util.logging.Level.INFO;
16+
import static java.util.logging.Level.WARNING;
17+
1118
@Service
1219
public class RecoveryService {
13-
14-
private static final Integer MINUTES_TO_EXPIRE = 20;
1520

16-
@Autowired
17-
private UserRepository users;
21+
private static final Logger LOGGER = Logger.getLogger(RecoveryService.class.getName());
1822

19-
@Autowired
20-
private RecoveryRepository recoveries;
23+
private final UserRepository users;
24+
private final RecoveryRepository recoveries;
25+
private final MailService service;
2126

2227
@Autowired
23-
private MailService service;
24-
28+
public RecoveryService(
29+
UserRepository users,
30+
RecoveryRepository recoveries,
31+
MailService service
32+
) {
33+
this.users = users;
34+
this.recoveries = recoveries;
35+
this.service = service;
36+
}
37+
2538
public void recovery(String email) {
39+
2640
var user = users.findOptionalByEmail(email);
2741

2842
if (user.isEmpty()) {
2943
return;
3044
}
3145

32-
var recovery = new Recovery(user.get(), MINUTES_TO_EXPIRE);
46+
var recovery = new Recovery(user.get(), MINUTES_TO_EXPIRE_RECOVERY_CODE);
3347

3448
recoveries.save(recovery);
3549

36-
try {
37-
var recoveryEmail = new RecoveryEmail(
38-
email,
39-
"password recovery code",
40-
user.get().getName(),
41-
recovery.getCode()
42-
);
43-
44-
service.send(recoveryEmail);
45-
} catch (Exception exception) { }
50+
var sendEmailBackground = new Thread(() -> {
51+
try {
52+
var recoveryEmail = new RecoveryEmail(
53+
email,
54+
SUBJECT_PASSWORD_RECOVERY_CODE,
55+
user.get().getName(),
56+
recovery.getCode()
57+
);
58+
59+
service.send(recoveryEmail);
60+
61+
LOGGER.log(INFO, format(EMAIL_SENT_SUCCESSFULLY_MESSAGE_LOG_TEMPLATE, email));
62+
} catch (Exception exception) {
63+
LOGGER.log(WARNING, format(UNABLE_TO_SEND_EMAIL_MESSAGE_TEMPLATE, email), exception);
64+
}
65+
});
66+
67+
sendEmailBackground.start();
4668
}
4769
}

src/main/java/com/github/throyer/common/springboot/domain/session/entity/RefreshToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public RefreshToken(User user, Integer daysToExpire) {
4848
public RefreshToken(UserDetails user, Integer daysToExpire) {
4949
this.expiresIn = now().plusDays(daysToExpire);
5050
this.code = randomUUID().toString();
51-
this.user = Optional.ofNullable(user.getId()).map(userId -> new User(userId)).orElse(null);
51+
this.user = Optional.ofNullable(user.getId()).map(User::new).orElse(null);
5252
}
5353

5454
public Boolean nonExpired() {

src/main/java/com/github/throyer/common/springboot/domain/session/model/Authorized.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.github.throyer.common.springboot.domain.session.model;
22

3-
import java.util.List;
4-
53
import com.github.throyer.common.springboot.domain.role.entity.Role;
6-
74
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
85
import org.springframework.security.core.userdetails.User;
96

7+
import java.io.Serial;
8+
import java.util.List;
9+
1010
public class Authorized extends User {
1111

12+
@Serial
1213
private static final long serialVersionUID = 1L;
1314

1415
private final Long id;

0 commit comments

Comments
 (0)