Repository navigation
Expand file tree
/
Copy pathBasePushNotificationSender.java
More file actions
117 lines (99 loc) · 4.45 KB
/
Copy pathBasePushNotificationSender.java
File metadata and controls
117 lines (99 loc) · 4.45 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package io.a2a.server.tasks;
import static io.a2a.client.http.A2AHttpClient.APPLICATION_JSON;
import static io.a2a.client.http.A2AHttpClient.CONTENT_TYPE;
import static io.a2a.common.A2AHeaders.X_A2A_NOTIFICATION_TOKEN;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import io.a2a.client.http.A2AHttpClient;
import io.a2a.client.http.JdkA2AHttpClient;
import io.a2a.jsonrpc.common.json.JsonUtil;
import io.a2a.spec.ListTaskPushNotificationConfigParams;
import io.a2a.spec.ListTaskPushNotificationConfigResult;
import io.a2a.spec.PushNotificationConfig;
import io.a2a.spec.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ApplicationScoped
public class BasePushNotificationSender implements PushNotificationSender {
private static final Logger LOGGER = LoggerFactory.getLogger(BasePushNotificationSender.class);
// Fields set by constructor injection cannot be final. We need a noargs constructor for
// Jakarta compatibility, and it seems that making fields set by constructor injection
// final, is not proxyable in all runtimes
private A2AHttpClient httpClient;
private PushNotificationConfigStore configStore;
/**
* No-args constructor for CDI proxy creation.
* CDI requires a non-private constructor to create proxies for @ApplicationScoped beans.
* All fields are initialized by the @Inject constructor during actual bean creation.
*/
@SuppressWarnings("NullAway")
protected BasePushNotificationSender() {
// For CDI proxy creation
this.httpClient = null;
this.configStore = null;
}
@Inject
public BasePushNotificationSender(PushNotificationConfigStore configStore) {
this.httpClient = new JdkA2AHttpClient();
this.configStore = configStore;
}
public BasePushNotificationSender(PushNotificationConfigStore configStore, A2AHttpClient httpClient) {
this.configStore = configStore;
this.httpClient = httpClient;
}
@Override
public void sendNotification(Task task) {
ListTaskPushNotificationConfigResult pushConfigs = configStore.getInfo(new ListTaskPushNotificationConfigParams(task.id()));
if (pushConfigs == null || pushConfigs.isEmpty()) {
return;
}
List<CompletableFuture<Boolean>> dispatchResults = pushConfigs.configs()
.stream()
.map(pushConfig -> dispatch(task, pushConfig.pushNotificationConfig()))
.toList();
CompletableFuture<Void> allFutures = CompletableFuture.allOf(dispatchResults.toArray(new CompletableFuture[0]));
CompletableFuture<Boolean> dispatchResult = allFutures.thenApply(v -> dispatchResults.stream()
.allMatch(CompletableFuture::join));
try {
boolean allSent = dispatchResult.get();
if (! allSent) {
LOGGER.warn("Some push notifications failed to send for taskId: " + task.id());
}
} catch (InterruptedException | ExecutionException e) {
LOGGER.warn("Some push notifications failed to send for taskId " + task.id() + ": {}", e.getMessage(), e);
}
}
private CompletableFuture<Boolean> dispatch(Task task, PushNotificationConfig pushInfo) {
return CompletableFuture.supplyAsync(() -> dispatchNotification(task, pushInfo));
}
private boolean dispatchNotification(Task task, PushNotificationConfig pushInfo) {
String url = pushInfo.url();
String token = pushInfo.token();
A2AHttpClient.PostBuilder postBuilder = httpClient.createPost();
if (token != null && !token.isBlank()) {
postBuilder.addHeader(X_A2A_NOTIFICATION_TOKEN, token);
}
String body;
try {
body = JsonUtil.toJson(task);
} catch (Throwable throwable) {
LOGGER.debug("Error writing value as string: {}", throwable.getMessage(), throwable);
return false;
}
try {
postBuilder
.url(url)
.addHeader(CONTENT_TYPE, APPLICATION_JSON)
.body(body)
.post();
} catch (IOException | InterruptedException e) {
LOGGER.debug("Error pushing data to " + url + ": {}", e.getMessage(), e);
return false;
}
return true;
}
}