Skip to content

Commit

Permalink
feat: upgraded versions + added template support
Browse files Browse the repository at this point in the history
  • Loading branch information
melistik committed Jul 24, 2023
1 parent 80d0bec commit 86bba50
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-to-maven-central.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -77,11 +77,11 @@ SOFTWARE.

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.javadoc.failOnError>false</maven.javadoc.failOnError>

<spring-boot.version>2.7.9</spring-boot.version>
<spring-boot.version>3.1.2</spring-boot.version>
</properties>


Expand Down Expand Up @@ -130,12 +130,12 @@ SOFTWARE.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M9</version>
<version>3.1.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -148,7 +148,7 @@ SOFTWARE.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down Expand Up @@ -184,7 +184,7 @@ SOFTWARE.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -197,7 +197,7 @@ SOFTWARE.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -213,7 +213,7 @@ SOFTWARE.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/rocketbase/mail/PostmarkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,15 @@ public List<MessageResponse> deliverMessage(List<Message> messages) {
return response.getBody();
}

public MessageResponse deliverMessageWithTemplate(MessageWithTemplate msg) {
ResponseEntity<MessageResponse> 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());
}
Expand Down
189 changes: 189 additions & 0 deletions src/main/java/io/rocketbase/mail/dto/MessageWithTemplate.java
Original file line number Diff line number Diff line change
@@ -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<Header> headers;

private Boolean trackOpens;

private TrackLinksType trackLinks;

private Map<String, String> 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<EmailAddress> 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<EmailAddress> 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<EmailAddress> 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<EmailAddress> 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(","));
}
}
26 changes: 25 additions & 1 deletion src/test/java/io/rocketbase/mail/PostmarkClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("[email protected]"),
new EmailAddress("[email protected]", "Marten Prieß"), "application-error", templateObject);

// when
MessageResponse response = client.deliverMessageWithTemplate(message);
// then
assertThat(response, notNullValue());
}

@Test
public void testBuildJson() throws Exception {
// given
Expand Down

0 comments on commit 86bba50

Please sign in to comment.