forked from eclipse-ditto/ditto
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DeactivatePolicyTokenIntegrationStrategy.java
118 lines (107 loc) · 5.35 KB
/
DeactivatePolicyTokenIntegrationStrategy.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (c) 2020 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.services.policies.persistence.actors.strategies.commands;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.entity.metadata.Metadata;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.entitytag.EntityTag;
import org.eclipse.ditto.model.policies.Label;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.model.policies.PolicyEntry;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.model.policies.Subject;
import org.eclipse.ditto.model.policies.SubjectId;
import org.eclipse.ditto.services.policies.common.config.PolicyConfig;
import org.eclipse.ditto.services.utils.persistentactors.results.Result;
import org.eclipse.ditto.services.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.signals.commands.policies.actions.DeactivatePolicyTokenIntegration;
import org.eclipse.ditto.signals.commands.policies.actions.DeactivatePolicyTokenIntegrationResponse;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyActionFailedException;
import org.eclipse.ditto.signals.events.policies.PolicyEvent;
import org.eclipse.ditto.signals.events.policies.SubjectsDeletedPartially;
import akka.actor.ActorSystem;
/**
* This strategy handles the {@link DeactivatePolicyTokenIntegration} command.
*/
final class DeactivatePolicyTokenIntegrationStrategy
extends AbstractPolicyActionCommandStrategy<DeactivatePolicyTokenIntegration> {
DeactivatePolicyTokenIntegrationStrategy(final PolicyConfig policyConfig, final ActorSystem system) {
super(DeactivatePolicyTokenIntegration.class, policyConfig, system);
}
@Override
protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
@Nullable final Policy policy,
final long nextRevision,
final DeactivatePolicyTokenIntegration command,
@Nullable final Metadata metadata) {
final Policy nonNullPolicy = checkNotNull(policy, "policy");
final PolicyId policyId = context.getState();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
final List<PolicyEntry> entries = command.getLabels()
.stream()
.map(nonNullPolicy::getEntryFor)
.flatMap(Optional::stream)
.collect(Collectors.toList());
if (entries.isEmpty() || entries.size() != command.getLabels().size()) {
// Command is constructed incorrectly. This is a bug.
return ResultFactory.newErrorResult(
PolicyActionFailedException.newBuilderForDeactivateTokenIntegration().build(), command);
}
final Map<Label, SubjectId> deactivatedSubjectsIds = new HashMap<>();
for (final PolicyEntry entry : entries) {
final SubjectId subjectId;
try {
subjectId = subjectIdFromActionResolver.resolveSubjectId(entry, command);
} catch (final DittoRuntimeException e) {
return ResultFactory.newErrorResult(e, command);
}
final Optional<Subject> optionalSubject = entry.getSubjects().getSubject(subjectId);
if (optionalSubject.isPresent()) {
if (optionalSubject.get().getExpiry().isEmpty()) {
return ResultFactory.newErrorResult(
PolicyActionFailedException.newBuilderForDeactivatingPermanentSubjects()
.dittoHeaders(dittoHeaders)
.build(),
command);
}
deactivatedSubjectsIds.put(entry.getLabel(), subjectId);
}
}
// Validation is not necessary because temporary subjects do not affect validity
final PolicyEvent<?> event =
SubjectsDeletedPartially.of(policyId, deactivatedSubjectsIds, nextRevision, getEventTimestamp(),
dittoHeaders);
final DeactivatePolicyTokenIntegrationResponse rawResponse =
DeactivatePolicyTokenIntegrationResponse.of(policyId, dittoHeaders);
return ResultFactory.newMutationResult(command, event, rawResponse);
}
@Override
public Optional<EntityTag> previousEntityTag(final DeactivatePolicyTokenIntegration command,
@Nullable final Policy previousEntity) {
// activated subjects do not support entity tag
return Optional.empty();
}
@Override
public Optional<EntityTag> nextEntityTag(final DeactivatePolicyTokenIntegration command,
@Nullable final Policy newEntity) {
// activated subjects do not support entity tag
return Optional.empty();
}
}