-
Notifications
You must be signed in to change notification settings - Fork 214
/
DeactivateTokenIntegrationStrategy.java
104 lines (91 loc) · 4.7 KB
/
DeactivateTokenIntegrationStrategy.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
99
100
101
102
103
104
/*
* 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.policies.service.persistence.actors.strategies.commands;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.entitytag.EntityTag;
import org.eclipse.ditto.internal.utils.persistentactors.results.Result;
import org.eclipse.ditto.internal.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.policies.model.Label;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.SubjectId;
import org.eclipse.ditto.policies.model.signals.commands.actions.DeactivateTokenIntegration;
import org.eclipse.ditto.policies.model.signals.commands.actions.DeactivateTokenIntegrationResponse;
import org.eclipse.ditto.policies.model.signals.events.PolicyActionEvent;
import org.eclipse.ditto.policies.model.signals.events.SubjectDeleted;
import org.eclipse.ditto.policies.model.signals.events.SubjectsDeletedPartially;
import org.eclipse.ditto.policies.service.common.config.PolicyConfig;
import org.apache.pekko.actor.ActorSystem;
/**
* This strategy handles the {@link DeactivateTokenIntegration} command.
*/
final class DeactivateTokenIntegrationStrategy
extends AbstractPolicyActionCommandStrategy<DeactivateTokenIntegration> {
DeactivateTokenIntegrationStrategy(final PolicyConfig policyConfig, final ActorSystem system) {
super(DeactivateTokenIntegration.class, policyConfig, system);
}
@Override
protected Result<PolicyActionEvent<?>> doApply(final Context<PolicyId> context,
@Nullable final Policy policy,
final long nextRevision,
final DeactivateTokenIntegration command,
@Nullable final Metadata metadata) {
final Policy nonNullPolicy = checkNotNull(policy, "policy");
final PolicyId policyId = context.getState();
final Label label = command.getLabel();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
final Optional<PolicyEntry> optionalEntry = nonNullPolicy.getEntryFor(label)
.filter(entry -> command.isApplicable(entry, dittoHeaders.getAuthorizationContext()));
if (optionalEntry.isPresent()) {
final PolicyEntry policyEntry = optionalEntry.get();
final Set<SubjectId> subjectIds = subjectIdFromActionResolver.resolveSubjectIds(policyEntry, command);
final DeactivateTokenIntegration adjustedCommand =
DeactivateTokenIntegration.of(command.getEntityId(), command.getLabel(), subjectIds, dittoHeaders);
final PolicyActionEvent<?> event;
final Instant eventTimestamp = getEventTimestamp();
if (subjectIds.size() == 1) {
final SubjectId subjectId = subjectIds.stream().findFirst().orElseThrow();
event = SubjectDeleted.of(policyId, label, subjectId, nextRevision, eventTimestamp, dittoHeaders,
metadata);
} else {
event = SubjectsDeletedPartially.of(policyId, Map.of(label, subjectIds), nextRevision, eventTimestamp,
dittoHeaders, metadata);
}
final DeactivateTokenIntegrationResponse rawResponse =
DeactivateTokenIntegrationResponse.of(policyId, label, dittoHeaders);
return ResultFactory.newMutationResult(adjustedCommand, event, rawResponse);
} else {
return ResultFactory.newErrorResult(command.getNotApplicableException(dittoHeaders), command);
}
}
@Override
public Optional<EntityTag> previousEntityTag(final DeactivateTokenIntegration command,
@Nullable final Policy previousEntity) {
// activated subjects do not support entity tag
return Optional.empty();
}
@Override
public Optional<EntityTag> nextEntityTag(final DeactivateTokenIntegration command,
@Nullable final Policy newEntity) {
// activated subjects do not support entity tag
return Optional.empty();
}
}