use of software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError 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.InvalidArgumentsError in project aws-greengrass-nucleus by aws-greengrass.
the class ConfigStoreIPCEventStreamAgentTest method GIVEN_no_validation_event_is_tracked_WHEN_send_config_validity_report_request_THEN_fail.
@Test
void GIVEN_no_validation_event_is_tracked_WHEN_send_config_validity_report_request_THEN_fail() {
when(mockAuthenticationData.getIdentityLabel()).thenReturn(TEST_COMPONENT_A);
SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
validityReport.setStatus(ConfigurationValidityStatus.ACCEPTED);
validityReport.setDeploymentId("abc");
reportRequest.setConfigurationValidityReport(validityReport);
InvalidArgumentsError error = assertThrows(InvalidArgumentsError.class, () -> agent.getSendConfigurationValidityReportHandler(mockContext).handleRequest(reportRequest));
assertEquals("Validation request either timed out or was never made", error.getMessage());
}
use of software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError in project aws-greengrass-nucleus by aws-greengrass.
the class ConfigStoreIPCEventStreamAgentTest method GIVEN_request_has_null_deployment_id_THEN_fail.
@Test
void GIVEN_request_has_null_deployment_id_THEN_fail() {
SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
reportRequest.setConfigurationValidityReport(validityReport);
InvalidArgumentsError error = assertThrows(InvalidArgumentsError.class, () -> agent.getSendConfigurationValidityReportHandler(mockContext).handleRequest(reportRequest));
assertEquals("Cannot accept configuration validity report, the deployment ID provided was null", error.getMessage());
}
Aggregations