forked from eclipse-ditto/ditto
-
Notifications
You must be signed in to change notification settings - Fork 7
/
PolicyEntryPlaceholder.java
72 lines (62 loc) · 2.69 KB
/
PolicyEntryPlaceholder.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
/*
* 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);
}
}