forked from eclipse-ditto/ditto
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-ditto#926] add command and event strategies for ActivateSubj…
…ect. Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
- Loading branch information
Showing
12 changed files
with
492 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...lipse/ditto/services/policies/persistence/actors/placeholders/PolicyEntryPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.placeholders; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.ditto.model.placeholders.Placeholder; | ||
import org.eclipse.ditto.model.placeholders.PlaceholderFactory; | ||
import org.eclipse.ditto.model.placeholders.UnresolvedPlaceholderException; | ||
import org.eclipse.ditto.model.policies.PolicyEntry; | ||
import org.eclipse.ditto.model.policies.SubjectId; | ||
|
||
/** | ||
* Placeholder with prefix policy-entry. | ||
*/ | ||
public final class PolicyEntryPlaceholder implements Placeholder<PolicyEntry> { | ||
|
||
private static PolicyEntryPlaceholder INSTANCE = new PolicyEntryPlaceholder(); | ||
|
||
private static final String PREFIX = "policy-entry"; | ||
private static final String LABEL = "label"; | ||
|
||
/** | ||
* Resolve a subject ID containing policy-entry placeholders. | ||
* | ||
* @param entry the policy entry. | ||
* @param subjectIdWithPlaceholder the subject ID containing placeholders. | ||
* @return the subject ID after resolution, or an empty optional if it contains an unresolvable placeholder. | ||
* @throws org.eclipse.ditto.model.placeholders.UnresolvedPlaceholderException if the subject ID contains unsupported placeholders. | ||
* @throws org.eclipse.ditto.model.policies.SubjectIdInvalidException if the resolved subject ID is invalid. | ||
*/ | ||
public static SubjectId resolveSubjectId(final PolicyEntry entry, | ||
final SubjectId subjectIdWithPlaceholder) { | ||
return PlaceholderFactory.newExpressionResolver(PlaceholderFactory.newPlaceholderResolver(INSTANCE, entry)) | ||
.resolve(subjectIdWithPlaceholder.toString()) | ||
.toOptional() | ||
.map(SubjectId::newInstance) | ||
.orElseThrow(() -> | ||
UnresolvedPlaceholderException.newBuilder(subjectIdWithPlaceholder.toString()).build()); | ||
} | ||
|
||
@Override | ||
public Optional<String> resolve(final PolicyEntry policyEntry, final String name) { | ||
return supports(name) ? Optional.of(policyEntry.getLabel().toString()) : Optional.empty(); | ||
} | ||
|
||
@Override | ||
public String getPrefix() { | ||
return PREFIX; | ||
} | ||
|
||
@Override | ||
public List<String> getSupportedNames() { | ||
return List.of(LABEL); | ||
} | ||
|
||
@Override | ||
public boolean supports(final String name) { | ||
return LABEL.equals(name); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ava/org/eclipse/ditto/services/policies/persistence/actors/placeholders/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* 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 | ||
*/ | ||
@org.eclipse.ditto.utils.jsr305.annotations.AllParametersAndReturnValuesAreNonnullByDefault | ||
package org.eclipse.ditto.services.policies.persistence.actors.placeholders; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...tto/services/policies/persistence/actors/strategies/commands/ActivateSubjectStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2019 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.SubjectExpiry; | ||
import org.eclipse.ditto.model.policies.SubjectId; | ||
import org.eclipse.ditto.services.models.policies.PoliciesValidator; | ||
import org.eclipse.ditto.services.policies.common.config.PolicyConfig; | ||
import org.eclipse.ditto.services.policies.persistence.actors.placeholders.PolicyEntryPlaceholder; | ||
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.modify.ActivateSubject; | ||
import org.eclipse.ditto.signals.commands.policies.modify.ActivateSubjectResponse; | ||
import org.eclipse.ditto.signals.events.policies.PolicyEvent; | ||
import org.eclipse.ditto.signals.events.policies.SubjectActivated; | ||
|
||
/** | ||
* This strategy handles the {@link org.eclipse.ditto.signals.commands.policies.modify.ActivateSubject} command. | ||
*/ | ||
final class ActivateSubjectStrategy extends AbstractPolicyCommandStrategy<ActivateSubject> { | ||
|
||
ActivateSubjectStrategy(final PolicyConfig policyConfig) { | ||
super(ActivateSubject.class, policyConfig); | ||
} | ||
|
||
@Override | ||
protected Result<PolicyEvent> doApply(final Context<PolicyId> context, | ||
@Nullable final Policy policy, | ||
final long nextRevision, | ||
final ActivateSubject command, | ||
@Nullable final Metadata metadata) { | ||
|
||
final Policy nonNullPolicy = checkNotNull(policy, "policy"); | ||
final PolicyId policyId = context.getState(); | ||
final Label label = command.getLabel(); | ||
final SubjectExpiry commandSubjectExpiry = SubjectExpiry.newInstance(command.getExpiry()); | ||
final DittoHeaders dittoHeaders = command.getDittoHeaders(); | ||
|
||
final Optional<PolicyEntry> optionalEntry = nonNullPolicy.getEntryFor(label); | ||
if (optionalEntry.isPresent()) { | ||
final SubjectId subjectId; | ||
try { | ||
subjectId = PolicyEntryPlaceholder.resolveSubjectId(optionalEntry.get(), command.getSubjectId()); | ||
} catch (final DittoRuntimeException e) { | ||
return ResultFactory.newErrorResult(e, command); | ||
} | ||
final Subject subject = Subject.newInstance(subjectId, TOKEN_INTEGRATION, commandSubjectExpiry); | ||
final Subject adjustedSubject = potentiallyAdjustSubject(subject); | ||
final ActivateSubject adjustedCommand = ActivateSubject.of( | ||
command.getEntityId(), command.getLabel(), adjustedSubject.getId(), | ||
adjustedSubject.getExpiry().orElseThrow().getTimestamp(), dittoHeaders); | ||
|
||
// Validation is necessary because activation may add expiry to the policy admin subject. | ||
final Policy newPolicy = nonNullPolicy.setSubjectFor(label, adjustedSubject); | ||
final PoliciesValidator validator = PoliciesValidator.newInstance(newPolicy); | ||
if (validator.isValid()) { | ||
final PolicyEvent<?> event = | ||
SubjectActivated.of(policyId, label, adjustedSubject, nextRevision, getEventTimestamp(), | ||
dittoHeaders); | ||
final ActivateSubjectResponse rawResponse = | ||
ActivateSubjectResponse.of(policyId, label, adjustedSubject.getId(), dittoHeaders); | ||
// do not append ETag - activated subjects do not support ETags. | ||
return ResultFactory.newMutationResult(adjustedCommand, event, rawResponse); | ||
} else { | ||
return ResultFactory.newErrorResult( | ||
policyEntryInvalid(policyId, label, validator.getReason().orElse(null), dittoHeaders), | ||
command); | ||
} | ||
} else { | ||
return ResultFactory.newErrorResult(policyEntryNotFound(policyId, label, dittoHeaders), command); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<EntityTag> previousEntityTag(final ActivateSubject command, @Nullable final Policy previousEntity) { | ||
// activated subjects do not support entity tag | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<EntityTag> nextEntityTag(final ActivateSubject command, @Nullable final Policy newEntity) { | ||
// activated subjects do not support entity tag | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...itto/services/policies/persistence/actors/strategies/events/SubjectActivatedStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.events; | ||
|
||
import org.eclipse.ditto.model.policies.PolicyBuilder; | ||
import org.eclipse.ditto.signals.events.policies.SubjectActivated; | ||
|
||
/** | ||
* This strategy handles {@link org.eclipse.ditto.signals.events.policies.SubjectActivated} events. | ||
*/ | ||
final class SubjectActivatedStrategy extends AbstractPolicyEventStrategy<SubjectActivated> { | ||
|
||
@Override | ||
protected PolicyBuilder applyEvent(final SubjectActivated sm, final PolicyBuilder policyBuilder) { | ||
return policyBuilder.setSubjectFor(sm.getLabel(), sm.getSubject()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.