/
ModifyThingDefinitionResponse.java
283 lines (247 loc) · 11.4 KB
/
ModifyThingDefinitionResponse.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
/*
* Copyright (c) 2019 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.model.signals.commands.modify;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonParsableCommandResponse;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandResponse;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseHttpStatusValidator;
import org.eclipse.ditto.base.model.signals.commands.CommandResponseJsonDeserializer;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
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.things.model.Thing;
import org.eclipse.ditto.things.model.ThingDefinition;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandResponse;
/**
* Response to a {@link ModifyThingDefinition} command.
*/
@Immutable
@JsonParsableCommandResponse(type = ModifyThingDefinitionResponse.TYPE)
public final class ModifyThingDefinitionResponse extends AbstractCommandResponse<ModifyThingDefinitionResponse>
implements ThingModifyCommandResponse<ModifyThingDefinitionResponse> {
/**
* Type of this response.
*/
public static final String TYPE = TYPE_PREFIX + ModifyThingDefinition.NAME;
static final JsonFieldDefinition<String> JSON_DEFINITION =
JsonFieldDefinition.ofString("definition", FieldType.REGULAR, JsonSchemaVersion.V_2);
private static final Set<HttpStatus> HTTP_STATUSES;
static {
final Set<HttpStatus> httpStatuses = new HashSet<>();
Collections.addAll(httpStatuses, HttpStatus.CREATED, HttpStatus.NO_CONTENT);
HTTP_STATUSES = Collections.unmodifiableSet(httpStatuses);
}
private static final CommandResponseJsonDeserializer<ModifyThingDefinitionResponse> JSON_DESERIALIZER =
CommandResponseJsonDeserializer.newInstance(TYPE,
context -> {
final JsonObject jsonObject = context.getJsonObject();
return newInstance(
ThingId.of(jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.JSON_THING_ID)),
jsonObject.getValue(JSON_DEFINITION)
.map(ThingsModelFactory::newDefinition)
.orElse(null),
context.getDeserializedHttpStatus(),
context.getDittoHeaders()
);
});
private final ThingId thingId;
@Nullable private final ThingDefinition definition;
private ModifyThingDefinitionResponse(final ThingId thingId,
@Nullable final ThingDefinition definition,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {
super(TYPE, httpStatus, dittoHeaders);
this.thingId = checkNotNull(thingId, "thingId");
this.definition = definition;
if (HttpStatus.NO_CONTENT.equals(httpStatus) && null != definition) {
throw new IllegalArgumentException(
MessageFormat.format("ThingDefinition <{0}> is illegal in conjunction with <{1}>.",
definition,
httpStatus)
);
}
}
/**
* Returns a new {@code ModifyThingDefinitionResponse} for a created definition. This corresponds to the HTTP
* status {@link org.eclipse.ditto.base.model.common.HttpStatus#CREATED}.
*
* @param thingId the Thing ID of the created definition.
* @param definition the created definition.
* @param dittoHeaders the headers of the ThingCommand which caused the new response.
* @return a command response for a created definition.
* @throws NullPointerException if {@code thingId} or {@code dittoHeaders} is {@code null}.
*/
public static ModifyThingDefinitionResponse created(final ThingId thingId,
@Nullable final ThingDefinition definition,
final DittoHeaders dittoHeaders) {
return newInstance(thingId, definition, HttpStatus.CREATED, dittoHeaders);
}
/**
* Returns a new {@code ModifyThingDefinitionResponse} for a modified definition. This corresponds to the HTTP
* status {@link org.eclipse.ditto.base.model.common.HttpStatus#NO_CONTENT}.
*
* @param thingId the Thing ID of the modified definition.
* @param dittoHeaders the headers of the ThingCommand which caused the new response.
* @return a command response for a modified definition.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ModifyThingDefinitionResponse modified(final ThingId thingId, final DittoHeaders dittoHeaders) {
return newInstance(thingId, null, HttpStatus.NO_CONTENT, dittoHeaders);
}
/**
* Returns a new instance of {@code ModifyThingDefinitionResponse} for the specified arguments.
*
* @param thingId the ID of the thing the attribute belongs to.
* @param httpStatus the status of the response.
* @param definition the {@code ThingDefinition} that was created or {@code null} if an existing definition was
* modified.
* @param dittoHeaders the headers of the response.
* @return the {@code ModifyThingDefinitionResponse} instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code httpStatus} is not allowed for a {@code ModifyThingDefinitionResponse}
* or if {@code httpStatus} contradicts {@code definition}.
* @since 2.3.0
*/
public static ModifyThingDefinitionResponse newInstance(final ThingId thingId,
@Nullable final ThingDefinition definition,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {
return new ModifyThingDefinitionResponse(thingId,
definition,
CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus,
HTTP_STATUSES,
ModifyThingDefinitionResponse.class),
dittoHeaders);
}
/**
* Creates a response to a {@link ModifyThingDefinition} 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 any argument 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 ModifyThingDefinitionResponse fromJson(final String jsonString, final DittoHeaders dittoHeaders) {
return fromJson(JsonObject.of(jsonString), dittoHeaders);
}
/**
* Creates a response to a {@link ModifyThingDefinition} 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 any argument is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected
* format.
*/
public static ModifyThingDefinitionResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return JSON_DESERIALIZER.deserialize(jsonObject, dittoHeaders);
}
/**
* ModifyThingDefinitionResponse is only available in JsonSchemaVersion V_2.
*
* @return the supported JsonSchemaVersions.
*/
@Override
public JsonSchemaVersion[] getSupportedSchemaVersions() {
return new JsonSchemaVersion[]{JsonSchemaVersion.V_2};
}
@Override
public ThingId getEntityId() {
return thingId;
}
/**
* Returns the created definition.
*
* @return the created definition.
*/
public Optional<ThingDefinition> getDefinition() {
return Optional.ofNullable(definition);
}
@Override
public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {
return Optional.ofNullable(definition).map(JsonValue::of);
}
@Override
public ModifyThingDefinitionResponse setEntity(final JsonValue entity) {
return newInstance(thingId,
getHttpStatus() == HttpStatus.CREATED ? ThingsModelFactory.newDefinition(entity.asString()) : null,
getHttpStatus(), getDittoHeaders());
}
@Override
public JsonPointer getResourcePath() {
return JsonPointer.of(Thing.JsonFields.DEFINITION.getPointer().toString());
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,
final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(ThingCommandResponse.JsonFields.JSON_THING_ID, thingId.toString(), predicate);
if (definition != null) {
jsonObjectBuilder.set(JSON_DEFINITION, String.valueOf(definition), predicate);
}
}
@Override
public ModifyThingDefinitionResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return newInstance(thingId, definition, getHttpStatus(), dittoHeaders);
}
@Override
protected boolean canEqual(@Nullable final Object other) {
return other instanceof ModifyThingDefinitionResponse;
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ModifyThingDefinitionResponse that = (ModifyThingDefinitionResponse) o;
return that.canEqual(this) &&
Objects.equals(thingId, that.thingId) &&
Objects.equals(definition, that.definition) &&
super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), thingId, definition);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" + super.toString() + ", thingId=" + thingId + ", definition=" +
definition + "]";
}
}