Move notification implementations to Nenjim
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package com.r35157.libs.notification.impl.pushover;
|
||||
|
||||
import com.r35157.libs.notification.BoundNotifier;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class PushOverNotifier implements BoundNotifier<PushMessage> {
|
||||
|
||||
public PushOverNotifier(ΩAPIKeyΩ token, ΩUserNameΩ userName) {
|
||||
this.httpClient = HttpClient.newHttpClient();
|
||||
this.token = token;
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(PushMessage message) throws IOException {
|
||||
String body = formEncode(
|
||||
"token", token,
|
||||
"user", userName,
|
||||
"message", message.text()
|
||||
);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(PUSHOVER_MESSAGES_URI)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response;
|
||||
|
||||
try {
|
||||
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
} catch(InterruptedException ie) {
|
||||
throw new IOException("Pushover request was interrupted!", ie);
|
||||
}
|
||||
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new IOException("Pushover request failed! HTTP "
|
||||
+ response.statusCode() + ": " + response.body());
|
||||
}
|
||||
}
|
||||
|
||||
private static String formEncode(String... keyValues) {
|
||||
if (keyValues.length % 2 != 0) {
|
||||
throw new IllegalArgumentException("keyValues must contain key/value pairs");
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < keyValues.length; i += 2) {
|
||||
if (!result.isEmpty()) {
|
||||
result.append('&');
|
||||
}
|
||||
|
||||
result.append(encode(keyValues[i]))
|
||||
.append('=')
|
||||
.append(encode(keyValues[i + 1]));
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static String encode(String value) {
|
||||
return URLEncoder.encode(value, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private void test() throws IOException {
|
||||
ΩAPIKeyΩ apiKey = "SomeKey";
|
||||
ΩUserNameΩ userName = "userNmae";
|
||||
|
||||
BoundNotifier<PushMessage> boundNotifier = new PushOverNotifier(apiKey, userName);
|
||||
|
||||
PushMessage message = new PushMessage("Hello World!");
|
||||
boundNotifier.push(message);
|
||||
}
|
||||
|
||||
private static final URI PUSHOVER_MESSAGES_URI =
|
||||
URI.create("https://api.pushover.net/1/messages.json");
|
||||
|
||||
private final HttpClient httpClient;
|
||||
private final ΩAPIKeyΩ token;
|
||||
private final ΩUserNameΩ userName;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.r35157.libs.notification.impl.smtp;
|
||||
|
||||
import com.r35157.libs.notification.NotificationDestination;
|
||||
|
||||
public record SMTPDestination(ΩEmailAddressΩ emailAddress) implements NotificationDestination { }
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.r35157.libs.notification.impl.smtp;
|
||||
|
||||
import com.r35157.libs.notification.AddressedNotifier;
|
||||
import com.r35157.libs.notification.BoundNotifier;
|
||||
import com.r35157.libs.valuetypes.basic.Credentials;
|
||||
import com.r35157.libs.valuetypes.basic.NetworkEndPoint;
|
||||
import com.r35157.libs.valuetypes.basic.SmtpConfiguration;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SMTPNotifier implements AddressedNotifier<SMTPDestination, EmailMessage> {
|
||||
|
||||
public SMTPNotifier(SmtpConfiguration smtpConfiguration) {
|
||||
this.smtpConfiguration = smtpConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(SMTPDestination destination, EmailMessage message) throws IOException {
|
||||
throw new UnsupportedOperationException("Not implemented yet!");
|
||||
}
|
||||
|
||||
private void test() throws IOException {
|
||||
ΩHostnameΩ hostName = new ΩHostnameΩ("SomeSMTPHost");
|
||||
ΩportNumberΩ portNumber = 2525;
|
||||
NetworkEndPoint networkEndPoint = new NetworkEndPoint(hostName, portNumber);
|
||||
|
||||
ΩUserNameΩ userName = "SomeUser";
|
||||
ΩPasswordΩ password = "SomePassword";
|
||||
Credentials credentials = new Credentials(userName, password);
|
||||
|
||||
SmtpConfiguration smtpConfiguration = new SmtpConfiguration(networkEndPoint, credentials);
|
||||
AddressedNotifier<SMTPDestination, EmailMessage> notifier = new SMTPNotifier(smtpConfiguration);
|
||||
|
||||
ΩEmailAddressΩ emailAddress = "SomeReceiver";
|
||||
SMTPDestination destination = new SMTPDestination(emailAddress);
|
||||
BoundNotifier boundNotifier = notifier.bind(destination);
|
||||
|
||||
EmailSubject subject = new EmailSubject("SomeSubject");
|
||||
EmailBody body = new EmailBody("SomeBody");
|
||||
EmailMessage message = new EmailMessage(subject, body);
|
||||
boundNotifier.push(message);
|
||||
}
|
||||
|
||||
private final SmtpConfiguration smtpConfiguration;
|
||||
}
|
||||
Reference in New Issue
Block a user