-
Notifications
You must be signed in to change notification settings - Fork 214
/
SudoSignalEnrichmentFacade.java
98 lines (82 loc) · 3.91 KB
/
SudoSignalEnrichmentFacade.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.streaming;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.internal.models.signalenrichment.SignalEnrichmentFacade;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThing;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThingResponse;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.pattern.Patterns;
/**
* Sudo-retrieve things by asking an actor.
*/
final class SudoSignalEnrichmentFacade implements SignalEnrichmentFacade {
private final ActorRef commandHandler;
private final Duration askTimeout;
private SudoSignalEnrichmentFacade(final ActorRef commandHandler, final Duration askTimeout) {
this.commandHandler = checkNotNull(commandHandler, "commandHandler");
this.askTimeout = checkNotNull(askTimeout, "askTimeout");
}
static SudoSignalEnrichmentFacade of(final ActorRef commandHandler, final Duration askTimeout) {
return new SudoSignalEnrichmentFacade(commandHandler, askTimeout);
}
@Override
public CompletionStage<JsonObject> retrievePartialThing(final ThingId thingId,
@Nullable final JsonFieldSelector jsonFieldSelector,
final DittoHeaders dittoHeaders,
@Nullable final Signal<?> concernedSignal) {
final DittoHeaders headersWithCorrelationId;
if (dittoHeaders.getCorrelationId().isEmpty()) {
headersWithCorrelationId = dittoHeaders.toBuilder().randomCorrelationId().build();
} else {
headersWithCorrelationId = dittoHeaders;
}
final SudoRetrieveThing command;
if (jsonFieldSelector == null) {
command = SudoRetrieveThing.of(thingId, headersWithCorrelationId);
} else {
command = SudoRetrieveThing.of(thingId, jsonFieldSelector, headersWithCorrelationId);
}
return Patterns.ask(commandHandler, command, askTimeout).thenCompose(SudoSignalEnrichmentFacade::extractThing);
}
private static CompletionStage<JsonObject> extractThing(final Object object) {
if (object instanceof SudoRetrieveThingResponse) {
final SudoRetrieveThingResponse response = (SudoRetrieveThingResponse) object;
return CompletableFuture.completedFuture(response.getEntity(JsonSchemaVersion.LATEST));
} else if (object instanceof ThingNotAccessibleException) {
return CompletableFuture.completedFuture(JsonObject.empty());
} else {
final CompletableFuture<JsonObject> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(toThrowable(object));
return failedFuture;
}
}
private static Throwable toThrowable(final Object object) {
if (object instanceof Throwable) {
return (Throwable) object;
} else {
return new IllegalStateException("Unexpected message: " + object);
}
}
}