-
Notifications
You must be signed in to change notification settings - Fork 215
/
MessageCommandResponse.java
executable file
·131 lines (112 loc) · 4.75 KB
/
MessageCommandResponse.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
/*
* 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.messages.model.signals.commands;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.SignalWithEntityId;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.messages.model.Message;
import org.eclipse.ditto.messages.model.MessageDirection;
import org.eclipse.ditto.things.model.WithThingId;
/**
* Base interface for all response messages to things and features.
*
* @param <P> the type of the message's payload.
* @param <C> the type of the MessageCommandResponse.
*/
public interface MessageCommandResponse<P, C extends MessageCommandResponse<P, C>>
extends CommandResponse<C>, SignalWithEntityId<C>, WithThingId, WithMessage<P> {
/**
* Type Prefix of Message commands.
*/
String TYPE_PREFIX = "messages." + TYPE_QUALIFIER + ":";
/**
* Retrieves the type of the message to be delivered. This will be used as routingKey for delivering the message
* over AMQP.
*
* @return the type of the message to be delivered
*/
default String getMessageType() {
return getName();
}
@Override
default String getResourceType() {
return MessageCommand.RESOURCE_TYPE;
}
@Override
C setDittoHeaders(DittoHeaders dittoHeaders);
@Override
default JsonPointer getResourcePath() {
final Message<?> message = getMessage();
final String box = message.getDirection() == MessageDirection.TO
? MessageCommand.INBOX_PREFIX
: MessageCommand.OUTBOX_PREFIX;
final JsonPointer pathSuffix =
JsonFactory.newPointer(JsonKey.of(box), JsonKey.of(MessageCommand.MESSAGES_PREFIX),
JsonKey.of(message.getSubject()));
final JsonPointer path = message.getFeatureId()
.map(fId -> JsonFactory.newPointer(JsonKey.of(MessageCommand.FEATURES_PREFIX), JsonKey.of(fId)))
.orElse(JsonPointer.empty());
return path.append(pathSuffix);
}
/**
* Indicates whether the specified signal argument is a {@link MessageCommandResponse}.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is a {@code MessageCommandResponse}, {@code false} else.
* @since 3.0.0
*/
static boolean isMessageCommandResponse(@Nullable final Signal<?> signal) {
return Signal.hasTypePrefix(signal, MessageCommandResponse.TYPE_PREFIX);
}
/**
* This class contains definitions for all specific fields of a {@code MessageCommandResponse}'s JSON
* representation.
*
*/
class JsonFields extends Command.JsonFields {
/**
* JSON field containing the MessageCommandResponse's thingId.
*/
public static final JsonFieldDefinition<String> JSON_THING_ID =
JsonFactory.newStringFieldDefinition("thingId", FieldType.REGULAR,
JsonSchemaVersion.V_2);
/**
* JSON field containing the MessageCommandResponse's Message.
*/
public static final JsonFieldDefinition<JsonObject> JSON_MESSAGE =
JsonFactory.newJsonObjectFieldDefinition("message", FieldType.REGULAR,
JsonSchemaVersion.V_2);
/**
* JSON field containing the MessageCommandResponse's Message headers.
*/
public static final JsonFieldDefinition<JsonObject> JSON_MESSAGE_HEADERS =
JsonFactory.newJsonObjectFieldDefinition("headers", FieldType.REGULAR,
JsonSchemaVersion.V_2);
/**
* JSON field containing the MessageCommandResponse's Message payload.
*/
public static final JsonFieldDefinition<String> JSON_MESSAGE_PAYLOAD =
JsonFactory.newStringFieldDefinition("payload", FieldType.REGULAR,
JsonSchemaVersion.V_2);
}
}