|
| 1 | +package io.rocketbase.mail.dto; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import com.fasterxml.jackson.annotation.JsonSetter; |
| 6 | +import lombok.*; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +@Data |
| 17 | +@AllArgsConstructor |
| 18 | +@NoArgsConstructor |
| 19 | +@Builder |
| 20 | +public class MessageWithTemplate { |
| 21 | + |
| 22 | + private Long templateId; |
| 23 | + |
| 24 | + private String templateAlias; |
| 25 | + |
| 26 | + private Object templateModel; |
| 27 | + |
| 28 | + private boolean inlineCss; |
| 29 | + |
| 30 | + private String from; |
| 31 | + |
| 32 | + private String to; |
| 33 | + |
| 34 | + private String cc; |
| 35 | + |
| 36 | + private String bcc; |
| 37 | + |
| 38 | + private String replyTo; |
| 39 | + |
| 40 | + private String tag; |
| 41 | + |
| 42 | + private List<Header> headers; |
| 43 | + |
| 44 | + private Boolean trackOpens; |
| 45 | + |
| 46 | + private TrackLinksType trackLinks; |
| 47 | + |
| 48 | + private Map<String, String> metadata; |
| 49 | + |
| 50 | + |
| 51 | + public MessageWithTemplate(String from, String to, Long templateId, Object templateModel) { |
| 52 | + setFrom(from); |
| 53 | + setTo(to); |
| 54 | + this.templateId = templateId; |
| 55 | + this.templateModel = templateModel; |
| 56 | + } |
| 57 | + |
| 58 | + public MessageWithTemplate(EmailAddress from, EmailAddress to, String templateAlias, Object templateModel) { |
| 59 | + setFrom(from); |
| 60 | + setTo(to); |
| 61 | + this.templateAlias = templateAlias; |
| 62 | + this.templateModel = templateModel; |
| 63 | + } |
| 64 | + |
| 65 | + @JsonCreator |
| 66 | + public MessageWithTemplate(@JsonProperty("from") String from, |
| 67 | + @JsonProperty("to") String to, |
| 68 | + @JsonProperty("templateId") Long templateId, |
| 69 | + @JsonProperty("templateAlias") String templateAlias, |
| 70 | + @JsonProperty("cc") String cc, |
| 71 | + @JsonProperty("bcc") String bcc) { |
| 72 | + setFrom(from); |
| 73 | + setTo(to); |
| 74 | + this.templateId = templateId; |
| 75 | + this.templateAlias = templateAlias; |
| 76 | + setCc(cc); |
| 77 | + setBcc(bcc); |
| 78 | + } |
| 79 | + |
| 80 | + public void setFrom(String from) { |
| 81 | + this.from = from; |
| 82 | + } |
| 83 | + |
| 84 | + public void setFrom(EmailAddress from) { |
| 85 | + this.from = from != null ? from.toRecipient() : null; |
| 86 | + } |
| 87 | + |
| 88 | + public void setTo(EmailAddress to) { |
| 89 | + this.to = to != null ? to.toRecipient() : null; |
| 90 | + } |
| 91 | + |
| 92 | + public void setTo(List<EmailAddress> to) { |
| 93 | + this.to = emailAddressList(to); |
| 94 | + } |
| 95 | + |
| 96 | + public void setTo(String... to) { |
| 97 | + this.to = emailList(to); |
| 98 | + } |
| 99 | + |
| 100 | + @JsonSetter |
| 101 | + public void setTo(String to) { |
| 102 | + this.to = to; |
| 103 | + } |
| 104 | + |
| 105 | + public void setCc(EmailAddress cc) { |
| 106 | + this.cc = cc != null ? cc.toRecipient() : null; |
| 107 | + } |
| 108 | + |
| 109 | + public void setCc(List<EmailAddress> cc) { |
| 110 | + this.cc = emailAddressList(cc); |
| 111 | + } |
| 112 | + |
| 113 | + public void setCc(String... cc) { |
| 114 | + this.cc = emailList(cc); |
| 115 | + } |
| 116 | + |
| 117 | + @JsonSetter |
| 118 | + public void setCc(String cc) { |
| 119 | + this.cc = cc; |
| 120 | + } |
| 121 | + |
| 122 | + public void setBcc(EmailAddress bcc) { |
| 123 | + this.bcc = bcc != null ? bcc.toRecipient() : null; |
| 124 | + } |
| 125 | + |
| 126 | + public void setBcc(List<EmailAddress> bcc) { |
| 127 | + this.bcc = emailAddressList(bcc); |
| 128 | + } |
| 129 | + |
| 130 | + public void setBcc(EmailAddress... bcc) { |
| 131 | + this.bcc = emailList(bcc); |
| 132 | + } |
| 133 | + |
| 134 | + public void setBcc(String... bcc) { |
| 135 | + this.bcc = emailList(bcc); |
| 136 | + } |
| 137 | + |
| 138 | + @JsonSetter |
| 139 | + public void setBcc(String bcc) { |
| 140 | + this.bcc = bcc; |
| 141 | + } |
| 142 | + |
| 143 | + @SneakyThrows |
| 144 | + protected byte[] readFileContent(String path) { |
| 145 | + return Files.readAllBytes(Paths.get(path)); |
| 146 | + } |
| 147 | + |
| 148 | + @SneakyThrows |
| 149 | + protected String readFileContentType(String path) { |
| 150 | + return Files.probeContentType(new File(path).toPath()); |
| 151 | + } |
| 152 | + |
| 153 | + public void setTemplateModel(Object templateModel) { |
| 154 | + this.templateModel = templateModel; |
| 155 | + } |
| 156 | + |
| 157 | + public void setInlineCss(boolean inlineCss) { |
| 158 | + this.inlineCss = inlineCss; |
| 159 | + } |
| 160 | + |
| 161 | + protected String emailAddressList(List<EmailAddress> addresses) { |
| 162 | + if (addresses == null || addresses.isEmpty()) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + return addresses.stream() |
| 166 | + .map(EmailAddress::toRecipient) |
| 167 | + .collect(Collectors.joining(",")); |
| 168 | + } |
| 169 | + |
| 170 | + protected String emailList(String... addresses) { |
| 171 | + if (addresses == null || addresses.length == 0) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + return Arrays.asList(addresses) |
| 175 | + .stream() |
| 176 | + .map(e -> new EmailAddress(e).toRecipient()) |
| 177 | + .collect(Collectors.joining(",")); |
| 178 | + } |
| 179 | + |
| 180 | + protected String emailList(EmailAddress... addresses) { |
| 181 | + if (addresses == null || addresses.length == 0) { |
| 182 | + return null; |
| 183 | + } |
| 184 | + return Arrays.asList(addresses) |
| 185 | + .stream() |
| 186 | + .map(EmailAddress::toRecipient) |
| 187 | + .collect(Collectors.joining(",")); |
| 188 | + } |
| 189 | +} |
0 commit comments