Skip to content

Commit

Permalink
small code format improvements;
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Maute <stefan.maute@bosch.io>
  • Loading branch information
Stefan Maute committed Nov 17, 2021
1 parent e0a8113 commit 73cf923
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private CompletionStage<Contextual<WithDittoHeaders>> enforceLiveCommandResponse
if (responseReceiversOptional.isPresent()) {
final Cache<String, Pair<ActorRef, AuthorizationContext>> responseReceivers =
responseReceiversOptional.get();

return returnCommandResponseContextual(responseReceivers, liveResponse, correlationId, enforcer);
} else {
log().info("Got live response when global dispatching is inactive: <{}> with correlation ID <{}>",
Expand All @@ -206,10 +207,9 @@ private CompletionStage<Contextual<WithDittoHeaders>> returnCommandResponseConte
final Pair<ActorRef, AuthorizationContext> responseReceiver = responseReceiverEntry.get();
final CommandResponse<?> response;
if (liveResponse instanceof ThingQueryCommandResponse) {

final var liveResponseWithRequesterAuthCtx = injectRequestersAuthContext(
(ThingQueryCommandResponse<?>) liveResponse,
responseReceiver.second());
final var liveResponseWithRequesterAuthCtx =
injectRequestersAuthContext((ThingQueryCommandResponse<?>) liveResponse,
responseReceiver.second());

response = ThingCommandEnforcement.buildJsonViewForThingQueryCommandResponse(
liveResponseWithRequesterAuthCtx,
Expand Down Expand Up @@ -250,11 +250,11 @@ private CompletionStage<Contextual<WithDittoHeaders>> enforceLiveSignal(final St
case LIVE_EVENTS:
return enforceLiveEvent(liveSignal, enforcer);
case LIVE_COMMANDS:

ThingCommandEnforcement.authorizeByPolicyOrThrow(enforcer, (ThingCommand<?>) liveSignal);
final ThingCommand<?> withReadSubjects =
addEffectedReadSubjectsToThingLiveSignal((ThingCommand<?>) liveSignal, enforcer);
log(withReadSubjects).info("Live Command was authorized: <{}>", withReadSubjects);

return publishLiveSignal(withReadSubjects, THING_COMMAND_ACK_EXTRACTOR, liveSignalPub.command());
default:
log(liveSignal).warning("Ignoring unsupported command signal: <{}>", liveSignal);
Expand Down Expand Up @@ -297,6 +297,7 @@ private CompletionStage<Contextual<WithDittoHeaders>> enforceLiveEvent(final Sig
log(liveSignal).info("Live Event was authorized: <{}>", liveSignal);
final ThingEvent<?> withReadSubjects =
addEffectedReadSubjectsToThingSignal((ThingEvent<?>) liveSignal, enforcer);

return publishLiveSignal(withReadSubjects, THING_EVENT_ACK_EXTRACTOR, liveSignalPub.event());
} else {
log(liveSignal).info("Live Event was NOT authorized: <{}>", liveSignal);
Expand Down Expand Up @@ -335,8 +336,8 @@ private CompletionStage<Contextual<WithDittoHeaders>> publishMessageCommand(fina
.readGrantedSubjects(effectedSubjects.getGranted())
.readRevokedSubjects(effectedSubjects.getRevoked())
.build();

final MessageCommand<?, ?> withReadSubjects = command.setDittoHeaders(headersWithReadSubjects);

return publishLiveSignal(withReadSubjects, MESSAGE_COMMAND_ACK_EXTRACTOR, liveSignalPub.message());
}

Expand All @@ -350,6 +351,7 @@ private MessageSendNotAllowedException rejectMessageCommand(final MessageCommand
"The command <{}> was not forwarded due to insufficient rights {}: {} - AuthorizationContext: {}",
command.getType(), error.getClass().getSimpleName(), error.getMessage(),
command.getDittoHeaders().getAuthorizationContext());

return error;
}

Expand All @@ -361,6 +363,7 @@ private <T extends Signal<?>, S extends T> CompletionStage<Contextual<WithDittoH

// using pub/sub to publish the command to any interested parties (e.g. a Websocket):
log(signal).debug("Publish message to pub-sub: <{}>", signal);

return addToResponseReceiver(signal).thenApply(newSignal ->
withMessageToReceiver(newSignal, pub.getPublisher(),
obj -> pub.wrapForPublicationWithAcks((S) obj, ackExtractor))
Expand All @@ -385,6 +388,7 @@ private static boolean isAuthorized(final MessageCommand<?, ?> command, final En
private static ResourceKey extractMessageResourceKey(final MessageCommand<?, ?> command) {
try {
final JsonPointer resourcePath = command.getResourcePath();

return PoliciesResourceType.messageResource(resourcePath);
} catch (final IllegalArgumentException e) {
throw MessageFormatInvalidException.newBuilder(JsonFactory.nullArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ private HttpRequest createRequest(final HttpPublishTarget publishTarget, final E
final var requestWithoutEntity = newRequestWithoutEntity(publishTarget, headersPair.first(), message);
final ContentType contentTypeHeader = headersPair.second();
if (contentTypeHeader != null) {
final var httpEntity = HttpEntities.create(contentTypeHeader.contentType(), getPayloadAsBytes(message));
final var httpEntity =
HttpEntities.create(contentTypeHeader.contentType(), getPayloadAsBytes(message));
result = requestWithoutEntity.withEntity(httpEntity);
} else if (message.isTextMessage()) {
result = requestWithoutEntity.withEntity(getTextPayload(message));
Expand Down Expand Up @@ -497,7 +498,8 @@ private CompletionStage<SendResult> toCommandResponseOrAcknowledgement(final Sig
.filter(org.eclipse.ditto.base.model.headers.contenttype.ContentType::isDittoProtocol)
.isPresent();
if (isDittoProtocolMessage && body.isObject()) {
final CommandResponse<?> parsedResponse = toCommandResponse(body.asObject(), targetAuthorizationContext);
final CommandResponse<?> parsedResponse =
toCommandResponse(body.asObject(), targetAuthorizationContext);
if (parsedResponse instanceof Acknowledgement) {
result = parsedResponse;
} else if (SignalInformationPoint.isLiveCommandResponse(parsedResponse)) {
Expand Down Expand Up @@ -564,7 +566,8 @@ private static Optional<SignalWithEntityId<?>> tryToGetAsLiveCommandWithEntityId
.filter(org.eclipse.ditto.base.model.headers.contenttype.ContentType::isDittoProtocol)
.isPresent();
if (isDittoProtocolMessage && jsonValue.isObject()) {
final CommandResponse<?> commandResponse = toCommandResponse(jsonValue.asObject(), targetAuthorizationContext);
final CommandResponse<?> commandResponse =
toCommandResponse(jsonValue.asObject(), targetAuthorizationContext);
if (commandResponse == null) {
return null;
} else if (commandResponse instanceof MessageCommandResponse) {
Expand Down Expand Up @@ -613,7 +616,8 @@ private CommandResponse<?> toLiveCommandResponse(final DittoHeaders dittoHeaders
.filter(org.eclipse.ditto.base.model.headers.contenttype.ContentType::isDittoProtocol)
.isPresent();
if (isDittoProtocolMessage && jsonValue.isObject()) {
final var commandResponse = toCommandResponse(jsonValue.asObject(), targetAuthorizationContext);
final var commandResponse =
toCommandResponse(jsonValue.asObject(), targetAuthorizationContext);
if (commandResponse == null) {
return null;
} else if (commandResponse instanceof ThingCommandResponse &&
Expand Down

0 comments on commit 73cf923

Please sign in to comment.