-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathNtfyConnectionImpl.java
More file actions
95 lines (81 loc) · 3.14 KB
/
NtfyConnectionImpl.java
File metadata and controls
95 lines (81 loc) · 3.14 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
84
85
86
87
88
89
90
91
92
93
94
95
package com.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.FileNotFoundException;
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.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
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() {
Dotenv dotenv = Dotenv.load();
String host = dotenv.get("HOST_NAME");
if (host == null || host.isEmpty()) {
throw new IllegalStateException("HOST_NAME has to be defined");
}
this.hostName = host;
}
public NtfyConnectionImpl(String hostName) {
this.hostName = hostName;
}
@Override
public boolean send (String message) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(hostName + "/mytopic"))
.POST(HttpRequest.BodyPublishers.ofString(message))
.build();
try {
http.send(request, HttpResponse.BodyHandlers.discarding());
return true;
} catch (IOException | InterruptedException e) {
System.out.println("Error sending message!");
return false;
}
}
@Override
public boolean sendFile(Path filePath) throws FileNotFoundException {
if (!Files.exists(filePath)) {
throw new FileNotFoundException("File does not exist: " + filePath);
}
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(URI.create(hostName + "/mytopic"))
.header("Content-type", "application/octet-stream")
.POST(HttpRequest.BodyPublishers.ofFile(filePath))
.build();
try {
http.send(httpRequest, HttpResponse.BodyHandlers.discarding());
return true;
} catch (IOException | InterruptedException e) {
System.out.println("Error sending file: " + e.getMessage());
return false;
}
}
@Override
public void receive (Consumer<NtfyMessageDto> handler) {
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(URI.create(hostName + "/mytopic/json"))
.GET()
.build();
http.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofLines())
.thenAccept(response -> response.body()
.map(line -> {
try {
return mapper.readValue(line, NtfyMessageDto.class);
} catch (JsonProcessingException e) {
return null;
}
})
.filter(msg -> msg != null && msg.event().equals("message"))
.forEach(handler));
}
}