/
RetrieveThingsResponse.java
executable file
·318 lines (281 loc) · 13.5 KB
/
RetrieveThingsResponse.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.signals.commands.things.query;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.exceptions.DittoJsonException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingsModelFactory;
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
import org.eclipse.ditto.signals.commands.base.CommandResponseJsonDeserializer;
import org.eclipse.ditto.signals.commands.base.WithNamespace;
/**
* Response to a {@link RetrieveThings} command.
*/
@Immutable
public final class RetrieveThingsResponse extends AbstractCommandResponse<RetrieveThingsResponse> implements
ThingQueryCommandResponse<RetrieveThingsResponse>, WithNamespace {
/**
* Type of this response.
*/
public static final String TYPE = TYPE_PREFIX + RetrieveThings.NAME;
static final JsonFieldDefinition<JsonArray> JSON_THINGS =
JsonFactory.newJsonArrayFieldDefinition("things", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
static final JsonFieldDefinition<String> JSON_THINGS_PLAIN_JSON =
JsonFactory.newStringFieldDefinition("thingsPlainJson", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
static final JsonFieldDefinition<String> JSON_NAMESPACE =
JsonFactory.newStringFieldDefinition("namespace", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
private static final String PROPERTY_NAME_THINGS = "Things";
private final String thingsPlainJson;
@Nullable private final String namespace;
@Nullable private JsonArray things;
private RetrieveThingsResponse(final HttpStatusCode statusCode, @Nullable final JsonArray things,
final String thingsPlainJson, @Nullable final String namespace, final DittoHeaders dittoHeaders) {
super(TYPE, statusCode, dittoHeaders);
this.thingsPlainJson = checkNotNull(thingsPlainJson, "Things plain JSON");
this.namespace = namespace;
this.things = things;
}
/**
* Creates a response to a {@link RetrieveThings} command.
*
* @param thingsPlainJson the retrieved Things.
* @param namespace the namespace of this search request
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
*/
public static RetrieveThingsResponse of(final List<String> thingsPlainJson, @Nullable final String namespace,
final DittoHeaders dittoHeaders) {
return new RetrieveThingsResponse(HttpStatusCode.OK, null, thingsPlainJson.stream()
.collect(Collectors.joining(",", "[", "]")), namespace, dittoHeaders);
}
/**
* Creates a response to a {@link RetrieveThings} command.
*
* @param thingsPlainJson the retrieved Things.
* @param namespace the namespace of this search request
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
*/
public static RetrieveThingsResponse of(final String thingsPlainJson, @Nullable final String namespace,
final DittoHeaders dittoHeaders) {
return new RetrieveThingsResponse(HttpStatusCode.OK, null, thingsPlainJson, namespace, dittoHeaders);
}
/**
* Creates a response to a {@link RetrieveThings} command.
*
* @param things the retrieved Things.
* @param namespace the namespace of this search request
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
*/
public static RetrieveThingsResponse of(final JsonArray things, @Nullable final String namespace,
final DittoHeaders dittoHeaders) {
return new RetrieveThingsResponse(HttpStatusCode.OK, things, things.toString(), namespace, dittoHeaders);
}
/**
* Creates a response to a {@link RetrieveThings} command.
*
* @param things the retrieved Things.
* @param predicate the predicate to apply to the things when transforming to JSON.
* @param namespace the namespace of this search request
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
*/
public static RetrieveThingsResponse of(final List<Thing> things, final Predicate<JsonField> predicate,
@Nullable final String namespace, final DittoHeaders dittoHeaders) {
final JsonArray thingsArray = checkNotNull(things, PROPERTY_NAME_THINGS).stream()
.map(thing -> thing.toJson(dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST),
predicate))
.collect(JsonCollectors.valuesToArray());
return new RetrieveThingsResponse(HttpStatusCode.OK, thingsArray, thingsArray.toString(), namespace, dittoHeaders);
}
/**
* Creates a response to a {@link RetrieveThings} command.
*
* @param things the retrieved Things.
* @param fieldSelector the JsonFieldSelector to apply to the passed things when transforming to JSON.
* @param predicate the predicate to apply to the things when transforming to JSON.
* @param namespace the namespace of this retrieve things request
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if any argument is {@code null}.
*/
public static RetrieveThingsResponse of(final List<Thing> things, @Nullable final JsonFieldSelector fieldSelector,
@Nullable final Predicate<JsonField> predicate, @Nullable final String namespace, final DittoHeaders dittoHeaders) {
final JsonArray thingsArray = checkNotNull(things, PROPERTY_NAME_THINGS).stream()
.map(thing -> getJsonFields(fieldSelector, predicate, dittoHeaders, thing))
.collect(JsonCollectors.valuesToArray());
return new RetrieveThingsResponse(HttpStatusCode.OK, thingsArray, thingsArray.toString(), namespace, dittoHeaders);
}
private static JsonObject getJsonFields(@Nullable final JsonFieldSelector fieldSelector,
@Nullable final Predicate<JsonField> predicate, final DittoHeaders dittoHeaders,
final Thing thing) {
if (fieldSelector != null) {
return predicate != null ?
thing.toJson(dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST), fieldSelector, predicate) :
thing.toJson(dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST), fieldSelector);
} else {
return predicate != null ?
thing.toJson(dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST), predicate) :
thing.toJson(dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST));
}
}
/**
* Creates a response to a {@link RetrieveThings} command from a JSON string.
*
* @param jsonString the JSON string of which the response is to be created.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if {@code jsonString} is {@code null}.
* @throws IllegalArgumentException if {@code jsonString} is empty.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonString} was not in the expected
* format.
*/
public static RetrieveThingsResponse fromJson(final String jsonString, final DittoHeaders dittoHeaders) {
final JsonObject jsonObject =
DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
return fromJson(jsonObject, dittoHeaders);
}
/**
* Creates a response to a {@link RetrieveThings} command from a JSON object.
*
* @param jsonObject the JSON object of which the response is to be created.
* @param dittoHeaders the headers of the preceding command.
* @return the response.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected
* format.
*/
public static RetrieveThingsResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new CommandResponseJsonDeserializer<RetrieveThingsResponse>(TYPE, jsonObject)
.deserialize(statusCode -> {
final JsonArray thingsJsonArray = jsonObject.getValue(JSON_THINGS).orElse(null);
final String plainJsonString = jsonObject.getValue(JSON_THINGS_PLAIN_JSON)
.orElseGet(() -> thingsJsonArray != null ? thingsJsonArray.toString() : null);
final String namespace = jsonObject.getValue(JSON_NAMESPACE).orElse(null);
return new RetrieveThingsResponse(HttpStatusCode.OK, thingsJsonArray, plainJsonString, namespace,
dittoHeaders);
});
}
@Override
public String getThingId() {
return ":_";
}
@Override
public Optional<String> getNamespace() {
return Optional.ofNullable(namespace);
}
/**
* Returns the retrieved Things.
*
* @return the retrieved Things.
*/
public List<Thing> getThings() {
return getThingStream(lazyLoadThingsJsonArray()).collect(Collectors.toList());
}
private Stream<Thing> getThingStream(JsonArray thingsArray) {
return thingsArray.stream()
.filter(JsonValue::isObject)
.map(JsonValue::asObject)
.map(ThingsModelFactory::newThing);
}
private JsonArray lazyLoadThingsJsonArray() {
if (things == null) {
things = JsonFactory.readFrom(thingsPlainJson).asArray();
}
return things;
}
@Override
public Optional<String> getEntityPlainString() {
return Optional.of(thingsPlainJson);
}
@Override
public JsonValue getEntity(final JsonSchemaVersion schemaVersion) {
return lazyLoadThingsJsonArray();
}
@Override
public RetrieveThingsResponse setEntity(final JsonValue entity) {
checkNotNull(entity, "entity");
return of(entity.asArray(), namespace, getDittoHeaders());
}
@Override
public RetrieveThingsResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingsPlainJson, namespace, dittoHeaders);
}
@Override
public JsonPointer getResourcePath() {
return JsonPointer.empty(); // no path for retrieve of multiple things
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(JSON_THINGS_PLAIN_JSON, thingsPlainJson, predicate);
if (namespace != null) {
jsonObjectBuilder.set(JSON_NAMESPACE, namespace, predicate);
}
}
@Override
protected boolean canEqual(@Nullable final Object other) {
return (other instanceof RetrieveThingsResponse);
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final RetrieveThingsResponse that = (RetrieveThingsResponse) o;
return that.canEqual(this) && Objects.equals(things, that.things) &&
Objects.equals(thingsPlainJson, that.thingsPlainJson) && Objects.equals(namespace, that.namespace) &&
super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), things, thingsPlainJson, namespace);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" + super.toString() + ", things=" + things +
", thingsPlainJson=" + thingsPlainJson + ", namespace=" + namespace + "]";
}
}