-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailParameter.java
More file actions
105 lines (90 loc) · 3.02 KB
/
Copy pathEmailParameter.java
File metadata and controls
105 lines (90 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package it.aboutbits.springboot.emailservice.lib.application;
import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import lombok.Builder;
import lombok.Singular;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.io.InputStream;
import java.time.OffsetDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
@Builder
@NullMarked
public record EmailParameter(
OffsetDateTime scheduledAt,
@Valid
Email email
) {
@Builder
public record Email(
@NotBlank
String subject,
@Singular
@NotEmpty
List<String> recipients,
String textBody,
String htmlBody,
@NotBlank
String fromAddress,
@NotBlank
String fromName,
@Nullable
String replyToAddress,
@Nullable
String replyToName,
@Singular
@Valid
Set<Attachment> attachments
) {
// the "cid:" scheme is case-insensitive, the contentId itself is not
private static final Pattern CID_REFERENCE_PATTERN = Pattern.compile(
"cid:([^\\s\"'<>]+)",
Pattern.CASE_INSENSITIVE
);
@AssertTrue(message = "each inline attachment contentId must be referenced in the htmlBody as cid:contentId")
public boolean isInlineAttachmentsValid() {
var contentIds = inlineContentIds();
if (contentIds.isEmpty()) {
return true;
}
if (htmlBody == null) {
return false;
}
var referencedContentIds = new HashSet<String>();
var matcher = CID_REFERENCE_PATTERN.matcher(htmlBody);
while (matcher.find()) {
referencedContentIds.add(matcher.group(1));
}
return referencedContentIds.containsAll(contentIds);
}
@AssertTrue(message = "each inline attachment contentId must be unique")
public boolean isInlineAttachmentContentIdsUnique() {
var contentIds = inlineContentIds();
return contentIds.size() == new HashSet<>(contentIds).size();
}
private List<String> inlineContentIds() {
return attachments.stream()
.map(Attachment::contentId)
.filter(Objects::nonNull)
.toList();
}
@Builder
public record Attachment(
InputStream payload,
@NotBlank
String fileName,
@NotBlank
String contentType,
// if set, the attachment is embedded inline and can be referenced in the htmlBody as "cid:contentId"
@Nullable
String contentId
) {
}
}
}