/
ModifyThing.java
executable file
·366 lines (323 loc) · 16.4 KB
/
ModifyThing.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
/*
* 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.modify;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonMissingFieldException;
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.JsonSchemaVersion;
import org.eclipse.ditto.model.things.AccessControlList;
import org.eclipse.ditto.model.things.AclNotAllowedException;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingsModelFactory;
import org.eclipse.ditto.signals.commands.base.AbstractCommand;
import org.eclipse.ditto.signals.commands.base.CommandJsonDeserializer;
import org.eclipse.ditto.signals.commands.things.ThingCommandSizeValidator;
import org.eclipse.ditto.signals.commands.things.exceptions.PolicyIdNotAllowedException;
/**
* This command modifies an existing Thing. It contains the full {@link Thing} including the Thing ID which should be
* used for modification.
*/
public final class ModifyThing extends AbstractCommand<ModifyThing> implements ThingModifyCommand<ModifyThing> {
/**
* Name of the "Modify Thing" command.
*/
public static final String NAME = "modifyThing";
/**
* Type of this command.
*/
public static final String TYPE = TYPE_PREFIX + NAME;
static final JsonFieldDefinition<JsonObject> JSON_THING =
JsonFactory.newJsonObjectFieldDefinition("thing", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
/**
* Json Field definition for the optional initial "inline" policy when creating a Thing.
*/
public static final JsonFieldDefinition<JsonObject> JSON_INITIAL_POLICY =
JsonFactory.newJsonObjectFieldDefinition("initialPolicy", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* Json Field definition for the optional initial "inline" policy when creating a Thing.
*/
public static final JsonFieldDefinition<String> JSON_POLICY_ID_OR_PLACEHOLDER =
JsonFactory.newStringFieldDefinition("policyIdOrPlaceholder", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* The name of the JSON field when a policy is copied.
*/
public static final String JSON_COPY_POLICY_FROM = "_copyPolicyFrom";
/**
* Json Field definition for the optional initial "inline" policy for usage in getEntity().
*/
public static final JsonFieldDefinition<JsonObject> JSON_INLINE_POLICY =
JsonFactory.newJsonObjectFieldDefinition("_policy", FieldType.REGULAR, JsonSchemaVersion.V_2);
private final String thingId;
private final Thing thing;
@Nullable private final JsonObject initialPolicy;
@Nullable private final String policyIdOrPlaceholder;
private ModifyThing(final String thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
@Nullable final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders) {
super(TYPE, dittoHeaders);
this.thingId = thingId;
this.thing = thing;
this.initialPolicy = initialPolicy;
this.policyIdOrPlaceholder = policyIdOrPlaceholder;
ThingCommandSizeValidator.getInstance().ensureValidSize(() -> thing.toJsonString().length(), () ->
dittoHeaders);
}
/**
* Returns a command for modifying a thing which is passed as argument.
*
* @param thingId the Thing's ID.
* @param thing the {@link Thing} to modify.
* @param initialPolicy the initial {@code Policy} to set for the Thing when creating it - may be null.
* @param dittoHeaders the headers of the command.
* @return the command.
* @throws NullPointerException if any argument is {@code null}.
* @throws PolicyIdNotAllowedException if the passed {@code thing} contained a Policy or Policy ID but the command was
* created via API version {@link JsonSchemaVersion#V_1}.
* @throws AclNotAllowedException if the passed {@code thing} contained an ACL but the command was created via
* an API version greater than {@link JsonSchemaVersion#V_1}.
*/
public static ModifyThing of(final String thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
final DittoHeaders dittoHeaders) {
Objects.requireNonNull(thingId, "The Thing identifier must not be null!");
Objects.requireNonNull(thing, "The modified Thing must not be null!");
ensureAuthorizationMatchesSchemaVersion(thingId, thing, initialPolicy, null, dittoHeaders);
return new ModifyThing(thingId, thing, initialPolicy, null, dittoHeaders);
}
/**
* Returns a command for modifying a thing which is passed as argument. The thing will have a policy copied from
* a policy with the given policyIdOrPlaceholder.
*
* @param thingId the Thing's ID.
* @param thing the {@link Thing} to modify.
* @param policyIdOrPlaceholder the policy id of the {@code Policy} to copy and set for the Thing when creating it.
* If its a placeholder it will be resolved to a policy id.
* Placeholder must be of the syntax: {{ ref:things/theThingId/policyId }}.
* @param dittoHeaders the headers of the command.
* @return the command.
* @throws NullPointerException if any argument is {@code null}.
* @throws PolicyIdNotAllowedException if the passed {@code thing} contained a Policy or Policy ID but the command
* was created via API version {@link JsonSchemaVersion#V_1}.
* @throws AclNotAllowedException if the passed {@code thing} contained an ACL but the command was created via
* an API version greater than {@link JsonSchemaVersion#V_1}.
*/
public static ModifyThing withCopiedPolicy(final String thingId, final Thing thing,
final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders) {
Objects.requireNonNull(thingId, "The Thing identifier must not be null!");
Objects.requireNonNull(thing, "The modified Thing must not be null!");
Objects.requireNonNull(policyIdOrPlaceholder, "The policy id to copy must nit be null!");
ensureAuthorizationMatchesSchemaVersion(thingId, thing, null, policyIdOrPlaceholder, dittoHeaders);
return new ModifyThing(thingId, thing, null, policyIdOrPlaceholder, dittoHeaders);
}
/**
* Returns a command for modifying a thing which is passed as argument.
*
* @param thingId the Thing's ID.
* @param thing the {@link Thing} to modify.
* @param policyIdOrPlaceholder the policy id of the {@code Policy} to copy and set for the Thing when creating it.
* If its a placeholder it will be resolved to a policy id.
* Placeholder must be of the syntax: {{ ref:things/theThingId/policyId }}.
* @param initialPolicy the initial {@code Policy} to set for the Thing when creating it - may be null.
* @param dittoHeaders the headers of the command.
* @return the command.
* @throws NullPointerException if any argument is {@code null}.
* @throws PolicyIdNotAllowedException if the passed {@code thing} contained a Policy or Policy ID but the command
* was created via API version {@link JsonSchemaVersion#V_1}.
* @throws AclNotAllowedException if the passed {@code thing} contained an ACL but the command was created via
* an API version greater than {@link JsonSchemaVersion#V_1}.
*/
public static ModifyThing of(final String thingId, final Thing thing, @Nullable final JsonObject initialPolicy,
@Nullable final String policyIdOrPlaceholder, final DittoHeaders dittoHeaders) {
if (policyIdOrPlaceholder == null) {
return of(thingId, thing, initialPolicy, dittoHeaders);
} else {
return withCopiedPolicy(thingId, thing, policyIdOrPlaceholder, dittoHeaders);
}
}
/**
* Creates a new {@code ModifyThing} 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 ModifyThing fromJson(final String jsonString, final DittoHeaders dittoHeaders) {
return fromJson(JsonFactory.newObject(jsonString), dittoHeaders);
}
/**
* Creates a new {@code ModifyThing} 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.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain a field for {@link
* ThingModifyCommand.JsonFields#JSON_THING_ID} or {@link #JSON_THING}.
*/
public static ModifyThing fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new CommandJsonDeserializer<ModifyThing>(TYPE, jsonObject).deserialize(() -> {
final JsonObject thingJsonObject = jsonObject.getValueOrThrow(JSON_THING);
final Thing extractedThing = ThingsModelFactory.newThing(thingJsonObject);
final JsonObject initialPolicyObject = jsonObject.getValue(JSON_INITIAL_POLICY).orElse(null);
final String policyIdOrPlaceholder = jsonObject.getValue(JSON_POLICY_ID_OR_PLACEHOLDER).orElse(null);
final Optional<String> optionalThingId = jsonObject.getValue(ThingModifyCommand.JsonFields.JSON_THING_ID);
final String thingId = optionalThingId.orElseGet(() -> extractedThing.getId().orElseThrow(() ->
new JsonMissingFieldException(ThingModifyCommand.JsonFields.JSON_THING_ID)
));
return of(thingId, extractedThing, initialPolicyObject, policyIdOrPlaceholder, dittoHeaders);
});
}
/**
* Ensures that the command will not contain inconsistent authorization information. <ul> <li>{@link
* org.eclipse.ditto.model.base.json.JsonSchemaVersion#V_1} commands may not contain policy information.</li>
* <li>{@link org.eclipse.ditto.model.base.json.JsonSchemaVersion#LATEST} commands may not contain ACL
* information.</li> </ul>
*/
private static void ensureAuthorizationMatchesSchemaVersion(final String thingId,
final Thing thing,
@Nullable final JsonObject initialPolicy,
@Nullable final String policyIdOrPlaceholder,
final DittoHeaders dittoHeaders) {
final JsonSchemaVersion version = dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST);
if (JsonSchemaVersion.V_1.equals(version)) {
// v1 commands may not contain policy information
final boolean containsPolicy =
null != policyIdOrPlaceholder || null != initialPolicy || thing.getPolicyId().isPresent();
if (containsPolicy) {
throw PolicyIdNotAllowedException
.newBuilder(thingId)
.dittoHeaders(dittoHeaders)
.build();
}
} else {
// v2 commands may not contain ACL information
final boolean isCommandAclEmpty = thing
.getAccessControlList()
.map(AccessControlList::isEmpty)
.orElse(true);
if (!isCommandAclEmpty) {
throw AclNotAllowedException
.newBuilder(thingId)
.dittoHeaders(dittoHeaders)
.build();
}
}
}
/**
* Returns the {@code Thing} to modify.
*
* @return the Thing to modify.
*/
public Thing getThing() {
return thing;
}
@Override
public String getThingId() {
return thingId;
}
/**
* @return the initial {@code Policy} if there should be one applied when creating the Thing.
*/
public Optional<JsonObject> getInitialPolicy() {
return Optional.ofNullable(initialPolicy);
}
/**
* @return The policy id of the {@code Policy} to copy and set for the Thing when creating it.
* Could also be a placeholder like: {{ ref:things/theThingId/policyId }}.
*/
public Optional<String> getPolicyIdOrPlaceholder() {
return Optional.ofNullable(policyIdOrPlaceholder);
}
@Override
public JsonPointer getResourcePath() {
return JsonPointer.empty();
}
@Override
public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {
final JsonObject thingJson = thing.toJson(schemaVersion, FieldType.regularOrSpecial());
final JsonObject withInlinePolicyThingJson =
getInitialPolicy().map(ip -> thingJson.set(JSON_INLINE_POLICY, ip)).orElse(thingJson);
final JsonObject fullThingJson = getPolicyIdOrPlaceholder().map(
containedPolicyIdOrPlaceholder -> withInlinePolicyThingJson.setValue(JSON_COPY_POLICY_FROM,
containedPolicyIdOrPlaceholder)).orElse(withInlinePolicyThingJson);
return Optional.of(fullThingJson);
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(ThingModifyCommand.JsonFields.JSON_THING_ID, thingId, predicate);
jsonObjectBuilder.set(JSON_THING, thing.toJson(schemaVersion, thePredicate), predicate);
if (initialPolicy != null) {
jsonObjectBuilder.set(JSON_INITIAL_POLICY, initialPolicy, predicate);
}
if (policyIdOrPlaceholder != null) {
jsonObjectBuilder.set(JSON_COPY_POLICY_FROM, policyIdOrPlaceholder, predicate);
}
}
@Override
public Category getCategory() {
return Category.MODIFY;
}
@Override
public ModifyThing setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(thingId, thing, initialPolicy, policyIdOrPlaceholder, dittoHeaders);
}
@Override
public boolean changesAuthorization() {
return thing.getPolicyId().isPresent() ||
thing.getAccessControlList().isPresent();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), thingId, thing, initialPolicy, policyIdOrPlaceholder);
}
@SuppressWarnings("squid:MethodCyclomaticComplexity")
@Override
public boolean equals(@Nullable final Object obj) {
if (this == obj) {
return true;
}
if (null == obj || getClass() != obj.getClass()) {
return false;
}
final ModifyThing that = (ModifyThing) obj;
return that.canEqual(this) && Objects.equals(thingId, that.thingId)
&& Objects.equals(thing, that.thing) && Objects.equals(initialPolicy, that.initialPolicy) &&
Objects.equals(policyIdOrPlaceholder, that.policyIdOrPlaceholder) &&
super.equals(obj);
}
@Override
protected boolean canEqual(@Nullable final Object other) {
return (other instanceof ModifyThing);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" + super.toString() + ", thingId=" + thingId + ", thing=" + thing +
", initialPolicy=" + initialPolicy + ", policyIdOrPlaceholder=" + policyIdOrPlaceholder + "]";
}
}