-
Notifications
You must be signed in to change notification settings - Fork 214
/
RetrieveThingStrategy.java
168 lines (149 loc) · 7.71 KB
/
RetrieveThingStrategy.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
/*
* 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.Objects;
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.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeadersSettable;
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.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
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.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveWotThingDescriptionResponse;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommand;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.wot.integration.provider.WotThingDescriptionProvider;
import akka.actor.ActorSystem;
/**
* This strategy handles the {@link RetrieveThing} command.
*/
@Immutable
final class RetrieveThingStrategy extends AbstractThingCommandStrategy<RetrieveThing> {
private final WotThingDescriptionProvider wotThingDescriptionProvider;
/**
* Constructs a new {@code RetrieveThingStrategy} object.
*
* @param actorSystem the actor system to use for loading the WoT extension.
*/
RetrieveThingStrategy(final ActorSystem actorSystem) {
super(RetrieveThing.class);
wotThingDescriptionProvider = WotThingDescriptionProvider.get(actorSystem);
}
@Override
public boolean isDefined(final Context<ThingId> context, @Nullable final Thing thing,
final RetrieveThing command) {
final boolean thingExists = Optional.ofNullable(thing)
.map(t -> !t.isDeleted())
.orElse(false);
// when thing is null, there is nothing to retrieve.
final boolean shouldRetrieveDeleted = thing != null && command.getDittoHeaders().shouldRetrieveDeleted();
return Objects.equals(context.getState(), command.getEntityId()) && (thingExists || shouldRetrieveDeleted);
}
@Override
protected Result<ThingEvent<?>> doApply(final Context<ThingId> context,
@Nullable final Thing thing,
final long nextRevision,
final RetrieveThing command,
@Nullable final Metadata metadata) {
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 RetrieveThing 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 ResultFactory.newQueryResult(command,
appendETagHeaderIfProvided(command, getRetrieveThingResponse(thing, command), thing));
}
}
private static DittoHeadersSettable<?> getRetrieveThingResponse(@Nullable final Thing thing,
final ThingQueryCommand<RetrieveThing> command) {
if (thing != null) {
return RetrieveThingResponse.of(command.getEntityId(), getThingJson(thing, command),
command.getDittoHeaders());
} else {
return notAccessible(command);
}
}
private static JsonObject getThingJson(final Thing thing, final ThingQueryCommand<RetrieveThing> command) {
return command.getSelectedFields()
.map(selectedFields -> {
final Features features = thing.getFeatures().orElse(ThingsModelFactory.emptyFeatures());
final JsonFieldSelector expandedFieldSelector =
ThingsModelFactory.expandFeatureIdWildcards(features, selectedFields);
return thing.toJson(command.getImplementedSchemaVersion(), expandedFieldSelector);
})
.orElseGet(() -> thing.toJson(command.getImplementedSchemaVersion()));
}
private CompletionStage<WithDittoHeaders> getRetrieveThingDescriptionResponse(@Nullable final Thing thing,
final RetrieveThing command) {
if (thing != null) {
return wotThingDescriptionProvider
.provideThingTD(thing.getDefinition().orElse(null),
command.getEntityId(),
thing,
command.getDittoHeaders())
.thenApply(wotThingDescription ->
RetrieveWotThingDescriptionResponse.of(command.getEntityId(),
wotThingDescription.toJson(),
command.getDittoHeaders().toBuilder()
.contentType(ContentType.APPLICATION_TD_JSON)
.build()
)
);
} else {
return CompletableFuture.completedStage(notAccessible(command));
}
}
private static ThingNotAccessibleException notAccessible(final ThingQueryCommand<?> command) {
return new ThingNotAccessibleException(command.getEntityId(), command.getDittoHeaders());
}
@Override
public Result<ThingEvent<?>> unhandled(final Context<ThingId> context, @Nullable final Thing thing,
final long nextRevision, final RetrieveThing command) {
return ResultFactory.newErrorResult(
new ThingNotAccessibleException(context.getState(), command.getDittoHeaders()), command);
}
@Override
public Optional<EntityTag> previousEntityTag(final RetrieveThing command, @Nullable final Thing previousEntity) {
return nextEntityTag(command, previousEntity);
}
@Override
public Optional<EntityTag> nextEntityTag(final RetrieveThing command, @Nullable final Thing newEntity) {
return Optional.ofNullable(newEntity).flatMap(EntityTag::fromEntity);
}
}