|
1 | 1 | package io.a2a.extras.pushnotificationconfigstore.database.jpa; |
2 | 2 |
|
| 3 | +import io.a2a.server.config.A2AConfigProvider; |
| 4 | +import jakarta.annotation.PostConstruct; |
| 5 | +import jakarta.inject.Inject; |
3 | 6 | import jakarta.persistence.TypedQuery; |
4 | 7 | import java.time.Instant; |
5 | 8 | import java.util.List; |
@@ -29,18 +32,42 @@ public class JpaDatabasePushNotificationConfigStore implements PushNotificationC |
29 | 32 | private static final Logger LOGGER = LoggerFactory.getLogger(JpaDatabasePushNotificationConfigStore.class); |
30 | 33 |
|
31 | 34 | private static final Instant NULL_TIMESTAMP_SENTINEL = Instant.EPOCH; |
| 35 | + private static final String A2A_PUSH_NOTIFICATION_MAX_PAGE_SIZE_CONFIG = "a2a.push-notification-config.max-page-size"; |
| 36 | + private static final int A2A_PUSH_NOTIFICATION_DEFAULT_MAX_PAGE_SIZE = 100; |
32 | 37 |
|
33 | 38 | @PersistenceContext(unitName = "a2a-java") |
34 | 39 | EntityManager em; |
35 | 40 |
|
| 41 | + @Inject |
| 42 | + A2AConfigProvider configProvider; |
| 43 | + |
| 44 | + /** |
| 45 | + * Maximum page size when listing push notification configurations for a task. |
| 46 | + * Requested page sizes exceeding this value will be capped to this limit. |
| 47 | + * <p> |
| 48 | + * Property: {@code a2a.push-notification-config.max-page-size}<br> |
| 49 | + * Default: 100<br> |
| 50 | + * Note: Property override requires a configurable {@link A2AConfigProvider} on the classpath. |
| 51 | + */ |
| 52 | + int maxPageSize; |
| 53 | + |
| 54 | + @PostConstruct |
| 55 | + void initConfig() { |
| 56 | + try { |
| 57 | + maxPageSize = Integer.parseInt(configProvider.getValue(A2A_PUSH_NOTIFICATION_MAX_PAGE_SIZE_CONFIG)); |
| 58 | + } catch (Exception e) { |
| 59 | + LOGGER.warn("Failed to read '{}' configuration, falling back to default page size of {}.", |
| 60 | + A2A_PUSH_NOTIFICATION_MAX_PAGE_SIZE_CONFIG, A2A_PUSH_NOTIFICATION_DEFAULT_MAX_PAGE_SIZE, e); |
| 61 | + maxPageSize = A2A_PUSH_NOTIFICATION_DEFAULT_MAX_PAGE_SIZE; |
| 62 | + } |
| 63 | + } |
| 64 | + |
36 | 65 | @Transactional |
37 | 66 | @Override |
38 | 67 | public PushNotificationConfig setInfo(String taskId, PushNotificationConfig notificationConfig) { |
39 | 68 | // Ensure config has an ID - default to taskId if not provided (mirroring InMemoryPushNotificationConfigStore behavior) |
40 | 69 | PushNotificationConfig.Builder builder = PushNotificationConfig.builder(notificationConfig); |
41 | 70 | if (notificationConfig.id() == null || notificationConfig.id().isEmpty()) { |
42 | | - // This means the taskId and configId are same. This will not allow having multiple configs for a single Task. |
43 | | - // The configId is a required field in the spec and should not be empty |
44 | 71 | builder.id(taskId); |
45 | 72 | } |
46 | 73 | notificationConfig = builder.build(); |
@@ -80,44 +107,48 @@ public ListTaskPushNotificationConfigResult getInfo(ListTaskPushNotificationConf |
80 | 107 | LOGGER.debug("Retrieving PushNotificationConfigs for Task '{}' with params: pageSize={}, pageToken={}", |
81 | 108 | taskId, params.pageSize(), params.pageToken()); |
82 | 109 | try { |
83 | | - StringBuilder queryBuilder = new StringBuilder("SELECT c FROM JpaPushNotificationConfig c WHERE c.id.taskId = :taskId"); |
| 110 | + // Parse pageToken once upfront |
| 111 | + Instant tokenTimestamp = null; |
| 112 | + String tokenId = null; |
84 | 113 |
|
85 | 114 | if (params.pageToken() != null && !params.pageToken().isEmpty()) { |
86 | | - String[] tokenParts = params.pageToken().split(":", 2); |
87 | | - if (tokenParts.length == 2) { |
88 | | - // Keyset pagination: get tasks where timestamp < tokenTimestamp OR (timestamp = tokenTimestamp AND id > tokenId) |
89 | | - // All tasks have timestamps (TaskStatus canonical constructor ensures this) |
| 115 | + String[] tokenParts = params.pageToken().split(":", 2); |
| 116 | + if (tokenParts.length != 2) { |
| 117 | + throw new io.a2a.spec.InvalidParamsError(null, |
| 118 | + "Invalid pageToken format: pageToken must be in 'timestamp_millis:configId' format", null); |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + long timestampMillis = Long.parseLong(tokenParts[0]); |
| 123 | + tokenTimestamp = Instant.ofEpochMilli(timestampMillis); |
| 124 | + tokenId = tokenParts[1]; |
| 125 | + } catch (NumberFormatException e) { |
| 126 | + throw new io.a2a.spec.InvalidParamsError(null, |
| 127 | + "Invalid pageToken format: timestamp must be numeric milliseconds", null); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Build query using the parsed values |
| 132 | + StringBuilder queryBuilder = new StringBuilder("SELECT c FROM JpaPushNotificationConfig c WHERE c.id.taskId = :taskId"); |
| 133 | + |
| 134 | + if (tokenTimestamp != null) { |
| 135 | + // Keyset pagination: get notifications where timestamp < tokenTimestamp OR (timestamp = tokenTimestamp AND id > tokenId) |
90 | 136 | queryBuilder.append(" AND (COALESCE(c.createdAt, :nullSentinel) < :tokenTimestamp OR (COALESCE(c.createdAt, :nullSentinel) = :tokenTimestamp AND c.id.configId > :tokenId))"); |
91 | | - } else { |
92 | | - // Based on the comments in the test case, if the pageToken is invalid start from the beginning. |
93 | | - } |
94 | 137 | } |
95 | 138 |
|
96 | 139 | queryBuilder.append(" ORDER BY COALESCE(c.createdAt, :nullSentinel) DESC, c.id.configId ASC"); |
97 | 140 |
|
| 141 | + // Create query and set parameters |
98 | 142 | TypedQuery<JpaPushNotificationConfig> query = em.createQuery(queryBuilder.toString(), JpaPushNotificationConfig.class); |
99 | 143 | query.setParameter("taskId", taskId); |
100 | 144 | query.setParameter("nullSentinel", NULL_TIMESTAMP_SENTINEL); |
101 | 145 |
|
102 | | - if (params.pageToken() != null && !params.pageToken().isEmpty()) { |
103 | | - String[] tokenParts = params.pageToken().split(":", 2); |
104 | | - if (tokenParts.length == 2) { |
105 | | - try { |
106 | | - long timestampMillis = Long.parseLong(tokenParts[0]); |
107 | | - String tokenId = tokenParts[1]; |
108 | | - |
109 | | - Instant tokenTimestamp = Instant.ofEpochMilli(timestampMillis); |
110 | | - query.setParameter("tokenTimestamp", tokenTimestamp); |
111 | | - query.setParameter("tokenId", tokenId); |
112 | | - } catch (NumberFormatException e) { |
113 | | - // Malformed timestamp in pageToken |
114 | | - throw new io.a2a.spec.InvalidParamsError(null, |
115 | | - "Invalid pageToken format: timestamp must be numeric milliseconds", null); |
116 | | - } |
117 | | - } |
| 146 | + if (tokenTimestamp != null) { |
| 147 | + query.setParameter("tokenTimestamp", tokenTimestamp); |
| 148 | + query.setParameter("tokenId", tokenId); |
118 | 149 | } |
119 | 150 |
|
120 | | - int pageSize = params.getEffectivePageSize(); |
| 151 | + int pageSize = params.getEffectivePageSize(maxPageSize); |
121 | 152 | query.setMaxResults(pageSize + 1); |
122 | 153 | List<JpaPushNotificationConfig> jpaConfigsPage = query.getResultList(); |
123 | 154 |
|
|
0 commit comments