Skip to content

Commit

Permalink
#1296 started MailJet notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Oct 29, 2022
1 parent 817c479 commit 0b7ba13
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 5 deletions.
29 changes: 29 additions & 0 deletions self-api/src/main/java/com/selfxdsd/api/EmailNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 0.0.99
* @checkstyle MethodName (100 lines)
*/
public interface EmailNotification {

Expand All @@ -36,6 +37,28 @@ public interface EmailNotification {
*/
String to();

/**
* Name of the receiver.
* @return String.
*/
String toName();

/**
* Sender e-mail address.
* @return String.
*/
default String from() {
return "[email protected]";
}

/**
* Sender name.
* @return String.
*/
default String fromName() {
return "Self XDSD";
}

/**
* Subect text.
* @return String.
Expand All @@ -47,4 +70,10 @@ public interface EmailNotification {
* @return String.
*/
String body();

/**
* Type of the notification.
* @return String.
*/
String type();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface EmailNotifications {

/**
* Send a notification.
* @param emailNotification - Notification to send.
* @param emailNotification Notification to send.
*/
void send(final EmailNotification emailNotification);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,42 @@

import com.selfxdsd.api.EmailNotification;
import com.selfxdsd.api.EmailNotifications;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.json.Json;
import javax.json.JsonObject;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;

/**
* Using MailJet to send e-mail notifications.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 0.0.99
* @todo #1296:60min Write unit tests for this class, as well as for class
* PlatformInvoiceEmailnotification.
* @todo #1296:60min Use this class to send e-mails as soon as a real payment
* is successful. We should do it in a Wallet decorator.
*/
public final class MailjetEmailNotifications implements EmailNotifications {

/**
* Logger.
*/
private static final Logger LOG = LoggerFactory.getLogger(
MailjetEmailNotifications.class
);

/**
* MailJet Mail-Sending endpoint.
*/
private static final String MAILJET = "https://api.mailjet.com/v3.1/send";

/**
* API Public Key (username).
*/
Expand All @@ -43,6 +70,11 @@ public final class MailjetEmailNotifications implements EmailNotifications {
*/
private final String apiSecretKey;

/**
* HTTP Version to use.
*/
private final HttpClient.Version httpVersion;

/**
* Ctor.
* @param apiKey MailJet's API Key.
Expand All @@ -51,13 +83,130 @@ public final class MailjetEmailNotifications implements EmailNotifications {
public MailjetEmailNotifications(
final String apiKey,
final String apiSecretKey
) {
this(apiKey, apiSecretKey, HttpClient.Version.HTTP_2);
}

/**
* Ctor.
* @param apiKey MailJet's API Key.
* @param apiSecretKey MailJet's API Secret Key.
* @param httpVersion HTTP Version to use.
*/
MailjetEmailNotifications(
final String apiKey,
final String apiSecretKey,
final HttpClient.Version httpVersion
) {
this.apiKey = apiKey;
this.apiSecretKey = apiSecretKey;
this.httpVersion = httpVersion;
}

@Override
public void send(final EmailNotification emailNotification) {
try {
final JsonObject messages = Json.createObjectBuilder()
.add(
"Messages",
Json.createArrayBuilder()
.add(this.notificationToJsonMessage(emailNotification))
.build()
).build();
LOG.debug(
"Sending EmailNotification [" + emailNotification.type()
+ "] to " + emailNotification.to() + "... "
);
final HttpResponse<String> response = GlobalHttpClient.instance(
this.httpVersion
).send(
this.request(
URI.create(MAILJET),
"POST",
HttpRequest.BodyPublishers.ofString(messages.toString())
),
HttpResponse.BodyHandlers.ofString()
);
if(response.statusCode() == 200) {
LOG.debug("Notification sent successfully!");
} else {
LOG.error(
"Failed sending " + emailNotification.type()
+ " notification! Status: "
+ response.statusCode()
);
LOG.error("MailJet Response: " + response.body());
LOG.error("MailJet Payload: " + messages);
}
} catch (final IOException | InterruptedException ex) {
LOG.error(
"Caught exception when sending " + emailNotification.type()
+ " to " + emailNotification.toName(), ex
);
}
}

/**
* Build and return the HTTP Request.
* @param uri URI.
* @param method Method.
* @param body Body.
* @return HttpRequest.
* @checkstyle LineLength (100 lines)
*/
private HttpRequest request(
final URI uri,
final String method,
final HttpRequest.BodyPublisher body
) {
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.uri(uri)
.method(method, body)
.header("Content-Type", "application/json")
.header(
"User-Agent",
"Self XDSD; https://self-xdsd.com; "
+ "https://github.com/self-xdsd"
)
.header(
"Authorization",
"Basic " + Base64.getEncoder().encodeToString(
(this.apiKey + ":" + this.apiSecretKey).getBytes()
)
);
return requestBuilder.build();
}

/**
* Turn an EmailNotification to a MailJet Message JSON.
* @param emailNotification EmailNotification.
* @return JsonObject.
*/
private JsonObject notificationToJsonMessage(
final EmailNotification emailNotification
) {
return Json.createObjectBuilder()
.add(
"From",
Json.createObjectBuilder()
.add("Email", emailNotification.from())
.add("Name", emailNotification.fromName())
.build()
).add(
"To",
Json.createArrayBuilder().add(
Json.createObjectBuilder()
.add("Email", emailNotification.to())
.add("Name", emailNotification.toName())
.build()
)
).add(
"Subject",
emailNotification.subject()
).add(
"TextPart",
emailNotification.body()
).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@
*/
final class PlatformInvoiceEmailNotification implements EmailNotification {

/**
* PlatformInvoice for which we send the notification.
*/
private final PlatformInvoice platformInvoice;

/**
* Ctor.
* @param platformInvoice PlatformInvoice in the notification.
*/
PlatformInvoiceEmailNotification(final PlatformInvoice platformInvoice) {
this.platformInvoice = platformInvoice;
}
Expand All @@ -22,15 +29,25 @@ public String to() {
return "[email protected]";
}

@Override
public String toName() {
return "Self XDSD Admin";
}

@Override
public String subject() {
return "[Self XDSD] New Platform Invoice " +
"(ID: " + this.platformInvoice.id() + ")";
return "New Platform Invoice "
+ "(ID: " + this.platformInvoice.id() + ")";
}

@Override
public String body() {
return "New platform invoice (id is " + this.platformInvoice.id() +
") registered at " + this.platformInvoice.createdAt();
return "New platform invoice (id is " + this.platformInvoice.id()
+ ") registered at " + this.platformInvoice.createdAt() + ".";
}

@Override
public String type() {
return "PlatformInvoide Mail Notification";
}
}

0 comments on commit 0b7ba13

Please sign in to comment.