Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/ci.yml
#	pom.xml
  • Loading branch information
melistik committed May 23, 2024
2 parents cb19837 + 86bba50 commit ddd1d07
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 1 deletion.
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 ddd1d07

Please sign in to comment.