-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
817c479
commit 0b7ba13
Showing
4 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
* @author Mihai Andronache ([email protected]) | ||
* @version $Id$ | ||
* @since 0.0.99 | ||
* @checkstyle MethodName (100 lines) | ||
*/ | ||
public interface EmailNotification { | ||
|
||
|
@@ -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. | ||
|
@@ -47,4 +70,10 @@ public interface EmailNotification { | |
* @return String. | ||
*/ | ||
String body(); | ||
|
||
/** | ||
* Type of the notification. | ||
* @return String. | ||
*/ | ||
String type(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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). | ||
*/ | ||
|
@@ -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. | ||
|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -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"; | ||
} | ||
} |