blob: 4a91b3af72c3295e2a81a843bf7de2acfa763ead [file] [log] [blame]
Nils Diewaldc471b182014-11-19 22:51:15 +00001package de.ids_mannheim.korap.response;
2
3import com.fasterxml.jackson.annotation.*;
4import com.fasterxml.jackson.annotation.JsonInclude.Include;
5import com.fasterxml.jackson.databind.ObjectMapper;
6import com.fasterxml.jackson.databind.JsonNode;
7import com.fasterxml.jackson.databind.node.*;
8
9import java.util.*;
10import java.io.*;
11import de.ids_mannheim.korap.response.Message;
12import de.ids_mannheim.korap.response.Messages;
13import de.ids_mannheim.korap.util.QueryException;
14
15/**
Nils Diewald2f2b0672014-11-25 20:26:22 +000016 * A unified notification class for KorAP related errors,
Nils Diewaldc471b182014-11-19 22:51:15 +000017 * warnings and messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +000018 *
Nils Diewald2f2b0672014-11-25 20:26:22 +000019 * <p>
20 * The object contains lists of errors, warnings and messages
21 * and new warnings, errors or messages are appended to these lists.
Nils Diewaldbb33da22015-03-04 16:24:25 +000022 *
Nils Diewald2f2b0672014-11-25 20:26:22 +000023 * <p>
24 * <blockquote><pre>
Nils Diewaldbb33da22015-03-04 16:24:25 +000025 * Notifications n = new Notifications();
26 * n.addWarning(456, "Something went wrong");
27 * if (n.hasWarnings()) {
28 * for (Message msg : n.getWarnings())
29 * System.err.out(msg.getCode() + ": " + msg.getMessage());
30 * };
31 * System.err.println(n.toJsonString());
Nils Diewald2f2b0672014-11-25 20:26:22 +000032 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +000033 *
Nils Diewaldc471b182014-11-19 22:51:15 +000034 * @author Nils Diewald
Nils Diewald2f2b0672014-11-25 20:26:22 +000035 * @see de.ids_mannheim.korap.response.Messages
Nils Diewaldc471b182014-11-19 22:51:15 +000036 */
Nils Diewald2f2b0672014-11-25 20:26:22 +000037/*
38 * This will be inherited most of the time as Java does not support roles
39 * and I have no idea how to do this more elegantly.
40 */
Nils Diewaldc471b182014-11-19 22:51:15 +000041@JsonInclude(Include.NON_NULL)
42@JsonIgnoreProperties(ignoreUnknown = true)
43public class Notifications {
44
45 // Create object mapper for JSON generation
46 ObjectMapper mapper = new ObjectMapper();
47
Nils Diewald44d5fa12015-01-15 21:31:52 +000048 private Messages warnings, errors, messages;
49
Nils Diewaldbb33da22015-03-04 16:24:25 +000050
Nils Diewald44d5fa12015-01-15 21:31:52 +000051 /**
52 * Check for warnings.
Nils Diewaldbb33da22015-03-04 16:24:25 +000053 *
54 * @return <tt>true</tt> in case there are warnings, otherwise
55 * <tt>false</tt>
Nils Diewald44d5fa12015-01-15 21:31:52 +000056 */
57 public boolean hasWarnings () {
58 if (this.warnings == null || this.warnings.size() == 0)
59 return false;
60 return true;
61 };
62
63
64 /**
65 * Return all warnings.
Nils Diewaldbb33da22015-03-04 16:24:25 +000066 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000067 * @return {@link Messages} representing all warnings
Nils Diewald44d5fa12015-01-15 21:31:52 +000068 */
Akron7d45e6b2015-06-26 17:23:42 +020069 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +000070 public Messages getWarnings () {
71 return this.warnings;
72 };
73
74
75 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000076 * Set warnings by means of a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +000077 *
78 * @param msgs
79 * JSON array of warnings.
Nils Diewaldafab8f32015-01-26 19:11:32 +000080 * @return {@link Notifications} object for chaining.
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000081 */
82 public Notifications setWarnings (JsonNode msgs) {
83 for (JsonNode msg : msgs)
84 this.addWarning(msg);
85 return this;
86 };
87
88
89
90 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +000091 * Return a specific warning based on an index.
Nils Diewaldbb33da22015-03-04 16:24:25 +000092 *
93 * @param index
94 * The index of the warning in the list of warnings.
95 * @return The message in case it exists, otherwise
96 * <code>null</code>
Nils Diewald44d5fa12015-01-15 21:31:52 +000097 */
Akron7d45e6b2015-06-26 17:23:42 +020098 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +000099 public Message getWarning (int index) {
100 if (this.warnings != null)
101 return this.warnings.get(index);
102 return (Message) null;
103 };
104
Nils Diewaldc471b182014-11-19 22:51:15 +0000105
106 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000107 * Appends a new warning.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000108 *
109 * @param code
110 * Integer code representation of the warning
111 * @param msg
112 * String representation of the warning
113 * @param terms
114 * Optional strings of additional information
Nils Diewald2f2b0672014-11-25 20:26:22 +0000115 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000116 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000117 public Notifications addWarning (int code, String msg, String ... terms) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000118 if (this.warnings == null)
119 this.warnings = new Messages();
120 this.warnings.add(code, msg, terms);
121 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000122 };
123
Nils Diewaldbb33da22015-03-04 16:24:25 +0000124
Nils Diewald2f2b0672014-11-25 20:26:22 +0000125 /**
126 * Appends a new warning.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000127 *
128 * @param node
129 * {@link JsonNode} representing a warning message
Nils Diewald2f2b0672014-11-25 20:26:22 +0000130 * @return Notification object for chaining
131 */
132 public Notifications addWarning (JsonNode node) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000133 if (this.warnings == null)
134 this.warnings = new Messages();
Nils Diewaldbb33da22015-03-04 16:24:25 +0000135
Nils Diewald44d5fa12015-01-15 21:31:52 +0000136 try {
137 this.warnings.add(node);
138 }
139 catch (QueryException qe) {
140 this.warnings.add(qe.getErrorCode(), qe.getMessage());
141 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000142
Nils Diewald44d5fa12015-01-15 21:31:52 +0000143 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000144 };
145
Nils Diewald44d5fa12015-01-15 21:31:52 +0000146
Nils Diewaldc471b182014-11-19 22:51:15 +0000147 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000148 * Appends new warnings.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000149 *
150 * @param msgs
151 * {@link Messages} representing multiple warnings
Nils Diewald2f2b0672014-11-25 20:26:22 +0000152 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000153 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000154 public Notifications addWarnings (Messages msgs) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000155 if (this.warnings == null)
156 this.warnings = msgs;
157 else
158 this.warnings.add(msgs);
159 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000160 };
161
162
163 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000164 * Return all errors.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000165 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000166 * @return The {@link Messages} object representing all errors
Nils Diewaldc471b182014-11-19 22:51:15 +0000167 */
Akron7d45e6b2015-06-26 17:23:42 +0200168 @JsonIgnore
Nils Diewaldc471b182014-11-19 22:51:15 +0000169 public Messages getErrors () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000170 return this.errors;
Nils Diewaldc471b182014-11-19 22:51:15 +0000171 };
172
Nils Diewald2f2b0672014-11-25 20:26:22 +0000173
174 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000175 * Set errors by means of a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000176 *
177 * @param msgs
178 * JSON array of errors.
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000179 * @return Notifications object for chaining.
180 */
181 public Notifications setErrors (JsonNode msgs) {
182 for (JsonNode msg : msgs)
183 this.addError(msg);
184 return this;
185 };
186
187
188 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000189 * Return a specific error based on an index.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000190 *
191 * @param index
192 * The index of the error in the list of errors.
193 * @return The message in case it exists, otherwise
194 * <code>null</code>
Nils Diewald2f2b0672014-11-25 20:26:22 +0000195 */
Akron7d45e6b2015-06-26 17:23:42 +0200196 @JsonIgnore
Nils Diewaldc471b182014-11-19 22:51:15 +0000197 public Message getError (int index) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000198 if (this.errors != null)
199 return this.errors.get(index);
200 return (Message) null;
Nils Diewaldc471b182014-11-19 22:51:15 +0000201 };
202
Nils Diewald2f2b0672014-11-25 20:26:22 +0000203
Nils Diewaldc471b182014-11-19 22:51:15 +0000204 /**
205 * Check for errors.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000206 *
207 * @return <tt>true</tt> in case there are errors, otherwise
208 * <tt>false</tt>
Nils Diewaldc471b182014-11-19 22:51:15 +0000209 */
210 public boolean hasErrors () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000211 if (this.errors == null || this.errors.size() == 0)
212 return false;
213 return true;
214 };
215
216
217 /**
218 * Appends a new error.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000219 *
220 * @param code
221 * Integer code representation of the error
222 * @param msg
223 * String representation of the error
224 * @param terms
225 * Optional strings of additional information
Nils Diewald44d5fa12015-01-15 21:31:52 +0000226 * @return Notification object for chaining
227 */
228 public Notifications addError (int code, String msg, String ... terms) {
229 if (this.errors == null)
230 this.errors = new Messages();
231 this.errors.add(code, msg, terms);
232 return this;
233 };
234
235
236 /**
237 * Appends a new error.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000238 *
239 * @param node
240 * {@link JsonNode} representing an error message
Nils Diewald44d5fa12015-01-15 21:31:52 +0000241 * @return Notification object for chaining
242 */
243 public Notifications addError (JsonNode msg) {
244 if (this.errors == null)
245 this.errors = new Messages();
246 try {
247 this.errors.add(msg);
248 }
249 catch (QueryException qe) {
250 this.errors.add(qe.getErrorCode(), qe.getMessage());
251 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000252
Nils Diewald44d5fa12015-01-15 21:31:52 +0000253 return this;
254 };
255
256
257 /**
258 * Appends new errors.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000259 *
260 * @param msgs
261 * {@link Messages} representing multiple errors
Nils Diewald44d5fa12015-01-15 21:31:52 +0000262 * @return Notification object for chaining
263 */
264 public Notifications addErrors (Messages msgs) {
265 if (this.errors == null)
266 this.errors = msgs;
267 else
268 this.errors.add(msgs);
269 return this;
270 };
271
272
273 /**
274 * Return all messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000275 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000276 * @return {@link Messages} representing all messages
Nils Diewald44d5fa12015-01-15 21:31:52 +0000277 */
Akron7d45e6b2015-06-26 17:23:42 +0200278 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000279 public Messages getMessages () {
280 return this.messages;
281 };
282
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000283
284 /**
285 * Set messages by means of a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000286 *
287 * @param msgs
288 * JSON array of messages.
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000289 * @return Notifications object for chaining.
290 */
291 public Notifications setMessages (JsonNode msgs) {
292 for (JsonNode msg : msgs)
293 this.addMessage(msg);
294 return this;
295 };
296
297
Nils Diewald44d5fa12015-01-15 21:31:52 +0000298 /**
299 * Return a specific message based on an index.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000300 *
301 * @param index
302 * The index of the message in the list of messages.
303 * @return The message in case it exists, otherwise
304 * <code>null</code>
Nils Diewald44d5fa12015-01-15 21:31:52 +0000305 */
Akron7d45e6b2015-06-26 17:23:42 +0200306 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000307 public Message getMessage (int index) {
308 if (this.messages != null)
309 return this.messages.get(index);
310 return (Message) null;
311 };
312
313
314 /**
315 * Check for messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000316 *
317 * @return <tt>true</tt> in case there are messages, otherwise
318 * <tt>false</tt>
Nils Diewald44d5fa12015-01-15 21:31:52 +0000319 */
320 public boolean hasMessages () {
321 if (this.messages == null || this.messages.size() == 0)
322 return false;
323 return true;
Nils Diewaldc471b182014-11-19 22:51:15 +0000324 };
325
326
327 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000328 * Appends a new message.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000329 *
330 * @param code
331 * Integer code representation of the message
332 * @param msg
333 * String representation of the message
334 * @param terms
335 * Optional strings of additional information
Nils Diewald2f2b0672014-11-25 20:26:22 +0000336 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000337 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000338 public Notifications addMessage (int code, String msg, String ... terms) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000339 if (this.messages == null)
340 this.messages = new Messages();
341 this.messages.add(code, msg, terms);
342 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000343 };
344
Nils Diewald44d5fa12015-01-15 21:31:52 +0000345
Nils Diewald2f2b0672014-11-25 20:26:22 +0000346 /**
347 * Appends a new message.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000348 *
349 * @param node
350 * {@link JsonNode} representing a message
Nils Diewald2f2b0672014-11-25 20:26:22 +0000351 * @return Notification object for chaining
352 */
353 public Notifications addMessage (JsonNode msg) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000354 if (this.messages == null)
355 this.messages = new Messages();
356 try {
357 this.messages.add(msg);
358 }
359 catch (QueryException qe) {
360 this.messages.add(qe.getErrorCode(), qe.getMessage());
361 };
362 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000363 };
364
Nils Diewald2f2b0672014-11-25 20:26:22 +0000365
Nils Diewaldc471b182014-11-19 22:51:15 +0000366 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000367 * Appends new messages.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000368 *
369 * @param msgs
370 * {@link Messages} representing multiple messages
Nils Diewald2f2b0672014-11-25 20:26:22 +0000371 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000372 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000373 public Notifications addMessages (Messages msgs) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000374 if (this.messages == null)
375 this.messages = msgs;
376 else
377 this.messages.add(msgs);
378 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000379 };
380
381
382 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000383 * Copy notifications from another notification object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000384 *
385 * @param notes
386 * Notification object to copy notifications from.
Nils Diewald2f2b0672014-11-25 20:26:22 +0000387 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000388 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000389 public Notifications copyNotificationsFrom (Notifications notes) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000390 try {
391 if (notes.hasErrors())
392 this.addErrors((Messages) notes.getErrors().clone());
393 if (notes.hasWarnings())
394 this.addWarnings((Messages) notes.getWarnings().clone());
395 if (notes.hasMessages())
396 this.addMessages((Messages) notes.getMessages().clone());
397 }
Nils Diewaldbb33da22015-03-04 16:24:25 +0000398 catch (CloneNotSupportedException cnse) {};
Nils Diewald44d5fa12015-01-15 21:31:52 +0000399 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000400 };
401
402
403 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000404 * Copy notifications from a {@link JsonNode} object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000405 *
406 * @param request
407 * Notifications containing {@link JsonNode}.
Nils Diewald2f2b0672014-11-25 20:26:22 +0000408 * @return Notification object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000409 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000410 public Notifications copyNotificationsFrom (JsonNode request) {
Nils Diewaldc471b182014-11-19 22:51:15 +0000411
Nils Diewald44d5fa12015-01-15 21:31:52 +0000412 // Add warnings from JSON
Nils Diewaldbb33da22015-03-04 16:24:25 +0000413 if (request.has("warnings") && request.get("warnings").isArray()) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000414 JsonNode msgs = request.get("warnings");
415 for (JsonNode msg : msgs)
416 this.addWarning(msg);
417 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000418
Nils Diewald44d5fa12015-01-15 21:31:52 +0000419 // Add messages from JSON
Nils Diewaldbb33da22015-03-04 16:24:25 +0000420 if (request.has("messages") && request.get("messages").isArray()) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000421 JsonNode msgs = request.get("messages");
422 if (msgs.isArray())
423 for (JsonNode msg : msgs)
424 this.addMessage(msg);
425 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000426
Nils Diewald44d5fa12015-01-15 21:31:52 +0000427 // Add errors from JSON
Nils Diewaldbb33da22015-03-04 16:24:25 +0000428 if (request.has("errors") && request.get("errors").isArray()) {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000429 JsonNode msgs = request.get("errors");
430 if (msgs.isArray())
431 for (JsonNode msg : msgs)
432 this.addError(msg);
433 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000434
Nils Diewald44d5fa12015-01-15 21:31:52 +0000435 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000436 };
437
438
439 /**
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000440 * Move notifications from a passed {@link Notification} object
441 * to the invocant.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000442 *
443 * @param notes
444 * Notification object.
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000445 * @return The invocant object for chaining
446 */
447 public Notifications moveNotificationsFrom (Notifications notes) {
448 this.copyNotificationsFrom(notes);
449 notes.clearNotifications();
450 return this;
451 };
452
453
454 /**
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000455 * Clear all notifications.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000456 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000457 * @return Notification object for chaining
458 */
459 public Notifications clearNotifications () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000460 if (this.warnings != null)
461 this.warnings.clear();
462 if (this.messages != null)
463 this.messages.clear();
464 if (this.errors != null)
465 this.errors.clear();
466 return this;
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000467 };
468
469
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000470
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000471 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000472 * Serialize Notifications as a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000473 *
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000474 * @return {@link JsonNode} representation of all warnings,
475 * errors, and messages.
Nils Diewaldc471b182014-11-19 22:51:15 +0000476 */
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000477 public JsonNode toJsonNode () {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000478 ObjectNode json = mapper.createObjectNode();
Nils Diewaldc471b182014-11-19 22:51:15 +0000479
Nils Diewald44d5fa12015-01-15 21:31:52 +0000480 // Add messages
481 if (this.hasWarnings())
482 json.put("warnings", this.getWarnings().toJsonNode());
483 if (this.hasErrors())
484 json.put("errors", this.getErrors().toJsonNode());
485 if (this.hasMessages())
486 json.put("messages", this.getMessages().toJsonNode());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000487
Nils Diewald44d5fa12015-01-15 21:31:52 +0000488 return (JsonNode) json;
Nils Diewaldc471b182014-11-19 22:51:15 +0000489 };
490
Nils Diewald44d5fa12015-01-15 21:31:52 +0000491
Nils Diewaldc471b182014-11-19 22:51:15 +0000492 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000493 * Serialize Notifications as a JSON string.
494 * <p>
495 * <blockquote><pre>
496 * {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000497 * "errors": [
498 * [123, "You are not allowed to serialize these messages"],
499 * [124, "Your request was invalid"]
500 * ],
501 * "messages" : [
502 * [125, "Class is deprecated", "Notifications"]
503 * ]
Nils Diewald2f2b0672014-11-25 20:26:22 +0000504 * }
505 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000506 *
507 * @return String representation of all warnings, errors, and
508 * messages
Nils Diewaldc471b182014-11-19 22:51:15 +0000509 */
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000510 public String toJsonString () {
Nils Diewald44d5fa12015-01-15 21:31:52 +0000511 String msg = "";
512 try {
513 JsonNode node = this.toJsonNode();
514 if (node == null)
515 return "{}";
516 return mapper.writeValueAsString(node);
517 }
518 catch (Exception e) {
519 // Bad in case the message contains quotes!
520 msg = ", \"" + e.getLocalizedMessage() + "\"";
521 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000522
Nils Diewaldbb33da22015-03-04 16:24:25 +0000523 return "{\"errors\" : [" + "[620, " + "\"Unable to generate JSON\""
524 + msg + "]" + "]}";
Nils Diewaldc471b182014-11-19 22:51:15 +0000525 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000526};