Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0bf8305

Browse files
committedJan 21, 2025·
fix WoT action validation was only done for application/json content-type
1 parent 4966ef4 commit 0bf8305

File tree

1 file changed

+52
-68
lines changed

1 file changed

+52
-68
lines changed
 

‎things/service/src/main/java/org/eclipse/ditto/things/service/enforcement/ThingEnforcerActor.java

+52-68
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
3535
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
3636
import org.eclipse.ditto.base.model.headers.DittoHeaders;
37-
import org.eclipse.ditto.base.model.headers.contenttype.ContentType;
3837
import org.eclipse.ditto.base.model.namespaces.NamespaceBlockedException;
3938
import org.eclipse.ditto.base.model.signals.Signal;
4039
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
@@ -572,29 +571,25 @@ private CompletionStage<Boolean> doesThingExist() {
572571
private CompletionStage<MessageCommand<?, ?>> performWotBasedMessageCommandValidation(
573572
final MessageCommand<?, ?> messageCommand
574573
) {
575-
if (isJsonMessageContent(messageCommand.getMessage())) {
576-
@SuppressWarnings("unchecked") final Message<JsonValue> message =
577-
((MessageCommand<JsonValue, ?>) messageCommand)
578-
.getMessage();
579-
580-
final MessageDirection messageDirection = message.getDirection();
581-
final JsonValue messageCommandPayload = message
582-
.getPayload()
583-
.orElse(null);
584-
585-
if (messageCommand instanceof SendThingMessage<?> sendThingMessage) {
586-
return performWotBasedThingMessageValidation(messageCommand, sendThingMessage, messageDirection,
587-
messageCommandPayload
588-
).thenApply(aVoid -> messageCommand);
589-
} else if (messageCommand instanceof SendFeatureMessage<?> sendFeatureMessage) {
590-
final String featureId = sendFeatureMessage.getFeatureId();
591-
return performWotBasedFeatureMessageValidation(messageCommand, sendFeatureMessage, featureId,
592-
messageDirection, messageCommandPayload
593-
).thenApply(aVoid -> messageCommand);
574+
@SuppressWarnings("unchecked") final Message<JsonValue> message =
575+
((MessageCommand<JsonValue, ?>) messageCommand)
576+
.getMessage();
577+
578+
final MessageDirection messageDirection = message.getDirection();
579+
final JsonValue messageCommandPayload = message
580+
.getPayload()
581+
.orElse(null);
582+
583+
if (messageCommand instanceof SendThingMessage<?> sendThingMessage) {
584+
return performWotBasedThingMessageValidation(messageCommand, sendThingMessage, messageDirection,
585+
messageCommandPayload
586+
).thenApply(aVoid -> messageCommand);
587+
} else if (messageCommand instanceof SendFeatureMessage<?> sendFeatureMessage) {
588+
final String featureId = sendFeatureMessage.getFeatureId();
589+
return performWotBasedFeatureMessageValidation(messageCommand, sendFeatureMessage, featureId,
590+
messageDirection, messageCommandPayload
591+
).thenApply(aVoid -> messageCommand);
594592

595-
} else {
596-
return CompletableFuture.completedFuture(messageCommand);
597-
}
598593
} else {
599594
return CompletableFuture.completedFuture(messageCommand);
600595
}
@@ -674,56 +669,45 @@ private CompletionStage<Void> performWotBasedFeatureMessageValidation(final Mess
674669
private CompletionStage<MessageCommandResponse<?, ?>> performWotBasedMessageCommandResponseValidation(
675670
final MessageCommandResponse<?, ?> messageCommandResponse
676671
) {
677-
if (isJsonMessageContent(messageCommandResponse.getMessage())) {
678-
@SuppressWarnings("unchecked") final Message<JsonValue> message =
679-
((MessageCommandResponse<JsonValue, ?>) messageCommandResponse)
680-
.getMessage();
681-
682-
final MessageDirection messageDirection = message.getDirection();
683-
final JsonValue messageCommandPayload = message
684-
.getPayload()
685-
.orElse(null);
686-
687-
if (messageDirection == MessageDirection.TO &&
688-
messageCommandResponse instanceof SendThingMessageResponse<?> sendThingMessageResponse) {
689-
return resolveThingDefinition()
690-
.thenCompose(optThingDefinition -> thingModelValidator.validateThingActionOutput(
691-
optThingDefinition.orElse(null),
692-
sendThingMessageResponse.getMessage().getSubject(),
693-
messageCommandPayload,
694-
sendThingMessageResponse.getResourcePath(),
695-
sendThingMessageResponse.getDittoHeaders()
696-
))
697-
.thenApply(aVoid -> messageCommandResponse);
698-
} else if (messageDirection == MessageDirection.TO &&
699-
messageCommandResponse instanceof SendFeatureMessageResponse<?> sendFeatureMessageResponse) {
700-
final String featureId = sendFeatureMessageResponse.getFeatureId();
701-
return resolveThingAndFeatureDefinition(featureId)
702-
.thenCompose(optDefinitionPair -> thingModelValidator.validateFeatureActionOutput(
703-
optDefinitionPair.first().orElse(null),
704-
optDefinitionPair.second().orElse(null),
705-
featureId,
706-
sendFeatureMessageResponse.getMessage().getSubject(),
707-
messageCommandPayload,
708-
sendFeatureMessageResponse.getResourcePath(),
709-
sendFeatureMessageResponse.getDittoHeaders()
710-
))
711-
.thenApply(aVoid -> messageCommandResponse);
712-
} else {
713-
return CompletableFuture.completedFuture(messageCommandResponse);
714-
}
672+
@SuppressWarnings("unchecked") final Message<JsonValue> message =
673+
((MessageCommandResponse<JsonValue, ?>) messageCommandResponse)
674+
.getMessage();
675+
676+
final MessageDirection messageDirection = message.getDirection();
677+
final JsonValue messageCommandPayload = message
678+
.getPayload()
679+
.orElse(null);
680+
681+
if (messageDirection == MessageDirection.TO &&
682+
messageCommandResponse instanceof SendThingMessageResponse<?> sendThingMessageResponse) {
683+
return resolveThingDefinition()
684+
.thenCompose(optThingDefinition -> thingModelValidator.validateThingActionOutput(
685+
optThingDefinition.orElse(null),
686+
sendThingMessageResponse.getMessage().getSubject(),
687+
messageCommandPayload,
688+
sendThingMessageResponse.getResourcePath(),
689+
sendThingMessageResponse.getDittoHeaders()
690+
))
691+
.thenApply(aVoid -> messageCommandResponse);
692+
} else if (messageDirection == MessageDirection.TO &&
693+
messageCommandResponse instanceof SendFeatureMessageResponse<?> sendFeatureMessageResponse) {
694+
final String featureId = sendFeatureMessageResponse.getFeatureId();
695+
return resolveThingAndFeatureDefinition(featureId)
696+
.thenCompose(optDefinitionPair -> thingModelValidator.validateFeatureActionOutput(
697+
optDefinitionPair.first().orElse(null),
698+
optDefinitionPair.second().orElse(null),
699+
featureId,
700+
sendFeatureMessageResponse.getMessage().getSubject(),
701+
messageCommandPayload,
702+
sendFeatureMessageResponse.getResourcePath(),
703+
sendFeatureMessageResponse.getDittoHeaders()
704+
))
705+
.thenApply(aVoid -> messageCommandResponse);
715706
} else {
716707
return CompletableFuture.completedFuture(messageCommandResponse);
717708
}
718709
}
719710

720-
private static boolean isJsonMessageContent(final Message<?> message) {
721-
return message
722-
.getInterpretedContentType()
723-
.filter(ContentType::isJson)
724-
.isPresent();
725-
}
726-
727711
private CompletionStage<Optional<ThingDefinition>> resolveThingDefinition() {
728712
return Patterns.ask(getContext().getParent(), SudoRetrieveThing.of(entityId,
729713
JsonFieldSelector.newInstance("definition"),

0 commit comments

Comments
 (0)
Please sign in to comment.