-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractPolicyMappingStrategies.java
269 lines (245 loc) · 9.76 KB
/
AbstractPolicyMappingStrategies.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* 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.protocol.mappingstrategies;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.Map;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.Label;
import org.eclipse.ditto.policies.model.PoliciesModelFactory;
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.PolicyImport;
import org.eclipse.ditto.policies.model.PolicyImports;
import org.eclipse.ditto.policies.model.Resource;
import org.eclipse.ditto.policies.model.ResourceKey;
import org.eclipse.ditto.policies.model.Resources;
import org.eclipse.ditto.policies.model.Subject;
import org.eclipse.ditto.policies.model.SubjectId;
import org.eclipse.ditto.policies.model.Subjects;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.JsonifiableMapper;
import org.eclipse.ditto.protocol.MessagePath;
import org.eclipse.ditto.protocol.TopicPath;
/**
* Provides helper methods to map from {@link Adaptable}s to policy commands.
*
* @param <T> the type of the mapped signals
*/
abstract class AbstractPolicyMappingStrategies<T extends Jsonifiable.WithPredicate<JsonObject, JsonField>>
extends AbstractMappingStrategies<T> {
private static final int RESOURCES_PATH_LEVEL = 2;
private static final int SUBJECT_PATH_LEVEL = 3;
private static final int RESOURCE_PATH_LEVEL = 3;
protected AbstractPolicyMappingStrategies(final Map<String, JsonifiableMapper<T>> mappingStrategies) {
super(mappingStrategies);
}
/**
* Policy id from topic path policy id.
*
* @param topicPath the topic path
* @return the policy id
*/
protected static PolicyId policyIdFromTopicPath(final TopicPath topicPath) {
checkNotNull(topicPath, "topicPath");
return PolicyId.of(topicPath.getNamespace(), topicPath.getEntityName());
}
/**
* Policy from policy.
*
* @param adaptable the adaptable
* @return the policy
*/
protected static Policy policyFrom(final Adaptable adaptable) {
final JsonObject value = getValueFromPayload(adaptable);
return PoliciesModelFactory.newPolicy(value);
}
protected static JsonObject policyJsonFrom(final Adaptable adaptable) {
return adaptable.getPayload()
.getValue()
.filter(JsonValue::isObject)
.map(JsonValue::asObject)
.orElseThrow(
() -> new NullPointerException("Payload value must be a non-null object."));
}
/**
* Policy entry from policy entry.
*
* @param adaptable the adaptable
* @return the policy entry
*/
protected static PolicyEntry policyEntryFrom(final Adaptable adaptable) {
final JsonObject value = getValueFromPayload(adaptable);
return PoliciesModelFactory.newPolicyEntry(labelFrom(adaptable), value);
}
/**
* Policy entries from iterable.
*
* @param adaptable the adaptable
* @return the iterable
*/
protected static Iterable<PolicyEntry> policyEntriesFrom(final Adaptable adaptable) {
final JsonObject value = getValueFromPayload(adaptable);
return PoliciesModelFactory.newPolicyEntries(value);
}
/**
* Policy imports from policy imports.
*
* @param adaptable the adaptable
* @return the policy imports
* @since 3.x.0 TODO ditto#298
*/
protected static PolicyImports policyImportsFrom(final Adaptable adaptable) {
final JsonObject value = getValueFromPayload(adaptable);
return PoliciesModelFactory.newPolicyImports(value);
}
/**
* Policy entry from policy entry.
*
* @param adaptable the adaptable
* @return the policy entry
* @since 3.x.0 TODO ditto#298
*/
protected static PolicyImport policyImportFrom(final Adaptable adaptable) {
final JsonObject value = getValueFromPayload(adaptable);
return PoliciesModelFactory.newPolicyImport(importedPolicyIdFrom(adaptable), value);
}
/**
* Resource from resource.
*
* @param adaptable the adaptable
* @return the resource
*/
static Resource resourceFrom(final Adaptable adaptable) {
return Resource.newInstance(entryResourceKeyFromPath(adaptable.getPayload().getPath()),
getValueFromPayload(adaptable));
}
/**
* Resources from resources.
*
* @param adaptable the adaptable
* @return the resources
*/
protected static Resources resourcesFrom(final Adaptable adaptable) {
return PoliciesModelFactory.newResources(getValueFromPayload(adaptable));
}
/**
* Label from label.
*
* @param adaptable the adaptable
* @return the label
*/
protected static Label labelFrom(final Adaptable adaptable) {
final MessagePath path = adaptable.getPayload().getPath();
return path.getRoot()
.filter(entries -> Policy.JsonFields.ENTRIES.getPointer().equals(entries.asPointer()))
.map(entries -> path.nextLevel())
.flatMap(JsonPointer::getRoot)
.map(JsonKey::toString)
.map(Label::of)
.orElseThrow(() -> JsonParseException.newBuilder().build());
}
/**
* Imported PolicyId.
*
* @param adaptable the adaptable
* @return the imported policyId.
* @since 3.x.0 TODO ditto#298
*/
protected static PolicyId importedPolicyIdFrom(final Adaptable adaptable) {
final MessagePath path = adaptable.getPayload().getPath();
return path.getRoot()
.filter(entries -> Policy.JsonFields.IMPORTS.getPointer().equals(entries.asPointer()))
.map(entries -> path.nextLevel())
.flatMap(JsonPointer::getRoot)
.map(JsonKey::toString)
.map(PolicyId::of)
.orElseThrow(() -> JsonParseException.newBuilder().build());
}
/**
* Entry resource key from path resource key.
*
* @param path the path
* @return the resource key
*/
protected static ResourceKey entryResourceKeyFromPath(final MessagePath path) {
// expected: entries/<entry>/resources/<type:/path1/path2>
return path.getRoot()
.filter(entries -> Policy.JsonFields.ENTRIES.getPointer().equals(entries.asPointer()))
.flatMap(entries -> path.get(RESOURCES_PATH_LEVEL))
.filter(resources -> PolicyEntry.JsonFields.RESOURCES.getPointer().equals(resources.asPointer()))
.flatMap(resources -> path.getSubPointer(RESOURCE_PATH_LEVEL))
.map(PoliciesModelFactory::newResourceKey)
.orElseThrow(() -> JsonParseException.newBuilder().build());
}
/**
* Entry subject id from path subject id.
*
* @param path the path
* @return the subject id
*/
protected static SubjectId entrySubjectIdFromPath(final MessagePath path) {
// expected: entries/<entry>/resources/<issuer:subject>
return path.getRoot()
.filter(entries -> Policy.JsonFields.ENTRIES.getPointer().equals(entries.asPointer()))
.flatMap(entries -> path.get(RESOURCES_PATH_LEVEL))
.filter(resources -> PolicyEntry.JsonFields.SUBJECTS.getPointer().equals(resources.asPointer()))
.flatMap(resources -> path.getSubPointer(SUBJECT_PATH_LEVEL))
.map(AbstractPolicyMappingStrategies::stripLeadingSlash)
.map(PoliciesModelFactory::newSubjectId)
.orElseThrow(() -> JsonParseException.newBuilder().build());
}
/**
* Subject from subject.
*
* @param adaptable the adaptable
* @return the subject
*/
protected static Subject subjectFrom(final Adaptable adaptable) {
final SubjectId subjectIssuerWithId = entrySubjectIdFromPath(adaptable.getPayload().getPath());
final JsonObject value = getValueFromPayload(adaptable);
return PoliciesModelFactory.newSubject(subjectIssuerWithId, value);
}
/**
* Subjects from subjects.
*
* @param adaptable the adaptable
* @return the subjects
*/
protected static Subjects subjectsFrom(final Adaptable adaptable) {
final JsonObject value = getValueFromPayload(adaptable);
return PoliciesModelFactory.newSubjects(value);
}
/**
* @throws NullPointerException if the value is null.
*/
protected static JsonObject getValueFromPayload(final Adaptable adaptable) {
final JsonValue value = adaptable.getPayload().getValue()
.filter(JsonValue::isObject)
.orElseThrow(() -> new NullPointerException("Payload value must be a non-null object."));
return value.asObject();
}
private static CharSequence stripLeadingSlash(final CharSequence charSequence) {
if (charSequence.length() == 0 || charSequence.charAt(0) != '/') {
return charSequence;
} else {
return charSequence.subSequence(1, charSequence.length());
}
}
}