Skip to content

Commit 845f3e2

Browse files
committed
:(
1 parent 21c51e8 commit 845f3e2

12 files changed

Lines changed: 265 additions & 10 deletions

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HOST_NAME=https://ntfy.fungover.org

pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@
4545
<artifactId>javafx-fxml</artifactId>
4646
<version>${javafx.version}</version>
4747
</dependency>
48+
<dependency>
49+
<groupId>io.github.cdimascio</groupId>
50+
<artifactId>dotenv-java</artifactId>
51+
<version>3.2.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>tools.jackson.core</groupId>
55+
<artifactId>jackson-databind</artifactId>
56+
<version>3.0.1</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.wiremock</groupId>
60+
<artifactId>wiremock</artifactId>
61+
<version>4.0.0-beta.15</version>
62+
<scope>test</scope>
63+
</dependency>
4864
</dependencies>
4965
<build>
5066
<plugins>
@@ -55,7 +71,7 @@
5571
<configuration>
5672
<mainClass>com.example.HelloFX</mainClass>
5773
<options>
58-
<option>--enable-native-access=javafx.graphics</option>
74+
<option>--enable-native-access=javafx.graphics</option>
5975
</options>
6076
<launcher>javafx</launcher>
6177
<stripDebug>true</stripDebug>
@@ -65,4 +81,4 @@
6581
</plugin>
6682
</plugins>
6783
</build>
68-
</project>
84+
</project>
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.example;
22

3+
import javafx.event.ActionEvent;
34
import javafx.fxml.FXML;
45
import javafx.scene.control.Label;
6+
import javafx.scene.control.ListView;
57

68
/**
79
* Controller layer: mediates between the view (FXML) and the model.
810
*/
911
public class HelloController {
1012

11-
private final HelloModel model = new HelloModel();
13+
private final HelloModel model = new HelloModel(new NtfyConnectionImpl());
14+
public ListView<NtfyMessageDto> messageView;
1215

1316
@FXML
1417
private Label messageLabel;
@@ -18,5 +21,10 @@ private void initialize() {
1821
if (messageLabel != null) {
1922
messageLabel.setText(model.getGreeting());
2023
}
24+
messageView.setItems(model.getMessages());
2125
}
22-
}
26+
27+
public void sendMessage(ActionEvent actionEvent) {
28+
model.sendMessage();
29+
}
30+
}
Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
package com.example;
22

