Skip to content

Commit 86bba50

Browse files
committed
feat: upgraded versions + added template support
1 parent 80d0bec commit 86bba50

File tree

6 files changed

+236
-13
lines changed

6 files changed

+236
-13
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
runs-on: ubuntu-latest
66
strategy:
77
matrix:
8-
java: [ '8', '11', '17', '19' ]
8+
java: [ '17', '19' ]
99
name: Temurin ${{ matrix.java }}
1010
steps:
1111
- uses: actions/checkout@v3

.github/workflows/release-to-maven-central.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Set up Maven Central Repository
2020
uses: actions/setup-java@v1
2121
with:
22-
java-version: 8
22+
java-version: 17
2323
server-id: ossrh
2424
server-username: MAVEN_USERNAME
2525
server-password: MAVEN_PASSWORD

pom.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
SOFTWARE.
2424
-->
2525
<project
26-
xmlns="http://maven.apache.org/POM/4.0.0"
2726
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xmlns="http://maven.apache.org/POM/4.0.0"
2828
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2929
<modelVersion>4.0.0</modelVersion>
3030

@@ -77,11 +77,11 @@ SOFTWARE.
7777

7878
<properties>
7979
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
80-
<maven.compiler.source>1.8</maven.compiler.source>
81-
<maven.compiler.target>1.8</maven.compiler.target>
80+
<maven.compiler.source>17</maven.compiler.source>
81+
<maven.compiler.target>17</maven.compiler.target>
8282
<maven.javadoc.failOnError>false</maven.javadoc.failOnError>
8383

84-
<spring-boot.version>2.7.9</spring-boot.version>
84+
<spring-boot.version>3.1.2</spring-boot.version>
8585
</properties>
8686

8787

@@ -130,12 +130,12 @@ SOFTWARE.
130130
<plugin>
131131
<groupId>org.apache.maven.plugins</groupId>
132132
<artifactId>maven-surefire-plugin</artifactId>
133-
<version>3.0.0-M9</version>
133+
<version>3.1.2</version>
134134
</plugin>
135135
<plugin>
136136
<groupId>org.apache.maven.plugins</groupId>
137137
<artifactId>maven-source-plugin</artifactId>
138-
<version>3.2.1</version>
138+
<version>3.3.0</version>
139139
<executions>
140140
<execution>
141141
<id>attach-sources</id>
@@ -148,7 +148,7 @@ SOFTWARE.
148148
<plugin>
149149
<groupId>org.apache.maven.plugins</groupId>
150150
<artifactId>maven-javadoc-plugin</artifactId>
151-
<version>3.4.1</version>
151+
<version>3.5.0</version>
152152
<executions>
153153
<execution>
154154
<id>attach-javadocs</id>
@@ -184,7 +184,7 @@ SOFTWARE.
184184
<plugin>
185185
<groupId>org.apache.maven.plugins</groupId>
186186
<artifactId>maven-source-plugin</artifactId>
187-
<version>3.2.1</version>
187+
<version>3.3.0</version>
188188
<executions>
189189
<execution>
190190
<id>attach-sources</id>
@@ -197,7 +197,7 @@ SOFTWARE.
197197
<plugin>
198198
<groupId>org.apache.maven.plugins</groupId>
199199
<artifactId>maven-javadoc-plugin</artifactId>
200-
<version>3.4.1</version>
200+
<version>3.5.0</version>
201201
<executions>
202202
<execution>
203203
<id>attach-javadocs</id>
@@ -213,7 +213,7 @@ SOFTWARE.
213213
<plugin>
214214
<groupId>org.apache.maven.plugins</groupId>
215215
<artifactId>maven-gpg-plugin</artifactId>
216-
<version>3.0.1</version>
216+
<version>3.1.0</version>
217217
<executions>
218218
<execution>
219219
<id>sign-artifacts</id>

src/main/java/io/rocketbase/mail/PostmarkClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.rocketbase.mail.dto.EmailAttachment;
55
import io.rocketbase.mail.dto.Message;
66
import io.rocketbase.mail.dto.MessageResponse;
7+
import io.rocketbase.mail.dto.MessageWithTemplate;
78
import io.rocketbase.mail.util.MessageJsonWriter;
89
import lombok.RequiredArgsConstructor;
910
import org.springframework.core.ParameterizedTypeReference;
@@ -69,6 +70,15 @@ public List<MessageResponse> deliverMessage(List<Message> messages) {
6970
return response.getBody();
7071
}
7172

73+
public MessageResponse deliverMessageWithTemplate(MessageWithTemplate msg) {
74+
ResponseEntity<MessageResponse> response = getRestTemplate().exchange(createUriBuilder().path("/email/withTemplate")
75+
.toUriString(),
76+
HttpMethod.POST,
77+
new HttpEntity<>(msg, buildHeaders()),
78+
MessageResponse.class);
79+
return response.getBody();
80+
}
81+
7282
protected UriComponentsBuilder createUriBuilder() {
7383
return UriComponentsBuilder.fromUriString(postmarkProperties.getApi().getUrl());
7484
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
}

src/test/java/io/rocketbase/mail/PostmarkClientTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import org.junit.jupiter.api.Disabled;
99
import org.junit.jupiter.api.Test;
1010

11-
1211
import java.io.File;
1312
import java.net.URL;
13+
import java.util.HashMap;
14+
import java.util.Map;
1415

1516
import static org.hamcrest.MatcherAssert.assertThat;
1617
import static org.hamcrest.Matchers.equalTo;
@@ -38,6 +39,29 @@ public void testDeliverMessage() {
3839
assertThat(response, notNullValue());
3940
}
4041

42+
43+
@Disabled
44+
@Test
45+
public void testDeliverMessageWithTemplate() {
46+
// given
47+
Map templateObject = new HashMap();
48+
templateObject.put("product_name", "RocketBase");
49+
templateObject.put("product_url", "https://www.rocketbase.io");
50+
templateObject.put("company_name", "RocketBase");
51+
templateObject.put("company_address", "Katharinenstraße 30a, 20457 Hamburg");
52+
templateObject.put("task_name", "task-name");
53+
templateObject.put("message", "Had some issues...\nplease take a look");
54+
55+
PostmarkClient client = new PostmarkClient(new PostmarkProperties("--"));
56+
MessageWithTemplate message = new MessageWithTemplate(new EmailAddress("info@rocketbase.io"),
57+
new EmailAddress("marten@rocketbase.io", "Marten Prieß"), "application-error", templateObject);
58+
59+
// when
60+
MessageResponse response = client.deliverMessageWithTemplate(message);
61+
// then
62+
assertThat(response, notNullValue());
63+
}
64+
4165
@Test
4266
public void testBuildJson() throws Exception {
4367
// given

0 commit comments

Comments
 (0)