forked from eclipse-ditto/ditto
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DeactivateTokenIntegrationStrategy.java
114 lines (101 loc) · 5.31 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
105
106
107
108
109
110
111
112
113
114
/*
* 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.Optional;
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.DeactivateTokenIntegration;
import org.eclipse.ditto.signals.commands.policies.actions.DeactivateTokenIntegrationResponse;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyActionFailedException;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyEntryNotAccessibleException;
import org.eclipse.ditto.signals.events.policies.PolicyEvent;
import org.eclipse.ditto.signals.events.policies.SubjectDeleted;
import akka.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<PolicyEvent<?>> 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);
if (optionalEntry.isPresent()) {
final SubjectId subjectId;
final PolicyEntry policyEntry = optionalEntry.get();
try {
subjectId = subjectIdFromActionResolver.resolveSubjectId(policyEntry, command);
} catch (final DittoRuntimeException e) {
return ResultFactory.newErrorResult(e, command);
}
final DeactivateTokenIntegration adjustedCommand =
DeactivateTokenIntegration.of(command.getEntityId(), command.getLabel(), subjectId, dittoHeaders);
final Optional<Subject> subject = policyEntry.getSubjects().getSubject(subjectId);
if (subject.filter(s -> s.getExpiry().isEmpty()).isPresent()) {
// It is forbidden to deactivate a permanent subject.
final DittoRuntimeException error =
PolicyActionFailedException.newBuilderForDeactivatingPermanentSubjects()
.dittoHeaders(dittoHeaders)
.build();
return ResultFactory.newErrorResult(error, command);
} else {
// Expiring subjects are not considered for validation. The result is always valid.
final PolicyEvent<?> event =
SubjectDeleted.of(policyId, label, subjectId, nextRevision, getEventTimestamp(),
dittoHeaders);
final DeactivateTokenIntegrationResponse rawResponse =
DeactivateTokenIntegrationResponse.of(policyId, label, dittoHeaders);
return ResultFactory.newMutationResult(adjustedCommand, event, rawResponse);
}
} else {
// Policy is configured incorrectly
return ResultFactory.newErrorResult(
PolicyEntryNotAccessibleException.newBuilder(policyId, label).dittoHeaders(dittoHeaders).build(),
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();
}
}