/
RetrieveThings.java
executable file
·383 lines (331 loc) · 14.3 KB
/
RetrieveThings.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* 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.signals.commands.things.query;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
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.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.JsonParsableCommand;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.signals.commands.base.AbstractCommand;
import org.eclipse.ditto.signals.commands.base.WithNamespace;
import org.eclipse.ditto.signals.commands.things.exceptions.MissingThingIdsException;
/**
* Command which retrieves several {@link org.eclipse.ditto.model.things.Thing}s based on the the passed in List of
* Thing IDs.
*/
@Immutable
@JsonParsableCommand(typePrefix = RetrieveThings.TYPE_PREFIX, name = RetrieveThings.NAME)
public final class RetrieveThings extends AbstractCommand<RetrieveThings>
implements ThingQueryCommand<RetrieveThings>, WithNamespace {
/**
* Name of the "Retrieve Things" command.
*/
public static final String NAME = "retrieveThings";
/**
* Type of this command.
*/
public static final String TYPE = TYPE_PREFIX + NAME;
public static final JsonFieldDefinition<JsonArray> JSON_THING_IDS =
JsonFactory.newJsonArrayFieldDefinition("thingIds", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
static final JsonFieldDefinition<String> JSON_SELECTED_FIELDS =
JsonFactory.newStringFieldDefinition("selectedFields", 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 final List<String> thingIds;
@Nullable private final JsonFieldSelector selectedFields;
@Nullable private final String namespace;
private RetrieveThings(final Builder builder) {
this(builder.thingIds, builder.selectedFields, builder.namespace, builder.dittoHeaders);
}
/**
* @throws MissingThingIdsException When {@code thingIds} is empty
*/
private RetrieveThings(final List<String> thingIds,
@Nullable final JsonFieldSelector selectedFields,
@Nullable final String namespace,
final DittoHeaders dittoHeaders) {
super(TYPE, dittoHeaders);
if (thingIds.isEmpty()) {
throw MissingThingIdsException.newBuilder()
.dittoHeaders(dittoHeaders)
.build();
}
this.thingIds = Collections.unmodifiableList(new ArrayList<>(thingIds));
this.selectedFields = selectedFields;
this.namespace = checkForDistinctNamespace(namespace, thingIds);
}
@Nullable
private static String checkForDistinctNamespace(@Nullable final String providedNamespace,
final Collection<String> thingIds) {
if (providedNamespace != null) {
final List<String> distinctNamespaces = thingIds.stream()
.map(id -> id.split(":"))
.filter(parts -> parts.length > 1)
.map(parts -> parts[0])
.distinct()
.collect(Collectors.toList());
if (distinctNamespaces.size() != 1) {
throw new IllegalArgumentException(
"Retrieving multiple things is only supported if all things are in the same, non empty namespace");
}
// if a specific namespace is provided it must match the namespace of the things to retrieve
if (!distinctNamespaces.get(0).equals(providedNamespace)) {
throw new IllegalArgumentException(
"The provided namespace must match the namespace of all things to retrieve.");
}
}
return providedNamespace;
}
/**
* Returns a builder for a command for retrieving the Things.
*
* @param thingIds one or more Thing IDs to be retrieved.
* @return a builder for a Thing retrieving command.
* @throws NullPointerException if {@code thingIds} is {@code null}.
*/
public static Builder getBuilder(final String... thingIds) {
return new Builder(Arrays.asList(checkNotNull(thingIds, "thing ids")));
}
/**
* Returns a builder for a command for retrieving the Things.
*
* @param thingIds the Thing IDs to be retrieved.
* @return a builder for a Thing retrieving command.
* @throws NullPointerException if {@code thingIds} is {@code null}.
*/
public static Builder getBuilder(final List<String> thingIds) {
return new Builder(checkNotNull(thingIds, "thing ids"));
}
/**
* Returns a builder for a command for retrieving Things. The builder gets initialised with the data from the
* specified RetrieveThings.
*
* @param retrieveThings a {@code RetrieveThings} object which acts as template for the new builder.
* @return a builder for a Thing retrieving command.
*/
public static Builder getBuilder(final RetrieveThings retrieveThings) {
return new Builder(retrieveThings);
}
/**
* Creates a new {@code RetrieveThings} from a JSON string.
*
* @param jsonString the JSON string of which the command is to be created.
* @param dittoHeaders the headers of the command.
* @return the command.
* @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 RetrieveThings fromJson(final String jsonString, final DittoHeaders dittoHeaders) {
return fromJson(JsonFactory.newObject(jsonString), dittoHeaders);
}
/**
* Creates a new {@code RetrieveThings} from a JSON object.
*
* @param jsonObject the JSON object of which the command is to be created.
* @param dittoHeaders the headers of the command.
* @return the command.
* @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 RetrieveThings fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
final String namespace = jsonObject.getValue(JSON_NAMESPACE).orElse(null);
final List<String> extractedThingIds = jsonObject.getValueOrThrow(JSON_THING_IDS)
.stream()
.filter(JsonValue::isString)
.map(JsonValue::asString)
.collect(Collectors.toList());
final JsonFieldSelector extractedFieldSelector = jsonObject.getValue(JSON_SELECTED_FIELDS)
.map(str -> JsonFactory.newFieldSelector(str, JsonFactory.newParseOptionsBuilder()
.withoutUrlDecoding()
.build()))
.orElse(null);
return new RetrieveThings(extractedThingIds, extractedFieldSelector, namespace, dittoHeaders);
}
/**
* Returns an unmodifiable unsorted List of the identifiers of the {@code Thing}s to be retrieved.
*
* @return the identifiers of the Things.
*/
public List<String> getThingIds() {
return thingIds;
}
@Override
public Optional<String> getNamespace() {
return Optional.ofNullable(namespace);
}
@Override
public Optional<JsonFieldSelector> getSelectedFields() {
return Optional.ofNullable(selectedFields);
}
@Override
public String getThingId() {
return "";
}
@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);
final JsonArray thingIdsArray = thingIds.stream()
.map(JsonFactory::newValue)
.collect(JsonCollectors.valuesToArray());
jsonObjectBuilder.set(JSON_THING_IDS, thingIdsArray, predicate);
if (namespace != null) {
jsonObjectBuilder.set(JSON_NAMESPACE, namespace, predicate);
}
if (null != selectedFields) {
jsonObjectBuilder.set(JSON_SELECTED_FIELDS, selectedFields.toString(), predicate);
}
}
@Override
public RetrieveThings setDittoHeaders(final DittoHeaders dittoHeaders) {
return getBuilder(this).dittoHeaders(dittoHeaders).build();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), thingIds, selectedFields, namespace);
}
@SuppressWarnings({"squid:MethodCyclomaticComplexity", "squid:S1067", "pmd:SimplifyConditional"})
@Override
public boolean equals(@Nullable final Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final RetrieveThings that = (RetrieveThings) obj;
return that.canEqual(this) && Objects.equals(thingIds, that.thingIds)
&& Objects.equals(namespace, that.namespace)
&& Objects.equals(selectedFields, that.selectedFields) && super.equals(that);
}
@Override
protected boolean canEqual(@Nullable final Object other) {
return other instanceof RetrieveThings;
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" + super.toString() + ", thingIds=" + thingIds + ", selectedFields="
+ selectedFields + ", namespace=" + namespace + "]";
}
/**
* Builder to facilitate creation of {@code RetrieveThings} instances. Multiple calls to one of this class' methods
* will overwrite before set values.
*/
@NotThreadSafe
public static final class Builder {
private final List<String> thingIds;
private DittoHeaders dittoHeaders;
@Nullable private JsonFieldSelector selectedFields;
@Nullable private String namespace;
private Builder(final List<String> thingIds) {
this.thingIds = new ArrayList<>(thingIds);
dittoHeaders = DittoHeaders.empty();
selectedFields = null;
namespace = null;
}
private Builder(final RetrieveThings retrieveThings) {
this.thingIds = retrieveThings.getThingIds();
dittoHeaders = retrieveThings.getDittoHeaders();
selectedFields = retrieveThings.getSelectedFields().orElse(null);
namespace = retrieveThings.getNamespace().orElse(null);
}
/**
* Sets the optional command headers.
*
* @param dittoHeaders the command headers.
* @return this builder to allow method chaining.
*/
public Builder dittoHeaders(final DittoHeaders dittoHeaders) {
this.dittoHeaders = dittoHeaders;
return this;
}
/**
* Sets the JSON fields which should be shown in the JSON document which is returned due to a HTTP request.
*
* @param selectedFields the selected JSON fields to be shown in the resulting JSON document.
* @return this builder to allow method chaining.
*/
public Builder selectedFields(@Nullable final JsonFieldSelector selectedFields) {
if (selectedFields == null || selectedFields.isEmpty()) {
this.selectedFields = null;
} else {
this.selectedFields = selectedFields;
}
return this;
}
/**
* Sets the JSON fields which should be shown in the JSON document which is returned due to a HTTP request.
*
* @param selectedFields the selected JSON fields to be shown in the resulting JSON document.
* @return this builder to allow method chaining.
*/
public Builder selectedFields(final Optional<JsonFieldSelector> selectedFields) {
if (selectedFields.isPresent()) {
return selectedFields(selectedFields.get());
} else {
this.selectedFields = null;
return this;
}
}
/**
* Sets namespace for this command.
*
* @param namespace the namespace used for this command
* @return this builder to allow method chaining.
*/
public Builder namespace(@Nullable final String namespace) {
this.namespace = namespace;
return this;
}
/**
* Builds an instance of {@code RetrieveThings} based on the provided values.
*
* @return a new instance of {@code RetrieveThings}.
*/
public RetrieveThings build() {
return new RetrieveThings(this);
}
}
}