/
ExternalMessage.java
167 lines (140 loc) · 4.68 KB
/
ExternalMessage.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
/*
* 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.services.models.connectivity;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.Optional;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.base.headers.DittoHeaderDefinition;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.HeaderMapping;
import org.eclipse.ditto.model.connectivity.PayloadMapping;
import org.eclipse.ditto.model.placeholders.EnforcementFilter;
import org.eclipse.ditto.protocoladapter.TopicPath;
/**
* Simple wrapper around the headers and the payload received from or sent to external AMQP (0.9 or 1.0)
* sources/targets.
*/
public interface ExternalMessage {
/**
* Message header for the Content-Type.
*/
String CONTENT_TYPE_HEADER = DittoHeaderDefinition.CONTENT_TYPE.getKey();
/**
* Message header for the reply to address. MUST be lower-case.
* "reply-to" is a standard internet message header (RFC-5322).
*/
String REPLY_TO_HEADER = "reply-to";
/**
* @return the headers of the ExternalMessage
*/
Map<String, String> getHeaders();
/**
* @param key the header key
* @param value the header value
* @return new instance of {@link ExternalMessage} including the provided header
*/
ExternalMessage withHeader(String key, String value);
/**
* @param additionalHeaders headers added to message headers
* @return new instance of {@link ExternalMessage} including the provided headers
*/
ExternalMessage withHeaders(Map<String, String> additionalHeaders);
/**
* @param topicPath the {@link TopicPath} to set in the new built ExternalMessage
* @return new instance of {@link ExternalMessage} including the provided TopicPath
*/
ExternalMessage withTopicPath(TopicPath topicPath);
/**
* @return the optional value of the Content-Type header
*/
default Optional<String> findContentType() {
return findHeaderIgnoreCase(CONTENT_TYPE_HEADER);
}
/**
* @param key the key to look up in the headers
* @return the optional value of the specified header {@code key}
*/
Optional<String> findHeader(String key);
/**
* @param key the key to look up in the headers case insensitively
* @return the optional value of the specified header {@code key}
*/
Optional<String> findHeaderIgnoreCase(String key);
/**
* @return whether this ExternalMessage is a text message
*/
boolean isTextMessage();
/**
* @return whether this ExternalMessage is a bytes message
*/
boolean isBytesMessage();
/**
* @return the text payload
*/
Optional<String> getTextPayload();
/**
* @return the bytes payload
*/
Optional<ByteBuffer> getBytePayload();
/**
* @return the PayloadType of this ExternalMessage
*/
PayloadType getPayloadType();
/**
* @return {@code true} if this message is a response
*/
boolean isResponse();
/**
* @return {@code true} if this message is an error
*/
boolean isError();
/**
* @return the {@link AuthorizationContext} assigned to this message
*/
Optional<AuthorizationContext> getAuthorizationContext();
/**
* @return the {@link TopicPath} assigned to this message
*/
Optional<TopicPath> getTopicPath();
/**
* @return the required data to apply the enforcement (if enforcement is enabled), empty otherwise
*/
Optional<EnforcementFilter<CharSequence>> getEnforcementFilter();
/**
* @return the optional header mapping
*/
Optional<HeaderMapping> getHeaderMapping();
/**
* @return the payload mapping that is applied for this message
*/
Optional<PayloadMapping> getPayloadMapping();
/**
* @return optional source address, where this message was received
*/
Optional<String> getSourceAddress();
/**
* @return Ditto headers of the signal that created this external message if any.
* Use those headers when sending error back into the Ditto cluster.
*/
DittoHeaders getInternalHeaders();
/**
* The known payload types of ExternalMessages.
*/
enum PayloadType {
TEXT,
BYTES,
TEXT_AND_BYTES,
UNKNOWN
}
}