-
Notifications
You must be signed in to change notification settings - Fork 215
/
RetrieveFeatureStrategy.java
158 lines (140 loc) · 7.48 KB
/
RetrieveFeatureStrategy.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
/*
* Copyright (c) 2017 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.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.apache.pekko.actor.ActorSystem;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.headers.contenttype.ContentType;
import org.eclipse.ditto.base.model.headers.entitytag.EntityTag;
import org.eclipse.ditto.base.model.signals.FeatureToggle;
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.things.model.Feature;
import org.eclipse.ditto.things.model.Features;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeature;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatureResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveWotThingDescriptionResponse;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.wot.integration.provider.WotThingDescriptionProvider;
/**
* This strategy handles the {@link org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeature} command.
*/
@Immutable
final class RetrieveFeatureStrategy extends AbstractThingCommandStrategy<RetrieveFeature> {
private final WotThingDescriptionProvider wotThingDescriptionProvider;
/**
* Constructs a new {@code RetrieveFeatureStrategy} object.
*
* @param actorSystem the actor system to use for loading the WoT extension.
*/
RetrieveFeatureStrategy(final ActorSystem actorSystem) {
super(RetrieveFeature.class);
wotThingDescriptionProvider = WotThingDescriptionProvider.get(actorSystem);
}
@Override
protected Result<ThingEvent<?>> doApply(final Context<ThingId> context,
@Nullable final Thing thing,
final long nextRevision,
final RetrieveFeature command,
@Nullable final Metadata metadata) {
final ThingId thingId = context.getState();
final boolean wotThingDescriptionRequested = command.getDittoHeaders().getAccept()
.filter(ContentType.APPLICATION_TD_JSON.getValue()::equals)
.isPresent();
if (wotThingDescriptionRequested) {
FeatureToggle.checkWotIntegrationFeatureEnabled(command.getType(), command.getDittoHeaders());
try {
// don't apply preconditions and don't provide etag in response
final RetrieveFeature commandWithoutPreconditions = command.setDittoHeaders(
command.getDittoHeaders().toBuilder().removePreconditionHeaders().build());
return ResultFactory.newQueryResult(commandWithoutPreconditions,
getRetrieveThingDescriptionResponse(thing, commandWithoutPreconditions));
} catch (final DittoRuntimeException e) {
return ResultFactory.newErrorResult(e, command);
}
} else {
return extractFeatures(thing)
.map(features -> getFeatureResult(features, thingId, command, thing))
.orElseGet(() -> ResultFactory.newErrorResult(ExceptionFactory.featureNotFound(thingId,
command.getFeatureId(), command.getDittoHeaders()), command));
}
}
private CompletionStage<WithDittoHeaders> getRetrieveThingDescriptionResponse(@Nullable final Thing thing,
final RetrieveFeature command) {
final String featureId = command.getFeatureId();
if (thing != null) {
return thing.getFeatures()
.flatMap(f -> f.getFeature(featureId))
.map(feature -> wotThingDescriptionProvider
.provideFeatureTD(command.getEntityId(), thing, feature, command.getDittoHeaders())
)
.map(tdStage -> tdStage.thenApply(td ->
RetrieveWotThingDescriptionResponse.of(command.getEntityId(), td.toJson(),
command.getDittoHeaders()
.toBuilder()
.contentType(ContentType.APPLICATION_TD_JSON)
.build()
)
).thenApply(WithDittoHeaders.class::cast)
)
.orElseGet(() -> CompletableFuture.completedFuture(ExceptionFactory.featureNotFound(command.getEntityId(), featureId,
command.getDittoHeaders())
));
} else {
return CompletableFuture.completedFuture(
ExceptionFactory.featureNotFound(command.getEntityId(), featureId,
command.getDittoHeaders())
);
}
}
private Optional<Features> extractFeatures(@Nullable final Thing thing) {
return getEntityOrThrow(thing).getFeatures();
}
private Result<ThingEvent<?>> getFeatureResult(final Features features, final ThingId thingId,
final RetrieveFeature command, @Nullable final Thing thing) {
final String featureId = command.getFeatureId();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
return features.getFeature(featureId)
.map(feature -> getFeatureJson(feature, command))
.map(featureJson -> RetrieveFeatureResponse.of(thingId, featureId, featureJson, dittoHeaders))
.<Result<ThingEvent<?>>>map(response ->
ResultFactory.newQueryResult(command, appendETagHeaderIfProvided(command, response, thing)))
.orElseGet(() -> ResultFactory.newErrorResult(
ExceptionFactory.featureNotFound(thingId, featureId, dittoHeaders), command));
}
private static JsonObject getFeatureJson(final Feature feature, final RetrieveFeature command) {
return command.getSelectedFields()
.map(selectedFields -> feature.toJson(command.getImplementedSchemaVersion(), selectedFields))
.orElseGet(() -> feature.toJson(command.getImplementedSchemaVersion()));
}
@Override
public Optional<EntityTag> previousEntityTag(final RetrieveFeature command, @Nullable final Thing previousEntity) {
return nextEntityTag(command, previousEntity);
}
@Override
public Optional<EntityTag> nextEntityTag(final RetrieveFeature command, @Nullable final Thing newEntity) {
return extractFeatures(newEntity)
.flatMap(features -> features.getFeature(command.getFeatureId()))
.flatMap(EntityTag::fromEntity);
}
}