3+
import javafx.application.Platform;
4+
import javafx.beans.property.SimpleStringProperty;
5+
import javafx.beans.property.StringProperty;
6+
import javafx.collections.FXCollections;
7+
import javafx.collections.ObservableList;
8+
39
/**
410
* Model layer: encapsulates application data and business logic.
511
*/
612
public class HelloModel {
13+
14+
private final NtfyConnection connection;
15+
16+
private final ObservableList<NtfyMessageDto> messages = FXCollections.observableArrayList();
17+
private final StringProperty messageToSend = new SimpleStringProperty();
18+
19+
public HelloModel(NtfyConnection connection) {
20+
this.connection = connection;
21+
receiveMessage();
22+
}
23+
24+
public ObservableList<NtfyMessageDto> getMessages() {
25+
return messages;
26+
}
27+
28+
public String getMessageToSend() {
29+
return messageToSend.get();
30+
}
31+
32+
public StringProperty messageToSendProperty() {
33+
return messageToSend;
34+
}
35+
36+
public void setMessageToSend(String message) {
37+
messageToSend.set(message);
38+
}
39+
740
/**
841
* Returns a greeting based on the current Java and JavaFX versions.
942
*/
@@ -12,4 +45,13 @@ public String getGreeting() {
1245
String javafxVersion = System.getProperty("javafx.version");
1346
return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".";
1447
}
15-
}
48+
49+
public void sendMessage() {
50+
connection.send(messageToSend.get());
51+
52+
}
53+
54+
public void receiveMessage() {
55+
connection.receive(m -> Platform.runLater(() -> messages.add(m)));
56+
}
57+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example;
2+
3+
public class ManyParameters {
4+
5+
public ManyParameters(String computerName, int timeout,
6+
String method, int size, byte[] data) {
7+
8+
}
9+
10+
11+
static void main() {
12+
ManyParametersBuilder builder = new ManyParametersBuilder();
13+
builder
14+
.setComputerName("localhost") //Fluent API
15+
.setTimeout(10)
16+
.setSize(0)
17+
.createManyParameters();
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example;
2+
3+
public class ManyParametersBuilder {
4+
private String computerName;
5+
private int timeout = 0;
6+
private String method;
7+
private int size = 0;
8+
private byte[] data = null;
9+
10+
public ManyParametersBuilder setComputerName(String computerName) {
11+
this.computerName = computerName;
12+
return this;
13+
}
14+
15+
public ManyParametersBuilder setTimeout(int timeout) {
16+
this.timeout = timeout;
17+
return this;
18+
}
19+
20+
public ManyParametersBuilder setMethod(String method) {
21+
this.method = method;
22+
return this;
23+
}
24+
25+
public ManyParametersBuilder setSize(int size) {
26+
this.size = size;
27+
return this;
28+
}
29+
30+
public ManyParametersBuilder setData(byte[] data) {
31+
this.data = data;
32+
return this;
33+
}
34+
35+
public ManyParameters createManyParameters() {
36+
return new ManyParameters(computerName, timeout, method, size, data);
37+
}
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example;
2+
3+
import java.util.function.Consumer;
4+
5+
public interface NtfyConnection {
6+
7+
public boolean send(String message);
8+
9+
public void receive(Consumer<NtfyMessageDto> messageHandler);
10+
11+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.example;
2+
3+
import io.github.cdimascio.dotenv.Dotenv;
4+
import tools.jackson.databind.ObjectMapper;
5+
6+
import java.io.IOException;
7+
import java.net.URI;
8+
import java.net.http.HttpClient;
9+
import java.net.http.HttpRequest;
10+
import java.net.http.HttpResponse;
11+
import java.util.Objects;
12+
import java.util.function.Consumer;
13+
14+
public class NtfyConnectionImpl implements NtfyConnection {
15+
16+
private final HttpClient http = HttpClient.newHttpClient();
17+
private final String hostName;
18+
private final ObjectMapper mapper = new ObjectMapper();
19+
20+
public NtfyConnectionImpl() {
21+
Dotenv dotenv = Dotenv.load();
22+
hostName = Objects.requireNonNull(dotenv.get("HOST_NAME"));
23+
}
24+
25+
public NtfyConnectionImpl(String hostName) {
26+
this.hostName = hostName;
27+
}
28+
29+
@Override
30+
public boolean send(String message) {
31+
HttpRequest httpRequest = HttpRequest.newBuilder()
32+
.POST(HttpRequest.BodyPublishers.ofString(message))
33+
.header("Cache", "no")
34+
.uri(URI.create(hostName + "/mytopic"))
35+
.build();
36+
try {
37+
//Todo: handle long blocking send requests to not freeze the JavaFX thread
38+
//1. Use thread send message?
39+
//2. Use async?
40+
var reponse = http.send(httpRequest, HttpResponse.BodyHandlers.discarding());
41+
return true;
42+
} catch (IOException e) {
43+
System.out.println("Error sending message");
44+
} catch (InterruptedException e) {
45+
System.out.println("Interruped sending message");
46+
}
47+
return false;
48+
}
49+
50+
@Override
51+
public void receive(Consumer<NtfyMessageDto> messageHandler) {
52+
HttpRequest httpRequest = HttpRequest.newBuilder()
53+
.GET()
54+
.uri(URI.create(hostName + "/mytopic/json"))
55+
.build();
56+
57+
http.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofLines())
58+
.thenAccept(response -> response.body()
59+
.map(s ->
60+
mapper.readValue(s, NtfyMessageDto.class))
61+
.filter(message -> message.event().equals("message"))
62+
.peek(System.out::println)
63+
.forEach(messageHandler));
64+
}
65+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
import java.util.Objects;
6+
7+
@JsonIgnoreProperties(ignoreUnknown = true)
8+
public record NtfyMessageDto(String id, long time, String event, String topic, String message) {
9+
10+
@Override
11+
public boolean equals(Object o) {
12+
if (o == null || getClass() != o.getClass()) return false;
13+
NtfyMessageDto that = (NtfyMessageDto) o;
14+
return time == that.time && Objects.equals(id, that.id) && Objects.equals(event, that.event) && Objects.equals(topic, that.topic) && Objects.equals(message, that.message);
15+
}
16+
17+
18+
19+
@Override
20+
public int hashCode() {
21+
return Objects.hash(id, time, event, topic, message);
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "NtfyMessageDto{" +
27+
"id='" + id + '\'' +
28+
", time=" + time +
29+
", event='" + event + '\'' +
30+
", topic='" + topic + '\'' +
31+
", message='" + message + '\'' +
32+
'}';
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example;
2+
3+
public class Singelton {
4+
5+
private final static Singelton instance = new Singelton();
6+
7+
private Singelton(){
8+
9+
}
10+
11+
public static Singelton getInstance(){
12+
return instance;
13+
}
14+
}

0 commit comments

Comments
 (0)