-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNtfyConnectionImpl.java
More file actions
83 lines (71 loc) · 3.08 KB
/
NtfyConnectionImpl.java
File metadata and controls
83 lines (71 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.example;
import io.github.cdimascio.dotenv.Dotenv;
import tools.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public class NtfyConnectionImpl implements NtfyConnection {
private final HttpClient http = HttpClient.newHttpClient();
private final String hostName;
private final ObjectMapper mapper = new ObjectMapper();
public NtfyConnectionImpl() {
String loadedHostName = null;
try {
Dotenv dotenv = Dotenv.load();
loadedHostName = dotenv.get("HOST_NAME");
} catch (Exception e) {
System.err.println("WARNING: Could not load .env file for HOST_NAME. Using fallback.");
}
this.hostName = (loadedHostName != null)
? loadedHostName
: "http://localhost:8080";
if (this.hostName.equals("http://localhost:8080")) {
System.out.println("DEBUG: NtfyConnectionImpl running in test/fallback mode.");
}
}
public NtfyConnectionImpl(String hostName) {
this.hostName = hostName;
}
@Override
public CompletableFuture<Void> send(String message) {
HttpRequest httpRequest = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(message))
.uri(URI.create(hostName + "/mytopic"))
.build();
return http.sendAsync(httpRequest, HttpResponse.BodyHandlers.discarding())
.thenAccept(response -> {
if (response.statusCode() >= 200 && response.statusCode() < 300) {
System.out.println("Message sent successfully.");
} else {
System.err.println("Error while sending: " + response.statusCode());
}
})
.exceptionally(e -> {
System.err.println("Network issue: " + e.getMessage());
return null;
});
}
@Override
public void receive(Consumer<NtfyMessageDto> messageHandler) {
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create(hostName + "/mytopic/json"))
.build();
http.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofLines())
.thenAccept(response -> response.body()
.map(s -> {
try {
return mapper.readValue(s, NtfyMessageDto.class);
} catch (Exception e) {
System.err.println("Failed to parse message: " + e.getMessage());
return null;
}
})
.filter(messageDto -> messageDto != null && messageDto.event().equals("message"))
.peek(System.out::println)
.forEach(messageHandler));
}
}