use of software.amazon.awssdk.aws.greengrass.model.JsonMessage in project aws-greengrass-nucleus by aws-greengrass.
the class PubSubIPCEventStreamAgent method handlePublishToTopicRequest.
@SuppressWarnings("PMD.PreserveStackTrace")
private PublishToTopicResponse handlePublishToTopicRequest(String topic, String serviceName, Optional<Map<String, Object>> jsonMessage, Optional<byte[]> binaryMessage) {
if (topic == null) {
throw new InvalidArgumentsError("Publish topic must not be null");
}
if (topic.contains(MQTT_SINGLELEVEL_WILDCARD) || topic.contains(MQTT_MULTILEVEL_WILDCARD) || topic.contains(GLOB_WILDCARD)) {
throw new InvalidArgumentsError("Publish topic must not contain a wildcard.");
}
Set<Object> contexts = listeners.get(topic);
if (contexts == null || contexts.isEmpty()) {
log.atDebug().kv(COMPONENT_NAME, serviceName).log("No one subscribed to topic {}. Returning.", topic);
// Still technically successful, just no one was subscribed
return new PublishToTopicResponse();
}
SubscriptionResponseMessage message = new SubscriptionResponseMessage();
PublishEvent publishedEvent = PublishEvent.builder().topic(topic).build();
if (jsonMessage.isPresent()) {
JsonMessage message1 = new JsonMessage();
message1.setMessage(jsonMessage.get());
message1.setEventTopic(topic);
message.setJsonMessage(message1);
try {
publishedEvent.setPayload(SERIALIZER.writeValueAsBytes(jsonMessage.get()));
} catch (JsonProcessingException e) {
log.atError().cause(e).kv(COMPONENT_NAME, serviceName).log("Unable to serialize JSON message.");
throw new InvalidArgumentsError("Unable to serialize payload as JSON");
}
}
if (binaryMessage.isPresent()) {
BinaryMessage binaryMessage1 = new BinaryMessage();
binaryMessage1.setMessage(binaryMessage.get());
binaryMessage1.setEventTopic(topic);
message.setBinaryMessage(binaryMessage1);
publishedEvent.setPayload(binaryMessage.get());
}
contexts.forEach(context -> {
log.atDebug().kv(COMPONENT_NAME, serviceName).log("Sending publish event for topic {}", topic);
if (context instanceof StreamEventPublisher) {
StreamEventPublisher<SubscriptionResponseMessage> publisher = (StreamEventPublisher<SubscriptionResponseMessage>) context;
orderedExecutorService.execute(() -> publisher.sendStreamEvent(message), publisher);
} else if (context instanceof Consumer) {
Consumer<PublishEvent> consumer = (Consumer<PublishEvent>) context;
orderedExecutorService.execute(() -> consumer.accept(publishedEvent), consumer);
}
});
return new PublishToTopicResponse();
}
use of software.amazon.awssdk.aws.greengrass.model.JsonMessage in project aws-greengrass-nucleus by aws-greengrass.
the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_json_message_THEN_publishes_message_inorder.
@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_json_message_THEN_publishes_message_inorder() throws InterruptedException, AuthorizationException {
StreamEventPublisher publisher = mock(StreamEventPublisher.class);
Set<Object> set = new HashSet<>();
set.add(publisher);
pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
List<PublishToTopicRequest> publishToTopicRequests = new ArrayList<>();
for (int i = 0; i < 10; i++) {
PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
publishToTopicRequest.setTopic(TEST_TOPIC);
PublishMessage publishMessage = new PublishMessage();
JsonMessage jsonMessage = new JsonMessage();
Map<String, Object> message = new HashMap<>();
message.putIfAbsent("SomeKey", i);
jsonMessage.setMessage(message);
publishMessage.setJsonMessage(jsonMessage);
publishToTopicRequest.setPublishMessage(publishMessage);
publishToTopicRequests.add(publishToTopicRequest);
}
try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
for (PublishToTopicRequest publishToTopicRequest : publishToTopicRequests) {
PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
assertNotNull(publishToTopicResponse);
}
verify(authorizationHandler, times(10)).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
Permission capturedPermission = permissionArgumentCaptor.getValue();
assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
assertThat(capturedPermission.getResource(), is(TEST_TOPIC));
TimeUnit.SECONDS.sleep(2);
assertNotNull(subscriptionResponseMessageCaptor.getAllValues());
assertEquals(10, subscriptionResponseMessageCaptor.getAllValues().size());
int i = 0;
for (SubscriptionResponseMessage responseMessage : subscriptionResponseMessageCaptor.getAllValues()) {
assertNotNull(responseMessage.getJsonMessage());
assertNull(responseMessage.getBinaryMessage());
assertThat(responseMessage.getJsonMessage().getMessage(), IsMapContaining.hasEntry("SomeKey", i));
assertEquals(TEST_TOPIC, responseMessage.getJsonMessage().getEventTopic());
i++;
}
}
}
use of software.amazon.awssdk.aws.greengrass.model.JsonMessage in project aws-greengrass-nucleus by aws-greengrass.
the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_json_message_THEN_publishes_message.
@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_json_message_THEN_publishes_message() throws InterruptedException, AuthorizationException {
StreamEventPublisher publisher = mock(StreamEventPublisher.class);
Set<Object> set = new HashSet<>();
set.add(publisher);
pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
publishToTopicRequest.setTopic(TEST_TOPIC);
PublishMessage publishMessage = new PublishMessage();
JsonMessage jsonMessage = new JsonMessage();
Map<String, Object> message = new HashMap<>();
message.putIfAbsent("SomeKey", "SomValue");
jsonMessage.setMessage(message);
publishMessage.setJsonMessage(jsonMessage);
publishToTopicRequest.setPublishMessage(publishMessage);
try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
assertNotNull(publishToTopicResponse);
verify(authorizationHandler).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
Permission capturedPermission = permissionArgumentCaptor.getValue();
assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
assertThat(capturedPermission.getResource(), is(TEST_TOPIC));
TimeUnit.SECONDS.sleep(2);
assertNotNull(subscriptionResponseMessageCaptor.getValue());
SubscriptionResponseMessage responseMessage = subscriptionResponseMessageCaptor.getValue();
assertNotNull(responseMessage.getJsonMessage());
assertNull(responseMessage.getBinaryMessage());
assertThat(responseMessage.getJsonMessage().getMessage(), IsMapContaining.hasEntry("SomeKey", "SomValue"));
assertEquals(TEST_TOPIC, responseMessage.getJsonMessage().getEventTopic());
}
}
Aggregations