Skip to content

Commit 7641b10

Browse files
chibenwaArsnael
authored andcommitted
JAMES-4164 Refactor VacationMailet
- Use record instead of unnamed pair - Avoid double computation of RecipientId, AccountId - Undo unneeded extract, but re-extract relevant portion of the code
1 parent 20af073 commit 7641b10

3 files changed

Lines changed: 39 additions & 33 deletions

File tree

server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/VacationMailet.java

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import jakarta.mail.internet.AddressException;
3131
import jakarta.mail.internet.InternetAddress;
3232

33-
import org.apache.commons.lang3.tuple.Pair;
3433
import org.apache.james.core.MailAddress;
3534
import org.apache.james.mime4j.dom.address.Mailbox;
3635
import org.apache.james.mime4j.field.address.LenientAddressParser;
@@ -131,26 +130,24 @@ private void manageVacation(MailAddress recipient, Mail processedMail, ZonedDate
131130
return;
132131
}
133132

134-
AccountId accountId = AccountId.fromString(recipient.toString());
135-
136-
Mono<Vacation> vacation = vacationService.retrieveVacation(accountId);
137-
Mono<Boolean> alreadySent = vacationService.isNotificationRegistered(
138-
AccountId.fromString(recipient.toString()),
139-
RecipientId.fromMailAddress(processedMail.getMaybeSender().get()));
140-
Pair<Vacation, Boolean> pair = Flux.combineLatest(vacation, alreadySent, Pair::of)
141-
.blockFirst();
133+
RecipientId replyRecipient = RecipientId.fromMailAddress(processedMail.getMaybeSender().get());
134+
VacationInformation vacationInformation = retrieveVacationInformation(recipient, replyRecipient);
142135

143-
sendNotificationIfRequired(recipient, processedMail, processingDate, pair.getKey(), pair.getValue());
136+
boolean shouldSendNotification = vacationInformation.vacation.isActiveAtDate(processingDate) && !vacationInformation.alreadySent;
137+
if (shouldSendNotification) {
138+
sendNotification(processedMail, vacationInformation);
139+
}
144140
}
145141

