diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 93c538a..69bd14b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - java: [ '8', '11', '17', '19' ] + java: [ '17', '19' ] name: Temurin ${{ matrix.java }} steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/release-to-maven-central.yml b/.github/workflows/release-to-maven-central.yml index a635aed..660fa34 100644 --- a/.github/workflows/release-to-maven-central.yml +++ b/.github/workflows/release-to-maven-central.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Maven Central Repository uses: actions/setup-java@v1 with: - java-version: 8 + java-version: 17 server-id: ossrh server-username: MAVEN_USERNAME server-password: MAVEN_PASSWORD diff --git a/pom.xml b/pom.xml index 4576528..6d5aa59 100755 --- a/pom.xml +++ b/pom.xml @@ -23,8 +23,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> 4.0.0 @@ -77,11 +77,11 @@ SOFTWARE. UTF-8 - 1.8 - 1.8 + 17 + 17 false - 2.7.9 + 3.1.2 @@ -130,12 +130,12 @@ SOFTWARE. org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M9 + 3.1.2 org.apache.maven.plugins maven-source-plugin - 3.2.1 + 3.3.0 attach-sources @@ -148,7 +148,7 @@ SOFTWARE. org.apache.maven.plugins maven-javadoc-plugin - 3.4.1 + 3.5.0 attach-javadocs @@ -184,7 +184,7 @@ SOFTWARE. org.apache.maven.plugins maven-source-plugin - 3.2.1 + 3.3.0 attach-sources @@ -197,7 +197,7 @@ SOFTWARE. org.apache.maven.plugins maven-javadoc-plugin - 3.4.1 + 3.5.0 attach-javadocs @@ -213,7 +213,7 @@ SOFTWARE. org.apache.maven.plugins maven-gpg-plugin - 3.0.1 + 3.1.0 sign-artifacts diff --git a/src/main/java/io/rocketbase/mail/PostmarkClient.java b/src/main/java/io/rocketbase/mail/PostmarkClient.java index 349f3e7..11c8ca0 100644 --- a/src/main/java/io/rocketbase/mail/PostmarkClient.java +++ b/src/main/java/io/rocketbase/mail/PostmarkClient.java @@ -4,6 +4,7 @@ import io.rocketbase.mail.dto.EmailAttachment; import io.rocketbase.mail.dto.Message; import io.rocketbase.mail.dto.MessageResponse; +import io.rocketbase.mail.dto.MessageWithTemplate; import io.rocketbase.mail.util.MessageJsonWriter; import lombok.RequiredArgsConstructor; import org.springframework.core.ParameterizedTypeReference; @@ -69,6 +70,15 @@ public List deliverMessage(List messages) { return response.getBody(); } + public MessageResponse deliverMessageWithTemplate(MessageWithTemplate msg) { + ResponseEntity response = getRestTemplate().exchange(createUriBuilder().path("/email/withTemplate") + .toUriString(), + HttpMethod.POST, + new HttpEntity<>(msg, buildHeaders()), + MessageResponse.class); + return response.getBody(); + } + protected UriComponentsBuilder createUriBuilder() { return UriComponentsBuilder.fromUriString(postmarkProperties.getApi().getUrl()); } diff --git a/src/main/java/io/rocketbase/mail/dto/MessageWithTemplate.java b/src/main/java/io/rocketbase/mail/dto/MessageWithTemplate.java new file mode 100644 index 0000000..b8c960a --- /dev/null +++ b/src/main/java/io/rocketbase/mail/dto/MessageWithTemplate.java @@ -0,0 +1,189 @@ +package io.rocketbase.mail.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; +import lombok.*; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class MessageWithTemplate { + + private Long templateId; + + private String templateAlias; + + private Object templateModel; + + private boolean inlineCss; + + private String from; + + private String to; + + private String cc; + + private String bcc; + + private String replyTo; + + private String tag; + + private List
headers; + + private Boolean trackOpens; + + private TrackLinksType trackLinks; + + private Map metadata; + + + public MessageWithTemplate(String from, String to, Long templateId, Object templateModel) { + setFrom(from); + setTo(to); + this.templateId = templateId; + this.templateModel = templateModel; + } + + public MessageWithTemplate(EmailAddress from, EmailAddress to, String templateAlias, Object templateModel) { + setFrom(from); + setTo(to); + this.templateAlias = templateAlias; + this.templateModel = templateModel; + } + + @JsonCreator + public MessageWithTemplate(@JsonProperty("from") String from, + @JsonProperty("to") String to, + @JsonProperty("templateId") Long templateId, + @JsonProperty("templateAlias") String templateAlias, + @JsonProperty("cc") String cc, + @JsonProperty("bcc") String bcc) { + setFrom(from); + setTo(to); + this.templateId = templateId; + this.templateAlias = templateAlias; + setCc(cc); + setBcc(bcc); + } + + public void setFrom(String from) { + this.from = from; + } + + public void setFrom(EmailAddress from) { + this.from = from != null ? from.toRecipient() : null; + } + + public void setTo(EmailAddress to) { + this.to = to != null ? to.toRecipient() : null; + } + + public void setTo(List to) { + this.to = emailAddressList(to); + } + + public void setTo(String... to) { + this.to = emailList(to); + } + + @JsonSetter + public void setTo(String to) { + this.to = to; + } + + public void setCc(EmailAddress cc) { + this.cc = cc != null ? cc.toRecipient() : null; + } + + public void setCc(List cc) { + this.cc = emailAddressList(cc); + } + + public void setCc(String... cc) { + this.cc = emailList(cc); + } + + @JsonSetter + public void setCc(String cc) { + this.cc = cc; + } + + public void setBcc(EmailAddress bcc) { + this.bcc = bcc != null ? bcc.toRecipient() : null; + } + + public void setBcc(List bcc) { + this.bcc = emailAddressList(bcc); + } + + public void setBcc(EmailAddress... bcc) { + this.bcc = emailList(bcc); + } + + public void setBcc(String... bcc) { + this.bcc = emailList(bcc); + } + + @JsonSetter + public void setBcc(String bcc) { + this.bcc = bcc; + } + + @SneakyThrows + protected byte[] readFileContent(String path) { + return Files.readAllBytes(Paths.get(path)); + } + + @SneakyThrows + protected String readFileContentType(String path) { + return Files.probeContentType(new File(path).toPath()); + } + + public void setTemplateModel(Object templateModel) { + this.templateModel = templateModel; + } + + public void setInlineCss(boolean inlineCss) { + this.inlineCss = inlineCss; + } + + protected String emailAddressList(List addresses) { + if (addresses == null || addresses.isEmpty()) { + return null; + } + return addresses.stream() + .map(EmailAddress::toRecipient) + .collect(Collectors.joining(",")); + } + + protected String emailList(String... addresses) { + if (addresses == null || addresses.length == 0) { + return null; + } + return Arrays.asList(addresses) + .stream() + .map(e -> new EmailAddress(e).toRecipient()) + .collect(Collectors.joining(",")); + } + + protected String emailList(EmailAddress... addresses) { + if (addresses == null || addresses.length == 0) { + return null; + } + return Arrays.asList(addresses) + .stream() + .map(EmailAddress::toRecipient) + .collect(Collectors.joining(",")); + } +} diff --git a/src/test/java/io/rocketbase/mail/PostmarkClientTest.java b/src/test/java/io/rocketbase/mail/PostmarkClientTest.java index dd6517b..3906b3f 100644 --- a/src/test/java/io/rocketbase/mail/PostmarkClientTest.java +++ b/src/test/java/io/rocketbase/mail/PostmarkClientTest.java @@ -8,9 +8,10 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; - import java.io.File; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -38,6 +39,29 @@ public void testDeliverMessage() { assertThat(response, notNullValue()); } + + @Disabled + @Test + public void testDeliverMessageWithTemplate() { + // given + Map templateObject = new HashMap(); + templateObject.put("product_name", "RocketBase"); + templateObject.put("product_url", "https://www.rocketbase.io"); + templateObject.put("company_name", "RocketBase"); + templateObject.put("company_address", "Katharinenstraße 30a, 20457 Hamburg"); + templateObject.put("task_name", "task-name"); + templateObject.put("message", "Had some issues...\nplease take a look"); + + PostmarkClient client = new PostmarkClient(new PostmarkProperties("--")); + MessageWithTemplate message = new MessageWithTemplate(new EmailAddress("info@rocketbase.io"), + new EmailAddress("marten@rocketbase.io", "Marten Prieß"), "application-error", templateObject); + + // when + MessageResponse response = client.deliverMessageWithTemplate(message); + // then + assertThat(response, notNullValue()); + } + @Test public void testBuildJson() throws Exception { // given