-
Notifications
You must be signed in to change notification settings - Fork 214
/
PreEnforcementThingIdCacheLoader.java
87 lines (75 loc) · 3.65 KB
/
PreEnforcementThingIdCacheLoader.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
/*
* Copyright (c) 2022 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.things.service.enforcement.pre;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.utils.cache.entry.Entry;
import org.eclipse.ditto.internal.utils.cacheloaders.ActorAskCacheLoader;
import org.eclipse.ditto.internal.utils.cacheloaders.config.AskWithRetryConfig;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThing;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThingResponse;
import org.eclipse.ditto.things.model.ThingConstants;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingRevision;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Scheduler;
/**
* Cache loader used for Thing existence check in pre-enforcement.
*/
final class PreEnforcementThingIdCacheLoader implements
AsyncCacheLoader<ThingId, Entry<PolicyId>> {
private final ActorAskCacheLoader<PolicyId, Command<?>, ThingId> delegate;
/**
* Constructor.
*
* @param askWithRetryConfig the configuration for the "ask with retry" pattern applied for the cache loader.
* @param scheduler the scheduler to use for the "ask with retry" for retries.
* @param shardRegionProxy the shard-region-proxy.
*/
public PreEnforcementThingIdCacheLoader(final AskWithRetryConfig askWithRetryConfig,
final Scheduler scheduler,
final ActorRef shardRegionProxy) {
delegate = ActorAskCacheLoader.forShard(askWithRetryConfig,
scheduler,
ThingConstants.ENTITY_TYPE,
shardRegionProxy,
thingId -> SudoRetrieveThing.of(thingId, DittoHeaders.empty()),
PreEnforcementThingIdCacheLoader::handleSudoRetrieveThingResponse);
}
@Override
public CompletableFuture<Entry<PolicyId>> asyncLoad(final ThingId key, final Executor executor) {
return delegate.asyncLoad(key, executor);
}
private static Entry<PolicyId> handleSudoRetrieveThingResponse(final Object response) {
if (response instanceof SudoRetrieveThingResponse sudoRetrieveThingResponse) {
final var thing = sudoRetrieveThingResponse.getThing();
final long revision = thing.getRevision().map(ThingRevision::toLong)
.orElseThrow(badThingResponse("no revision"));
final var policyId = thing.getPolicyId().orElseThrow(badThingResponse("no PolicyId"));
return Entry.of(revision, policyId);
} else if (response instanceof ThingNotAccessibleException) {
return Entry.nonexistent();
} else {
throw new IllegalStateException("expect SudoRetrieveThingResponse, got: " + response);
}
}
private static Supplier<RuntimeException> badThingResponse(final String message) {
return () -> new IllegalStateException("Bad SudoRetrieveThingResponse: " + message);
}
}