146-
private void sendNotificationIfRequired(MailAddress recipient, Mail processedMail, ZonedDateTime processingDate, Vacation vacation, Boolean alreadySent) {
147-
if (shouldSendNotification(vacation, processingDate, alreadySent)) {
148-
sendNotification(recipient, processedMail, vacation);
149-
}
142+
record VacationInformation(Vacation vacation, MailAddress recipient, AccountId accountId, RecipientId replyRecipient, Boolean alreadySent) {
143+
150144
}
151145

152-
private boolean shouldSendNotification(Vacation vacation, ZonedDateTime processingDate, boolean alreadySent) {
153-
return vacation.isActiveAtDate(processingDate) && !alreadySent;
146+
private VacationInformation retrieveVacationInformation(MailAddress recipient, RecipientId replyRecipient) {
147+
AccountId accountId = AccountId.fromString(recipient.toString());
148+
Mono<Vacation> vacation = vacationService.retrieveVacation(accountId);
149+
Mono<Boolean> alreadySent = vacationService.isNotificationRegistered(accountId, replyRecipient);
150+
return Flux.combineLatest(vacation, alreadySent, (a, b) -> new VacationInformation(a, recipient, accountId, replyRecipient, b)).blockFirst();
154151
}
155152

156153
private boolean isNoReplySender(Mail processedMail) {
@@ -168,30 +165,27 @@ private boolean isSentToSelf(Optional<MailAddress> maybeSender, MailAddress reci
168165
.orElse(false);
169166
}
170167

171-
private void sendNotification(MailAddress recipient, Mail processedMail, Vacation vacation) {
168+
private void sendNotification(Mail processedMail, VacationInformation vacationInformation) {
172169
try {
173170
VacationReply vacationReply = VacationReply.builder(processedMail)
174-
.receivedMailRecipient(recipient)
175-
.vacation(vacation)
171+
.replyRecipient(vacationInformation.replyRecipient.getMailAddress())
172+
.receivedMailRecipient(vacationInformation.recipient())
173+
.vacation(vacationInformation.vacation)
176174
.build(mimeMessageBodyGenerator);
177175

178-
sendNotification(vacationReply, recipient);
176+
getMailetContext().sendMail(getSender(vacationInformation.recipient),
177+
vacationReply.getRecipients(),
178+
vacationReply.getMimeMessage());
179179

180-
vacationService.registerNotification(AccountId.fromString(recipient.toString()),
181-
RecipientId.fromMailAddress(processedMail.getMaybeSender().get()),
182-
vacation.getToDate())
180+
vacationService.registerNotification(vacationInformation.accountId(),
181+
vacationInformation.replyRecipient(),
182+
vacationInformation.vacation().getToDate())
183183
.block();
184184
} catch (MessagingException e) {
185-
LOGGER.warn("Failed to send JMAP vacation notification from {} to {}", recipient, processedMail.getMaybeSender(), e);
185+
LOGGER.warn("Failed to send JMAP vacation notification from {} to {}", vacationInformation.recipient(), vacationInformation.replyRecipient().getAsString(), e);
186186
}
187187
}
188188

189-
private void sendNotification(VacationReply vacationReply, MailAddress recipient) throws MessagingException {
190-
getMailetContext().sendMail(getSender(recipient),
191-
vacationReply.getRecipients(),
192-
vacationReply.getMimeMessage());
193-
}
194-
195189
private MailAddress getSender(MailAddress recipient) {
196190
if (!useUserAsMailFrom) {
197191
return MailAddress.nullSender();

server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/VacationReply.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.github.fge.lambdas.Throwing;
3434
import com.google.common.base.Preconditions;
35+
import com.google.common.collect.ImmutableList;
3536

3637
public class VacationReply {
3738

@@ -47,6 +48,7 @@ public static class Builder {
4748
public static final boolean NOT_REPLY_TO_ALL = false;
4849
private final Mail originalMail;
4950
private MailAddress mailRecipient;
51+
private MailAddress replyRecipient;
5052
private Vacation vacation;
5153

5254
private Builder(Mail originalMail) {
@@ -65,18 +67,24 @@ public Builder vacation(Vacation vacation) {
6567
return this;
6668
}
6769

70+
public Builder replyRecipient(MailAddress replyRecipient) {
71+
this.replyRecipient = replyRecipient;
72+
return this;
73+
}
74+
6875
public VacationReply build(MimeMessageBodyGenerator mimeMessageBodyGenerator) throws MessagingException {
6976
Preconditions.checkState(mailRecipient != null, "Original recipient address should not be null");
77+
Preconditions.checkState(replyRecipient != null, "Reply recipient address should not be null");
7078
Preconditions.checkState(originalMail.hasSender(), "Original sender address should not be null");
7179

72-
return new VacationReply(mailRecipient, originalMail.getMaybeSender().asList(), generateMimeMessage(mimeMessageBodyGenerator));
80+
return new VacationReply(mailRecipient, ImmutableList.of(replyRecipient), generateMimeMessage(mimeMessageBodyGenerator));
7381
}
7482

7583
private MimeMessage generateMimeMessage(MimeMessageBodyGenerator mimeMessageBodyGenerator) throws MessagingException {
7684
MimeMessage reply = (MimeMessage) originalMail.getMessage().reply(NOT_REPLY_TO_ALL);
77-
vacation.getSubject().ifPresent(Throwing.consumer(subjectString -> reply.setSubject(subjectString)));
85+
vacation.getSubject().ifPresent(Throwing.consumer(reply::setSubject));
7886
reply.setHeader(FROM_HEADER, mailRecipient.toString());
79-
reply.setHeader(TO_HEADER, originalMail.getMaybeSender().get().asString());
87+
reply.setHeader(TO_HEADER, replyRecipient.asString());
8088
reply.setHeader(AutomaticallySentMailDetector.AUTO_SUBMITTED_HEADER, AutomaticallySentMailDetector.AUTO_REPLIED_VALUE);
8189

8290
return mimeMessageBodyGenerator.from(reply, vacation.getTextBody(), vacation.getHtmlBody());

server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/VacationReplyTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void vacationReplyShouldGenerateASuitableAnswer() throws Exception {
8282
.htmlBody(HTML_REASON)
8383
.build())
8484
.receivedMailRecipient(originalRecipient)
85+
.replyRecipient(originalSender)
8586
.build(mimeMessageBodyGenerator);
8687

8788
assertThat(vacationReply.getRecipients()).containsExactly(originalSender);
@@ -96,6 +97,7 @@ public void vacationReplyShouldAddReSuffixToSubjectByDefault() throws Exception
9697
.textBody(REASON)
9798
.build())
9899
.receivedMailRecipient(originalRecipient)
100+
.replyRecipient(originalSender)
99101
.build(mimeMessageBodyGenerator);
100102

101103
verify(mimeMessageBodyGenerator).from(argThat(createSubjectMatcher("Re: Original subject")), any(), any());
@@ -111,6 +113,7 @@ public void subjectShouldBeQEncodedWhenSpecialCharacters() throws Exception {
111113
.subject(Optional.of("Nghiêm Thị Tuyết Nhung"))
112114
.textBody(REASON)
113115
.build())
116+
.replyRecipient(originalRecipient)
114117
.receivedMailRecipient(originalRecipient)
115118
.build(mimeMessageBodyGenerator);
116119

@@ -126,6 +129,7 @@ public void aUserShouldBeAbleToSetTheSubjectOfTheGeneratedMimeMessage() throws E
126129
.subject(Optional.of(SUBJECT))
127130
.build())
128131
.receivedMailRecipient(originalRecipient)
132+
.replyRecipient(originalSender)
129133
.build(mimeMessageBodyGenerator);
130134

131135
verify(mimeMessageBodyGenerator).from(argThat(createSubjectMatcher(SUBJECT)), any(), any());

0 commit comments

Comments
 (0)