blob: 877c65525dffae2791a141b830cff52fcf0e01c3 [file] [log] [blame]
Nils Diewaldc471b182014-11-19 22:51:15 +00001package de.ids_mannheim.korap.response;
2
Akron74748c62016-06-29 00:22:43 +02003import static de.ids_mannheim.korap.util.KrillString.quote;
4
Nils Diewaldc471b182014-11-19 22:51:15 +00005import com.fasterxml.jackson.annotation.*;
6import com.fasterxml.jackson.annotation.JsonInclude.Include;
7import com.fasterxml.jackson.databind.ObjectMapper;
8import com.fasterxml.jackson.databind.JsonNode;
9import com.fasterxml.jackson.databind.node.*;
10
11import java.util.*;
12import java.io.*;
13import de.ids_mannheim.korap.response.Message;
14import de.ids_mannheim.korap.response.Messages;
15import de.ids_mannheim.korap.util.QueryException;
margaretha78f397a2017-06-29 13:44:46 +020016import de.ids_mannheim.korap.util.StatusCodes;
Nils Diewaldc471b182014-11-19 22:51:15 +000017
18/**
Nils Diewald2f2b0672014-11-25 20:26:22 +000019 * A unified notification class for KorAP related errors,
Nils Diewaldc471b182014-11-19 22:51:15 +000020 * warnings and messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +000021 *
Nils Diewald2f2b0672014-11-25 20:26:22 +000022 * <p>
23 * The object contains lists of errors, warnings and messages
24 * and new warnings, errors or messages are appended to these lists.
Nils Diewaldbb33da22015-03-04 16:24:25 +000025 *
Nils Diewald2f2b0672014-11-25 20:26:22 +000026 * <p>
27 * <blockquote><pre>
Nils Diewaldbb33da22015-03-04 16:24:25 +000028 * Notifications n = new Notifications();
29 * n.addWarning(456, "Something went wrong");
30 * if (n.hasWarnings()) {
31 * for (Message msg : n.getWarnings())
32 * System.err.out(msg.getCode() + ": " + msg.getMessage());
33 * };
34 * System.err.println(n.toJsonString());
Nils Diewald2f2b0672014-11-25 20:26:22 +000035 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +000036 *
Nils Diewaldc471b182014-11-19 22:51:15 +000037 * @author Nils Diewald
Nils Diewald2f2b0672014-11-25 20:26:22 +000038 * @see de.ids_mannheim.korap.response.Messages
Nils Diewaldc471b182014-11-19 22:51:15 +000039 */
Nils Diewald2f2b0672014-11-25 20:26:22 +000040/*
41 * This will be inherited most of the time as Java does not support roles
42 * and I have no idea how to do this more elegantly.
43 */
Nils Diewaldc471b182014-11-19 22:51:15 +000044@JsonInclude(Include.NON_NULL)
45@JsonIgnoreProperties(ignoreUnknown = true)
46public class Notifications {
47
48 // Create object mapper for JSON generation
49 ObjectMapper mapper = new ObjectMapper();
50
Nils Diewald44d5fa12015-01-15 21:31:52 +000051 private Messages warnings, errors, messages;
52
Nils Diewaldbb33da22015-03-04 16:24:25 +000053
Nils Diewald44d5fa12015-01-15 21:31:52 +000054 /**
55 * Check for warnings.
Nils Diewaldbb33da22015-03-04 16:24:25 +000056 *
57 * @return <tt>true</tt> in case there are warnings, otherwise
58 * <tt>false</tt>
Nils Diewald44d5fa12015-01-15 21:31:52 +000059 */
60 public boolean hasWarnings () {
61 if (this.warnings == null || this.warnings.size() == 0)
62 return false;
63 return true;
64 };
65
66
67 /**
68 * Return all warnings.
Nils Diewaldbb33da22015-03-04 16:24:25 +000069 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000070 * @return {@link Messages} representing all warnings
Nils Diewald44d5fa12015-01-15 21:31:52 +000071 */
Akron7d45e6b2015-06-26 17:23:42 +020072 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +000073 public Messages getWarnings () {
74 return this.warnings;
75 };
76
77
78 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000079 * Set warnings by means of a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +000080 *
81 * @param msgs
82 * JSON array of warnings.
Nils Diewaldafab8f32015-01-26 19:11:32 +000083 * @return {@link Notifications} object for chaining.
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000084 */
85 public Notifications setWarnings (JsonNode msgs) {
86 for (JsonNode msg : msgs)
87 this.addWarning(msg);
88 return this;
89 };
90
91
92
93 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +000094 * Return a specific warning based on an index.
Nils Diewaldbb33da22015-03-04 16:24:25 +000095 *
96 * @param index
97 * The index of the warning in the list of warnings.
98 * @return The message in case it exists, otherwise
99 * <code>null</code>
Nils Diewald44d5fa12015-01-15 21:31:52 +0000100 */
Akron7d45e6b2015-06-26 17:23:42 +0200101 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000102 public Message getWarning (int index) {
103 if (this.warnings != null)
104 return this.warnings.get(index);
105 return (Message) null;
106 };
107
Nils Diewaldc471b182014-11-19 22:51:15 +0000108
109 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000110 * Appends a new warning.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000111 *
112 * @param code
113 * Integer code representation of the warning
114 * @param msg
115 * String representation of the warning
116 * @param terms
117 * Optional strings of additional information
Nils Diewald2f2b0672014-11-25 20:26:22 +0000118 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000119 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000120 public Notifications addWarning (int code, String msg, String ... terms) {
Akron352dae82016-08-05 17:57:51 +0200121
122 if (this.warnings == null) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000123 this.warnings = new Messages();
Akron352dae82016-08-05 17:57:51 +0200124 };
125
Nils Diewald44d5fa12015-01-15 21:31:52 +0000126 this.warnings.add(code, msg, terms);
Akron352dae82016-08-05 17:57:51 +0200127
Nils Diewald44d5fa12015-01-15 21:31:52 +0000128 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000129 };
130
Nils Diewaldbb33da22015-03-04 16:24:25 +0000131
Nils Diewald2f2b0672014-11-25 20:26:22 +0000132 /**
133 * Appends a new warning.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000134 *
135 * @param node
136 * {@link JsonNode} representing a warning message
Nils Diewald2f2b0672014-11-25 20:26:22 +0000137 * @return Notification object for chaining
138 */
139 public Notifications addWarning (JsonNode node) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000140 if (this.warnings == null)
141 this.warnings = new Messages();
Nils Diewaldbb33da22015-03-04 16:24:25 +0000142
Nils Diewald44d5fa12015-01-15 21:31:52 +0000143 try {
144 this.warnings.add(node);
145 }
146 catch (QueryException qe) {
147 this.warnings.add(qe.getErrorCode(), qe.getMessage());
148 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000149
Nils Diewald44d5fa12015-01-15 21:31:52 +0000150 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000151 };
152
Nils Diewald44d5fa12015-01-15 21:31:52 +0000153
Nils Diewaldc471b182014-11-19 22:51:15 +0000154 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000155 * Appends new warnings.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000156 *
157 * @param msgs
158 * {@link Messages} representing multiple warnings
Nils Diewald2f2b0672014-11-25 20:26:22 +0000159 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000160 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000161 public Notifications addWarnings (Messages msgs) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000162 if (this.warnings == null)
163 this.warnings = msgs;
164 else
165 this.warnings.add(msgs);
166 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000167 };
168
169
170 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000171 * Return all errors.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000172 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000173 * @return The {@link Messages} object representing all errors
Nils Diewaldc471b182014-11-19 22:51:15 +0000174 */
Akron7d45e6b2015-06-26 17:23:42 +0200175 @JsonIgnore
Nils Diewaldc471b182014-11-19 22:51:15 +0000176 public Messages getErrors () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000177 return this.errors;
Nils Diewaldc471b182014-11-19 22:51:15 +0000178 };
179
Nils Diewald2f2b0672014-11-25 20:26:22 +0000180
181 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000182 * Set errors by means of a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000183 *
184 * @param msgs
185 * JSON array of errors.
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000186 * @return Notifications object for chaining.
187 */
188 public Notifications setErrors (JsonNode msgs) {
189 for (JsonNode msg : msgs)
190 this.addError(msg);
191 return this;
192 };
193
194
195 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000196 * Return a specific error based on an index.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000197 *
198 * @param index
199 * The index of the error in the list of errors.
200 * @return The message in case it exists, otherwise
201 * <code>null</code>
Nils Diewald2f2b0672014-11-25 20:26:22 +0000202 */
Akron7d45e6b2015-06-26 17:23:42 +0200203 @JsonIgnore
Nils Diewaldc471b182014-11-19 22:51:15 +0000204 public Message getError (int index) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000205 if (this.errors != null)
206 return this.errors.get(index);
207 return (Message) null;
Nils Diewaldc471b182014-11-19 22:51:15 +0000208 };
209
Nils Diewald2f2b0672014-11-25 20:26:22 +0000210
Nils Diewaldc471b182014-11-19 22:51:15 +0000211 /**
212 * Check for errors.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000213 *
214 * @return <tt>true</tt> in case there are errors, otherwise
215 * <tt>false</tt>
Nils Diewaldc471b182014-11-19 22:51:15 +0000216 */
217 public boolean hasErrors () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000218 if (this.errors == null || this.errors.size() == 0)
219 return false;
220 return true;
221 };
222
223
224 /**
225 * Appends a new error.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000226 *
227 * @param code
228 * Integer code representation of the error
229 * @param msg
230 * String representation of the error
231 * @param terms
232 * Optional strings of additional information
Nils Diewald44d5fa12015-01-15 21:31:52 +0000233 * @return Notification object for chaining
234 */
235 public Notifications addError (int code, String msg, String ... terms) {
236 if (this.errors == null)
237 this.errors = new Messages();
238 this.errors.add(code, msg, terms);
239 return this;
240 };
margarethaf2c31502017-06-26 17:57:16 +0200241
margaretha04c4d9d2017-06-29 13:40:42 +0200242 public Notifications addError (int code, String[] terms) {
243 if (this.errors == null)
244 this.errors = new Messages();
245 this.errors.add(code, terms[0], Arrays.copyOfRange(terms, 1, terms.length));
246 return this;
247 }
248
Nils Diewald44d5fa12015-01-15 21:31:52 +0000249 /**
250 * Appends a new error.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000251 *
252 * @param node
253 * {@link JsonNode} representing an error message
Nils Diewald44d5fa12015-01-15 21:31:52 +0000254 * @return Notification object for chaining
255 */
256 public Notifications addError (JsonNode msg) {
257 if (this.errors == null)
258 this.errors = new Messages();
259 try {
260 this.errors.add(msg);
261 }
262 catch (QueryException qe) {
263 this.errors.add(qe.getErrorCode(), qe.getMessage());
264 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000265
Nils Diewald44d5fa12015-01-15 21:31:52 +0000266 return this;
267 };
268
269
270 /**
271 * Appends new errors.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000272 *
273 * @param msgs
274 * {@link Messages} representing multiple errors
Nils Diewald44d5fa12015-01-15 21:31:52 +0000275 * @return Notification object for chaining
276 */
277 public Notifications addErrors (Messages msgs) {
278 if (this.errors == null)
279 this.errors = msgs;
280 else
281 this.errors.add(msgs);
282 return this;
283 };
284
285
286 /**
287 * Return all messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000288 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000289 * @return {@link Messages} representing all messages
Nils Diewald44d5fa12015-01-15 21:31:52 +0000290 */
Akron7d45e6b2015-06-26 17:23:42 +0200291 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000292 public Messages getMessages () {
293 return this.messages;
294 };
295
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000296
297 /**
298 * Set messages by means of a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000299 *
300 * @param msgs
301 * JSON array of messages.
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000302 * @return Notifications object for chaining.
303 */
304 public Notifications setMessages (JsonNode msgs) {
305 for (JsonNode msg : msgs)
306 this.addMessage(msg);
307 return this;
308 };
309
310
Nils Diewald44d5fa12015-01-15 21:31:52 +0000311 /**
312 * Return a specific message based on an index.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000313 *
314 * @param index
315 * The index of the message in the list of messages.
316 * @return The message in case it exists, otherwise
317 * <code>null</code>
Nils Diewald44d5fa12015-01-15 21:31:52 +0000318 */
Akron7d45e6b2015-06-26 17:23:42 +0200319 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000320 public Message getMessage (int index) {
321 if (this.messages != null)
322 return this.messages.get(index);
323 return (Message) null;
324 };
325
326
327 /**
328 * Check for messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000329 *
330 * @return <tt>true</tt> in case there are messages, otherwise
331 * <tt>false</tt>
Nils Diewald44d5fa12015-01-15 21:31:52 +0000332 */
333 public boolean hasMessages () {
334 if (this.messages == null || this.messages.size() == 0)
335 return false;
336 return true;
Nils Diewaldc471b182014-11-19 22:51:15 +0000337 };
338
339
340 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000341 * Appends a new message.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000342 *
343 * @param code
344 * Integer code representation of the message
345 * @param msg
346 * String representation of the message
347 * @param terms
348 * Optional strings of additional information
Nils Diewald2f2b0672014-11-25 20:26:22 +0000349 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000350 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000351 public Notifications addMessage (int code, String msg, String ... terms) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000352 if (this.messages == null)
353 this.messages = new Messages();
354 this.messages.add(code, msg, terms);
355 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000356 };
357
Nils Diewald44d5fa12015-01-15 21:31:52 +0000358
Nils Diewald2f2b0672014-11-25 20:26:22 +0000359 /**
360 * Appends a new message.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000361 *
362 * @param node
363 * {@link JsonNode} representing a message
Nils Diewald2f2b0672014-11-25 20:26:22 +0000364 * @return Notification object for chaining
365 */
366 public Notifications addMessage (JsonNode msg) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000367 if (this.messages == null)
368 this.messages = new Messages();
369 try {
370 this.messages.add(msg);
371 }
372 catch (QueryException qe) {
373 this.messages.add(qe.getErrorCode(), qe.getMessage());
374 };
375 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000376 };
377
Nils Diewald2f2b0672014-11-25 20:26:22 +0000378
Nils Diewaldc471b182014-11-19 22:51:15 +0000379 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000380 * Appends new messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000381 *
382 * @param msgs
383 * {@link Messages} representing multiple messages
Nils Diewald2f2b0672014-11-25 20:26:22 +0000384 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000385 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000386 public Notifications addMessages (Messages msgs) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000387 if (this.messages == null)
388 this.messages = msgs;
389 else
390 this.messages.add(msgs);
391 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000392 };
393
394
395 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000396 * Copy notifications from another notification object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000397 *
398 * @param notes
399 * Notification object to copy notifications from.
Nils Diewald2f2b0672014-11-25 20:26:22 +0000400 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000401 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000402 public Notifications copyNotificationsFrom (Notifications notes) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000403 try {
404 if (notes.hasErrors())
405 this.addErrors((Messages) notes.getErrors().clone());
406 if (notes.hasWarnings())
407 this.addWarnings((Messages) notes.getWarnings().clone());
408 if (notes.hasMessages())
409 this.addMessages((Messages) notes.getMessages().clone());
410 }
Nils Diewaldbb33da22015-03-04 16:24:25 +0000411 catch (CloneNotSupportedException cnse) {};
Nils Diewald44d5fa12015-01-15 21:31:52 +0000412 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000413 };
414
415
416 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000417 * Copy notifications from a {@link JsonNode} object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000418 *
419 * @param request
420 * Notifications containing {@link JsonNode}.
Nils Diewald2f2b0672014-11-25 20:26:22 +0000421 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000422 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000423 public Notifications copyNotificationsFrom (JsonNode request) {
Nils Diewaldc471b182014-11-19 22:51:15 +0000424
Nils Diewald44d5fa12015-01-15 21:31:52 +0000425 // Add warnings from JSON
Nils Diewaldbb33da22015-03-04 16:24:25 +0000426 if (request.has("warnings") && request.get("warnings").isArray()) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000427 JsonNode msgs = request.get("warnings");
428 for (JsonNode msg : msgs)
429 this.addWarning(msg);
430 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000431
Nils Diewald44d5fa12015-01-15 21:31:52 +0000432 // Add messages from JSON
Nils Diewaldbb33da22015-03-04 16:24:25 +0000433 if (request.has("messages") && request.get("messages").isArray()) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000434 JsonNode msgs = request.get("messages");
435 if (msgs.isArray())
436 for (JsonNode msg : msgs)
437 this.addMessage(msg);
438 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000439
Nils Diewald44d5fa12015-01-15 21:31:52 +0000440 // Add errors from JSON
Nils Diewaldbb33da22015-03-04 16:24:25 +0000441 if (request.has("errors") && request.get("errors").isArray()) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000442 JsonNode msgs = request.get("errors");
443 if (msgs.isArray())
444 for (JsonNode msg : msgs)
445 this.addError(msg);
446 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000447
Nils Diewald44d5fa12015-01-15 21:31:52 +0000448 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000449 };
450
451
452 /**
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000453 * Move notifications from a passed {@link Notification} object
454 * to the invocant.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000455 *
456 * @param notes
457 * Notification object.
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000458 * @return The invocant object for chaining
459 */
460 public Notifications moveNotificationsFrom (Notifications notes) {
461 this.copyNotificationsFrom(notes);
462 notes.clearNotifications();
463 return this;
464 };
465
466
467 /**
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000468 * Clear all notifications.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000469 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000470 * @return Notification object for chaining
471 */
472 public Notifications clearNotifications () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000473 if (this.warnings != null)
474 this.warnings.clear();
475 if (this.messages != null)
476 this.messages.clear();
477 if (this.errors != null)
478 this.errors.clear();
479 return this;
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000480 };
481
482
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000483
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000484 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000485 * Serialize Notifications as a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000486 *
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000487 * @return {@link JsonNode} representation of all warnings,
488 * errors, and messages.
Nils Diewaldc471b182014-11-19 22:51:15 +0000489 */
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000490 public JsonNode toJsonNode () {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000491 ObjectNode json = mapper.createObjectNode();
Nils Diewaldc471b182014-11-19 22:51:15 +0000492
Nils Diewald44d5fa12015-01-15 21:31:52 +0000493 // Add messages
494 if (this.hasWarnings())
495 json.put("warnings", this.getWarnings().toJsonNode());
496 if (this.hasErrors())
497 json.put("errors", this.getErrors().toJsonNode());
498 if (this.hasMessages())
499 json.put("messages", this.getMessages().toJsonNode());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000500
Nils Diewald44d5fa12015-01-15 21:31:52 +0000501 return (JsonNode) json;
Nils Diewaldc471b182014-11-19 22:51:15 +0000502 };
503
Nils Diewald44d5fa12015-01-15 21:31:52 +0000504
Nils Diewaldc471b182014-11-19 22:51:15 +0000505 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000506 * Serialize Notifications as a JSON string.
507 * <p>
508 * <blockquote><pre>
509 * {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000510 * "errors": [
511 * [123, "You are not allowed to serialize these messages"],
512 * [124, "Your request was invalid"]
513 * ],
514 * "messages" : [
515 * [125, "Class is deprecated", "Notifications"]
516 * ]
Nils Diewald2f2b0672014-11-25 20:26:22 +0000517 * }
518 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000519 *
520 * @return String representation of all warnings, errors, and
521 * messages
Nils Diewaldc471b182014-11-19 22:51:15 +0000522 */
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000523 public String toJsonString () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000524 String msg = "";
525 try {
526 JsonNode node = this.toJsonNode();
527 if (node == null)
528 return "{}";
529 return mapper.writeValueAsString(node);
530 }
531 catch (Exception e) {
532 // Bad in case the message contains quotes!
Akron74748c62016-06-29 00:22:43 +0200533 msg = ", " + quote(e.getLocalizedMessage());
Nils Diewald44d5fa12015-01-15 21:31:52 +0000534 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000535
Nils Diewaldbb33da22015-03-04 16:24:25 +0000536 return "{\"errors\" : [" + "[620, " + "\"Unable to generate JSON\""
537 + msg + "]" + "]}";
Nils Diewaldc471b182014-11-19 22:51:15 +0000538 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000539};