2424
2525import com .selfxdsd .api .EmailNotification ;
2626import 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 */
3449public 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}
0 commit comments