-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractThingCommandStrategy.java
245 lines (209 loc) · 11.6 KB
/
AbstractThingCommandStrategy.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
/*
* 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.things.service.persistence.actors.strategies.commands;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.entity.metadata.MetadataBuilder;
import org.eclipse.ditto.base.model.entity.metadata.MetadataModelFactory;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.WithOptionalEntity;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.utils.headers.conditional.ConditionalHeadersValidator;
import org.eclipse.ditto.internal.utils.persistentactors.MetadataFromSignal;
import org.eclipse.ditto.internal.utils.persistentactors.etags.AbstractConditionHeaderCheckingCommandStrategy;
import org.eclipse.ditto.internal.utils.persistentactors.results.Result;
import org.eclipse.ditto.internal.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommand;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
/**
* Abstract base class for {@link org.eclipse.ditto.things.model.signals.commands.ThingCommand} strategies.
*
* @param <C> the type of the handled command - of type {@code Command} as also
* {@link org.eclipse.ditto.things.api.commands.sudo.SudoCommand} are handled which are no ThingCommands.
*/
@Immutable
abstract class AbstractThingCommandStrategy<C extends Command<C>>
extends AbstractConditionHeaderCheckingCommandStrategy<C, Thing, ThingId, ThingEvent<?>> {
private static final ConditionalHeadersValidator VALIDATOR =
ThingsConditionalHeadersValidatorProvider.getInstance();
protected AbstractThingCommandStrategy(final Class<C> theMatchingClass) {
super(theMatchingClass);
}
@Override
protected ConditionalHeadersValidator getValidator() {
return VALIDATOR;
}
/**
* Execute a command strategy after it is determined applicable.
*
* @param context context of the persistent actor.
* @param entity entity of the persistent actor.
* @param nextRevision the next revision to allocate to events.
* @param command the incoming command.
* @return result of the command strategy.
*/
@Override
public Result<ThingEvent<?>> apply(final Context<ThingId> context, @Nullable final Thing entity,
final long nextRevision, final C command) {
final var dittoHeaders = command.getDittoHeaders();
final var dittoHeadersBuilder = dittoHeaders.toBuilder();
final var loggerWithCorrelationId = context.getLog().withCorrelationId(command);
final var thingConditionFailed = dittoHeaders.getCondition()
.flatMap(condition -> ThingConditionValidator.validate(command, condition, entity));
final Boolean liveChannelConditionPassed = dittoHeaders.getLiveChannelCondition()
.map(condition -> ThingConditionValidator.validate(command, condition, entity).isEmpty())
.orElse(false);
MetadataHeaderChecker.check(command, dittoHeaders);
final Result<ThingEvent<?>> result;
if (command instanceof ThingQueryCommand<?> &&
!dittoHeaders.getMetadataFieldsToGet().isEmpty()) {
final Optional<Metadata> optionalMetadata = calculateMetadataForGetRequests(entity, command);
dittoHeadersBuilder.putHeader(DittoHeaderDefinition.DITTO_METADATA.getKey(),
optionalMetadata.isPresent() ? optionalMetadata.get().toString() :
JsonObject.empty().toString());
}
if (thingConditionFailed.isPresent()) {
final var conditionFailedException = thingConditionFailed.get();
loggerWithCorrelationId.debug("Validating condition failed with exception <{}>.",
conditionFailedException.getMessage());
result = ResultFactory.newErrorResult(conditionFailedException, command);
} else if (dittoHeaders.getLiveChannelCondition().isPresent()) {
dittoHeadersBuilder.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(),
liveChannelConditionPassed.toString());
result = super.apply(context, entity, nextRevision, command.setDittoHeaders(dittoHeadersBuilder.build()));
} else {
result = super.apply(context, entity, nextRevision, command.setDittoHeaders(dittoHeadersBuilder.build()));
}
return result;
}
@Override
protected Optional<Metadata> calculateRelativeMetadata(@Nullable final Thing entity, final C command) {
final DittoHeaders dittoHeaders = command.getDittoHeaders();
final Metadata existingRelativeMetadata = getExistingMetadata(entity, command);
if (command instanceof WithOptionalEntity withOptionalEntity &&
!dittoHeaders.getMetadataHeadersToPut().isEmpty()) {
final MetadataFromSignal relativeMetadata =
MetadataFromSignal.of(command, withOptionalEntity, existingRelativeMetadata);
return Optional.ofNullable(relativeMetadata.get());
} else if (command instanceof ThingModifyCommand<?> && !dittoHeaders.getMetadataFieldsToDelete().isEmpty()) {
return calculateMetadataForDeleteMetadataRequests(entity, command);
}
return Optional.empty();
}
private Optional<Metadata> calculateMetadataForGetRequests(@Nullable Thing entity, final C command) {
final Metadata existingMetadata = getExistingMetadata(entity, command);
final Set<JsonPointer> metadataFields = command.getDittoHeaders().getMetadataFieldsToGet();
final Set<JsonPointer> metadataFieldsWithResolvedWildcard;
if (containsExactlySingleWildcard(metadataFields) && existingMetadata != null) {
return Optional.of(existingMetadata);
}
if (checkIfContainsWildcards(metadataFields) && entity != null) {
metadataFields.stream()
.filter(this::checkIfContainsWildcard)
.forEach(jsonPointer -> MetadataWildcardValidator.validateMetadataWildcard(command.getType(),
jsonPointer.toString(), DittoHeaderDefinition.GET_METADATA.getKey()));
metadataFieldsWithResolvedWildcard =
expandWildcardsInMetadataExpression(metadataFields, entity, command,
DittoHeaderDefinition.GET_METADATA.getKey());
} else {
metadataFieldsWithResolvedWildcard = metadataFields;
}
final MetadataBuilder metadataBuilder = Metadata.newBuilder();
if (existingMetadata != null && !metadataFieldsWithResolvedWildcard.isEmpty()) {
metadataFieldsWithResolvedWildcard.forEach(metadataFieldPointer -> {
final Optional<JsonValue> metadataValue = existingMetadata.getValue(metadataFieldPointer);
metadataValue.ifPresent(
jsonValue -> metadataBuilder.set(metadataFieldPointer.toString(), jsonValue));
});
return Optional.of(metadataBuilder.build());
}
return Optional.empty();
}
private Optional<Metadata> calculateMetadataForDeleteMetadataRequests(@Nullable Thing entity, final C command) {
final Metadata existingMetadata = getExistingMetadata(entity, command);
final Set<JsonPointer> metadataFieldsToDelete = command.getDittoHeaders().getMetadataFieldsToDelete();
if (containsExactlySingleWildcard(metadataFieldsToDelete) && existingMetadata != null) {
// delete all metadata
return Optional.of(MetadataModelFactory.emptyMetadata());
}
final Set<JsonPointer> metadataFieldsWithResolvedWildcard;
if (checkIfContainsWildcards(metadataFieldsToDelete) && entity != null) {
metadataFieldsToDelete.stream()
.filter(this::checkIfContainsWildcard)
.forEach(jsonPointer -> MetadataWildcardValidator.validateMetadataWildcard(command.getType(),
jsonPointer.toString(), DittoHeaderDefinition.DELETE_METADATA.getKey()));
metadataFieldsWithResolvedWildcard =
expandWildcardsInMetadataExpression(metadataFieldsToDelete, entity, command,
DittoHeaderDefinition.DELETE_METADATA.getKey());
} else {
metadataFieldsWithResolvedWildcard = metadataFieldsToDelete;
}
if (existingMetadata != null && !metadataFieldsWithResolvedWildcard.isEmpty()) {
return deleteMetadata(existingMetadata, metadataFieldsWithResolvedWildcard);
}
return Optional.empty();
}
private Optional<Metadata> deleteMetadata(final Metadata existingMetadata,
final Set<JsonPointer> metadataFieldsToDelete) {
final MetadataBuilder metadataBuilder = existingMetadata.toBuilder();
metadataFieldsToDelete.forEach(metadataBuilder::remove);
return Optional.of(metadataBuilder.build());
}
@Override
public boolean isDefined(final C command) {
return command instanceof ThingCommand;
}
@Nullable
private Metadata getExistingMetadata(@Nullable final Thing entity, final C command) {
return Optional.ofNullable(entity)
.flatMap(Thing::getMetadata)
.flatMap(m -> m.getValue(command.getResourcePath()))
.filter(JsonValue::isObject)
.map(JsonValue::asObject)
.map(Metadata::newMetadata)
.orElse(null);
}
private boolean checkIfContainsWildcards(final Set<JsonPointer> jsonPointers) {
return jsonPointers.stream().anyMatch(this::checkIfContainsWildcard);
}
private boolean checkIfContainsWildcard(final JsonPointer jsonPointer) {
return jsonPointer.toString().contains("/*/");
}
private boolean containsExactlySingleWildcard(final Set<JsonPointer> jsonPointers) {
return jsonPointers.size() == 1 && jsonPointers.contains(JsonPointer.of("*"));
}
private Set<JsonPointer> expandWildcardsInMetadataExpression(final Set<JsonPointer> metadataPointerWithWildcard,
final Thing entity, final C command, final String headerKey) {
final Set<JsonPointer> resolvedMetadataPointers = new HashSet<>();
metadataPointerWithWildcard.stream()
.filter(jsonPointer -> !checkIfContainsWildcard(jsonPointer))
.forEach(resolvedMetadataPointers::add);
metadataPointerWithWildcard.stream()
.filter(this::checkIfContainsWildcard)
.map(jsonPointer -> MetadataFieldsWildcardResolver.resolve(command, entity, jsonPointer, headerKey))
.forEach(resolvedMetadataPointers::addAll);
return resolvedMetadataPointers;
}
}