forked from elasticio/java-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
265 lines (228 loc) · 6.76 KB
/
Copy pathMessage.java
File metadata and controls
265 lines (228 loc) · 6.76 KB
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
package io.elastic.api;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.UUID;
/**
* Message to be processed by a {@link Module}. A message may have a body,
* which represents a message's payload to be processed, and multiple attachments.
* Both body and attachments are {@link JsonObject}s.
*
* <p>
*
* A {@link Module} may retrieve a value from {@link Message}'s body by a name,
* as shown in the following example.
*
* <pre>
* {@code
* JsonArray orders = message.getBody().getJsonArray("orders");
* }
* </pre>
*
* <p>
*
* A message is build using {@link Builder}, as shown in the following example.
*
* <pre>
* {@code
* JsonArray orders = JSON.parseArray(response.getOrders());
*
* JsonObject body = Json.createObjectBuilder()
* .add("orders", orders)
* .build();
*
* Message message = new Message.Builder().body(body).build();
* }
* </pre>
*/
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
public static final String PROPERTY_ID = "id";
public static final String PROPERTY_BODY = "body";
public static final String PROPERTY_HEADERS = "headers";
public static final String PROPERTY_ATTACHMENTS = "attachments";
public static final String PROPERTY_PASSTHROUGH = "passthrough";
private UUID id;
private JsonObject headers;
private JsonObject body;
private JsonObject attachments;
private JsonObject passthrough;
/**
* Creates a message with headers, body and attachments.
*
* @param id id of the message
* @param headers headers of the message
* @param body body of the message
* @param attachments attachments of the message
* @param passthrough passthrough of the message
*/
private Message(final UUID id,
final JsonObject headers,
final JsonObject body,
final JsonObject attachments,
final JsonObject passthrough) {
if (id == null) {
throw new IllegalArgumentException("Message id must not be null");
}
if (headers == null) {
throw new IllegalArgumentException("Message headers must not be null");
}
if (body == null) {
throw new IllegalArgumentException("Message body must not be null");
}
if (attachments == null) {
throw new IllegalArgumentException("Message attachments must not be null");
}
if (passthrough == null) {
throw new IllegalArgumentException("Message passthrough must not be null");
}
this.id = id;
this.headers = headers;
this.body = body;
this.attachments = attachments;
this.passthrough = passthrough;
}
/**
* Returns message id.
*
* @return id
*/
public UUID getId() {
return id;
}
/**
* Returns message headers.
*
* @return headers
*/
public JsonObject getHeaders() {
return headers;
}
/**
* Returns message body.
*
* @return body
*/
public JsonObject getBody() {
return body;
}
/**
* Returns message attachments.
*
* @return attachments
*/
public JsonObject getAttachments() {
return attachments;
}
/**
* Returns message passthrough.
*
* @return passthrough
*/
public JsonObject getPassthrough() {
return passthrough;
}
/**
* Returns this message as {@link JsonObject}.
*
* @return message as JSON object
*/
public JsonObject toJsonObject() {
return Json.createObjectBuilder()
.add(PROPERTY_ID, id.toString())
.add(PROPERTY_HEADERS, headers)
.add(PROPERTY_BODY, body)
.add(PROPERTY_ATTACHMENTS, attachments)
.add(PROPERTY_PASSTHROUGH, passthrough)
.build();
}
@Override
public String toString() {
final JsonObject json = toJsonObject();
final StringWriter writer = new StringWriter();
final JsonWriter jsonWriter = Json.createWriter(writer);
jsonWriter.writeObject(json);
jsonWriter.close();
return writer.toString();
}
/**
* Used to build {@link Message} instances.
*/
public static final class Builder {
private UUID id;
private JsonObject headers;
private JsonObject body;
private JsonObject attachments;
private JsonObject passthrough;
/**
* Default constructor.
*/
public Builder() {
this.id = UUID.randomUUID();
this.headers = Json.createObjectBuilder().build();
this.body = Json.createObjectBuilder().build();
this.attachments = Json.createObjectBuilder().build();
this.passthrough = Json.createObjectBuilder().build();
}
/**
* Sets message id.
*
* @param id id for the message
* @return same builder instance
*/
public Builder id(final UUID id) {
this.id = id;
return this;
}
/**
* Adds a headers to build message with.
*
* @param headers headers for the message
* @return same builder instance
*/
public Builder headers(final JsonObject headers) {
this.headers = headers;
return this;
}
/**
* Adds a body to build message with.
*
* @param body body for the message
* @return same builder instance
*/
public Builder body(final JsonObject body) {
this.body = body;
return this;
}
/**
* Adds attachments to build message with.
*
* @param attachments attachments for the message
* @return same builder instance
*/
public Builder attachments(final JsonObject attachments) {
this.attachments = attachments;
return this;
}
/**
* Adds passthrough to build message with.
*
* @param passthrough passthrough for the message
* @return same builder instance
*/
public Builder passthrough(final JsonObject passthrough) {
this.passthrough = passthrough;
return this;
}
/**
* Builds a {@link Message} instance and returns it.
*
* @return Message
*/
public Message build() {
return new Message(this.id, this.headers, this.body, this.attachments, this.passthrough);
}
}
}