Skip to content

Commit 0b7ba13

Browse files
committed
#1296 started MailJet notifications
1 parent 817c479 commit 0b7ba13

File tree

4 files changed

+200
-5
lines changed

4 files changed

+200
-5
lines changed

self-api/src/main/java/com/selfxdsd/api/EmailNotification.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @author Mihai Andronache (amihaiemil@gmail.com)
2828
* @version $Id$
2929
* @since 0.0.99
30+
* @checkstyle MethodName (100 lines)
3031
*/
3132
public interface EmailNotification {
3233

@@ -36,6 +37,28 @@ public interface EmailNotification {
3637
*/
3738
String to();
3839

40+
/**
41+
* Name of the receiver.
42+
* @return String.
43+
*/
44+
String toName();
45+
46+
/**
47+
* Sender e-mail address.
48+
* @return String.
49+
*/
50+
default String from() {
51+
return "support@self-xdsd.com";
52+
}
53+
54+
/**
55+
* Sender name.
56+
* @return String.
57+
*/
58+
default String fromName() {
59+
return "Self XDSD";
60+
}
61+
3962
/**
4063
* Subect text.
4164
* @return String.
@@ -47,4 +70,10 @@ public interface EmailNotification {
4770
* @return String.
4871
*/
4972
String body();
73+
74+
/**
75+
* Type of the notification.
76+
* @return String.
77+
*/
78+
String type();
5079
}

self-api/src/main/java/com/selfxdsd/api/EmailNotifications.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface EmailNotifications {
3232

3333
/**
3434
* Send a notification.
35-
* @param emailNotification - Notification to send.
35+
* @param emailNotification Notification to send.
3636
*/
3737
void send(final EmailNotification emailNotification);
3838
}

self-core-impl/src/main/java/com/selfxdsd/core/MailjetEmailNotifications.java

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,42 @@
2424

2525
import com.selfxdsd.api.EmailNotification;
2626
import com.selfxdsd.api.EmailNotifications;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import javax.json.Json;
31+
import javax.json.JsonObject;
32+
import java.io.IOException;
33+
import java.net.URI;
34+
import java.net.http.HttpClient;
35+
import java.net.http.HttpRequest;
36+
import java.net.http.HttpResponse;
37+
import java.util.Base64;
2738

2839
/**
2940
* Using MailJet to send e-mail notifications.
3041
* @author Mihai Andronache (amihaiemil@gmail.com)
3142
* @version $Id$
3243
* @since 0.0.99
44+
* @todo #1296:60min Write unit tests for this class, as well as for class
45+
* PlatformInvoiceEmailnotification.
46+
* @todo #1296:60min Use this class to send e-mails as soon as a real payment
47+
* is successful. We should do it in a Wallet decorator.
3348
*/
3449
public final class MailjetEmailNotifications implements EmailNotifications {
3550

51+
/**
52+
* Logger.
53+
*/
54+
private static final Logger LOG = LoggerFactory.getLogger(
55+
MailjetEmailNotifications.class
56+
);
57+
58+
/**
59+
* MailJet Mail-Sending endpoint.
60+
*/
61+
private static final String MAILJET = "https://api.mailjet.com/v3.1/send";
62+
3663
/**
3764
* API Public Key (username).
3865
*/
@@ -43,6 +70,11 @@ public final class MailjetEmailNotifications implements EmailNotifications {
4370
*/
4471
private final String apiSecretKey;
4572

73+
/**
74+
* HTTP Version to use.
75+
*/
76+
private final HttpClient.Version httpVersion;
77+
4678
/**
4779
* Ctor.
4880
* @param apiKey MailJet's API Key.
@@ -51,13 +83,130 @@ public final class MailjetEmailNotifications implements EmailNotifications {
5183
public MailjetEmailNotifications(
5284
final String apiKey,
5385
final String apiSecretKey
86+
) {
87+
this(apiKey, apiSecretKey, HttpClient.Version.HTTP_2);
88+
}
89+
90+
/**
91+
* Ctor.
92+
* @param apiKey MailJet's API Key.
93+
* @param apiSecretKey MailJet's API Secret Key.
94+
* @param httpVersion HTTP Version to use.
95+
*/
96+
MailjetEmailNotifications(
97+
final String apiKey,
98+
final String apiSecretKey,
99+
final HttpClient.Version httpVersion
54100
) {
55101
this.apiKey = apiKey;
56102
this.apiSecretKey = apiSecretKey;
103+
this.httpVersion = httpVersion;
57104
}
58105

59106
@Override
60107
public void send(final EmailNotification emailNotification) {
108+
try {
109+
final JsonObject messages = Json.createObjectBuilder()
110+
.add(
111+
"Messages",
112+
Json.createArrayBuilder()
113+
.add(this.notificationToJsonMessage(emailNotification))
114+
.build()
115+
).build();
116+
LOG.debug(
117+
"Sending EmailNotification [" + emailNotification.type()
118+
+ "] to " + emailNotification.to() + "... "
119+
);
120+
final HttpResponse<String> response = GlobalHttpClient.instance(
121+
this.httpVersion
122+
).send(
123+
this.request(
124+
URI.create(MAILJET),
125+
"POST",
126+
HttpRequest.BodyPublishers.ofString(messages.toString())
127+
),
128+
HttpResponse.BodyHandlers.ofString()
129+
);
130+
if(response.statusCode() == 200) {
131+
LOG.debug("Notification sent successfully!");
132+
} else {
133+
LOG.error(
134+
"Failed sending " + emailNotification.type()
135+
+ " notification! Status: "
136+
+ response.statusCode()
137+
);
138+
LOG.error("MailJet Response: " + response.body());
139+
LOG.error("MailJet Payload: " + messages);
140+
}
141+
} catch (final IOException | InterruptedException ex) {
142+
LOG.error(
143+
"Caught exception when sending " + emailNotification.type()
144+
+ " to " + emailNotification.toName(), ex
145+
);
146+
}
147+
}
148+
149+
/**
150+
* Build and return the HTTP Request.
151+
* @param uri URI.
152+
* @param method Method.
153+
* @param body Body.
154+
* @return HttpRequest.
155+
* @checkstyle LineLength (100 lines)
156+
*/
157+
private HttpRequest request(
158+
final URI uri,
159+
final String method,
160+
final HttpRequest.BodyPublisher body
161+
) {
162+
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
163+
.uri(uri)
164+
.method(method, body)
165+
.header("Content-Type", "application/json")
166+
.header(
167+
"User-Agent",
168+
"Self XDSD; https://self-xdsd.com; "
169+
+ "https://github.com/self-xdsd"
170+
)
171+
.header(
172+
"Authorization",
173+
"Basic " + Base64.getEncoder().encodeToString(
174+
(this.apiKey + ":" + this.apiSecretKey).getBytes()
175+
)
176+
);
177+
return requestBuilder.build();
178+
}
61179

180+
/**
181+
* Turn an EmailNotification to a MailJet Message JSON.
182+
* @param emailNotification EmailNotification.
183+
* @return JsonObject.
184+
*/
185+
private JsonObject notificationToJsonMessage(
186+
final EmailNotification emailNotification
187+
) {
188+
return Json.createObjectBuilder()
189+
.add(
190+
"From",
191+
Json.createObjectBuilder()
192+
.add("Email", emailNotification.from())
193+
.add("Name", emailNotification.fromName())
194+
.build()
195+
).add(
196+
"To",
197+
Json.createArrayBuilder().add(
198+
Json.createObjectBuilder()
199+
.add("Email", emailNotification.to())
200+
.add("Name", emailNotification.toName())
201+
.build()
202+
)
203+
).add(
204+
"Subject",
205+
emailNotification.subject()
206+
).add(
207+
"TextPart",
208+
emailNotification.body()
209+
).build();
62210
}
211+
63212
}

self-core-impl/src/main/java/com/selfxdsd/core/PlatformInvoiceEmailNotification.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111
*/
1212
final class PlatformInvoiceEmailNotification implements EmailNotification {
1313

14+
/**
15+
* PlatformInvoice for which we send the notification.
16+
*/
1417
private final PlatformInvoice platformInvoice;
1518

19+
/**
20+
* Ctor.
21+
* @param platformInvoice PlatformInvoice in the notification.
22+
*/
1623
PlatformInvoiceEmailNotification(final PlatformInvoice platformInvoice) {
1724
this.platformInvoice = platformInvoice;
1825
}
@@ -22,15 +29,25 @@ public String to() {
2229
return "office@extremelydistributed.com";
2330
}
2431

32+
@Override
33+
public String toName() {
34+
return "Self XDSD Admin";
35+
}
36+
2537
@Override
2638
public String subject() {
27-
return "[Self XDSD] New Platform Invoice " +
28-
"(ID: " + this.platformInvoice.id() + ")";
39+
return "New Platform Invoice "
40+
+ "(ID: " + this.platformInvoice.id() + ")";
2941
}
3042

3143
@Override
3244
public String body() {
33-
return "New platform invoice (id is " + this.platformInvoice.id() +
34-
") registered at " + this.platformInvoice.createdAt();
45+
return "New platform invoice (id is " + this.platformInvoice.id()
46+
+ ") registered at " + this.platformInvoice.createdAt() + ".";
47+
}
48+
49+
@Override
50+
public String type() {
51+
return "PlatformInvoide Mail Notification";
3552
}
3653
}

0 commit comments

Comments
 (0)