Skip to content

Commit

Permalink
[#1228] Set originator header for live responses in the smart channel…
Browse files Browse the repository at this point in the history
…; fix unit tests.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Dec 17, 2021
1 parent 7e37968 commit 861e8e5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.eclipse.ditto.concierge.service.actors;

import static org.eclipse.ditto.concierge.service.starter.proxy.DefaultEnforcerActorFactory.setOriginatorHeader;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -114,7 +116,8 @@ private void onAcknowledgements(final Acknowledgements acks) {
acknowledgementReceiver.forward(acks, getContext());
}

private void onCommandResponse(final CommandResponse<?> response) {
private void onCommandResponse(final CommandResponse<?> incomingResponse) {
final CommandResponse<?> response = setOriginatorHeader(incomingResponse);
final boolean validResponse = isValidResponse(response);
logger.debug("Got <{}>, valid=<{}>", response, validResponse);
if (validResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.DittoHeadersBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeadersSettable;
import org.eclipse.ditto.base.model.headers.LiveChannelTimeoutStrategy;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
Expand Down Expand Up @@ -1289,16 +1290,11 @@ private static ThingQueryCommand<?> ensureTwinChannel(final ThingQueryCommand<?>
}

private static DittoHeaders getAdditionalLiveResponseHeaders(final DittoHeaders responseHeaders) {
// TODO: ensure pre-enforcer headers in responses
final var liveChannelConditionMatched = responseHeaders.getOrDefault(
DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(), Boolean.TRUE.toString());
final var dittoHeadersBuilder = DittoHeaders.newBuilder()
final DittoHeadersBuilder<?, ?> dittoHeadersBuilder = DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(), liveChannelConditionMatched)
.responseRequired(false);
responseHeaders.getAuthorizationContext().getFirstAuthorizationSubject()
.map(AuthorizationSubject::toString)
.ifPresent(firstSubject ->
dittoHeadersBuilder.putHeader(DittoHeaderDefinition.ORIGINATOR.getKey(), firstSubject));
return dittoHeadersBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,16 @@ public ActorRef startEnforcerActor(final ActorContext context,
* @param originalSignal A signal with authorization context.
* @return A copy of the signal with the header "ditto-originator" set.
*/
public static DittoHeadersSettable<?> setOriginatorHeader(final DittoHeadersSettable<?> originalSignal) {
@SuppressWarnings("unchecked")
public static <T extends DittoHeadersSettable<?>> T setOriginatorHeader(final T originalSignal) {
final DittoHeaders dittoHeaders = originalSignal.getDittoHeaders();
final AuthorizationContext authorizationContext = dittoHeaders.getAuthorizationContext();
return authorizationContext.getFirstAuthorizationSubject()
.map(AuthorizationSubject::getId)
.map(originatorSubjectId -> DittoHeaders.newBuilder(dittoHeaders)
.putHeader(DittoHeaderDefinition.ORIGINATOR.getKey(), originatorSubjectId)
.build())
.<DittoHeadersSettable<?>>map(originalSignal::setDittoHeaders)
.map(originatorHeader -> (T) originalSignal.setDittoHeaders(originatorHeader))
.orElse(originalSignal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private DittoHeaders headers() {
}

private RetrieveThing getRetrieveThing(final Consumer<DittoHeadersBuilder<?, ?>> headerModifier) {
final DittoHeadersBuilder<?, ?> builder = headers().toBuilder();
final DittoHeadersBuilder<?, ?> builder = headers().toBuilder().randomCorrelationId();
headerModifier.accept(builder);
return RetrieveThing.of(TestSetup.THING_ID, builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void applyReturnsTheSameCommandInstanceWhenNoInlinePolicyIsSpecified() {
@Test
public void applyReturnsTheSameCommandInstanceWhenInvalidInlinePolicyIsSpecified() {
final CreateThing commandWithoutInlinePolicy =
CreateThing.of(THING, JsonObject.newBuilder().set("foo", "bar").build(), DITTO_HEADERS);
CreateThing.of(THING, JsonObject.newBuilder().set("_modified", "bar").build(), DITTO_HEADERS);

final WithDittoHeaders response = applyBlocking(commandWithoutInlinePolicy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void applyReturnsTheSameCommandInstanceWhenNoInlinePolicyIsSpecified() {
@Test
public void applyReturnsTheSameCommandInstanceWhenInvalidInlinePolicyIsSpecified() {
final ModifyThing commandWithoutInlinePolicy =
ModifyThing.of(THING_ID, THING, JsonObject.newBuilder().set("foo", "bar").build(), DITTO_HEADERS);
ModifyThing.of(THING_ID, THING, JsonObject.newBuilder().set("_modified", "bar").build(), DITTO_HEADERS);

final WithDittoHeaders response = applyBlocking(commandWithoutInlinePolicy);

Expand Down

0 comments on commit 861e8e5

Please sign in to comment.