blob: 3d27919599913c7ab023d882493e0986ac50da93 [file] [log] [blame]
Nils Diewaldc471b182014-11-19 22:51:15 +00001package de.ids_mannheim.korap.response;
2
3import java.util.*;
4import java.io.*;
5
6import com.fasterxml.jackson.annotation.*;
Nils Diewaldc99ed5b2015-01-21 22:08:53 +00007import com.fasterxml.jackson.annotation.JsonInclude.Include;
Nils Diewaldc471b182014-11-19 22:51:15 +00008import com.fasterxml.jackson.databind.JsonNode;
9import com.fasterxml.jackson.databind.ObjectMapper;
10import com.fasterxml.jackson.databind.node.ObjectNode;
Nils Diewald0881e242015-02-27 17:31:01 +000011
Nils Diewald2d5f8102015-02-26 21:07:54 +000012import de.ids_mannheim.korap.KrillCollection;
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000013import de.ids_mannheim.korap.KrillMeta;
Nils Diewald0339d462015-02-26 14:53:56 +000014import de.ids_mannheim.korap.KrillQuery;
Akrond7d7b1f2016-06-25 00:31:16 +020015import de.ids_mannheim.korap.KrillStats;
Nils Diewaldc471b182014-11-19 22:51:15 +000016import de.ids_mannheim.korap.response.Notifications;
Akron74748c62016-06-29 00:22:43 +020017import static de.ids_mannheim.korap.util.KrillString.quote;
Nils Diewaldc471b182014-11-19 22:51:15 +000018
Nils Diewald2f2b0672014-11-25 20:26:22 +000019/**
20 * Base class for objects meant to be responded by the server.
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000021 * This inherits KoralQuery requests.
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 * Response km = new Response();
26 * System.out.println(
27 * km.toJsonString()
28 * );
Nils Diewald2f2b0672014-11-25 20:26:22 +000029 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +000030 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000031 * @author diewald
32 * @see Notifications
Nils Diewald2f2b0672014-11-25 20:26:22 +000033 */
Akrond504f212015-06-20 00:27:54 +020034/*
35 * TODO: Use configuration file to get default token field "tokens"
36 * TODO: All these fields should be in meta!
37*/
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000038@JsonInclude(Include.NON_NULL)
39@JsonIgnoreProperties(ignoreUnknown = true)
Nils Diewald0881e242015-02-27 17:31:01 +000040public class Response extends Notifications {
Nils Diewaldc471b182014-11-19 22:51:15 +000041 ObjectMapper mapper = new ObjectMapper();
42
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000043 private KrillMeta meta;
Nils Diewald2d5f8102015-02-26 21:07:54 +000044 private KrillCollection collection;
Akronbb5d1732015-06-22 01:22:40 +020045 private KrillQuery query;
Akrond7d7b1f2016-06-25 00:31:16 +020046 private KrillStats stats;
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000047
Nils Diewald2f2b0672014-11-25 20:26:22 +000048 private String version, name, node, listener;
Nils Diewaldbbd39a52015-02-23 19:56:57 +000049
Nils Diewaldbb33da22015-03-04 16:24:25 +000050 private long totalResources = -2, // Not set
51 totalResults = -2; // Not set
Nils Diewaldc471b182014-11-19 22:51:15 +000052 private String benchmark;
Nils Diewald2f2b0672014-11-25 20:26:22 +000053 private boolean timeExceeded = false;
Nils Diewaldc471b182014-11-19 22:51:15 +000054
Akron91c60112015-09-24 22:05:40 +020055 private HashMap<String, ObjectNode> jsonFields;
56
Akron42993552016-02-04 13:24:24 +010057 private static final String KORAL_VERSION = "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld";
Akron09ae3732015-08-13 20:56:20 +020058
Nils Diewald01ff7af2015-02-04 22:54:26 +000059
Nils Diewald2f2b0672014-11-25 20:26:22 +000060 /**
Nils Diewald0881e242015-02-27 17:31:01 +000061 * Construct a new Response object.
Nils Diewald2f2b0672014-11-25 20:26:22 +000062 */
Nils Diewald0881e242015-02-27 17:31:01 +000063 public Response () {};
Nils Diewaldc471b182014-11-19 22:51:15 +000064
Nils Diewaldc471b182014-11-19 22:51:15 +000065
66 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +000067 * Get string representation of the backend's version.
Nils Diewaldbb33da22015-03-04 16:24:25 +000068 *
Nils Diewald44d5fa12015-01-15 21:31:52 +000069 * @return String representation of the backend's version
70 */
Akrond504f212015-06-20 00:27:54 +020071 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +000072 public String getVersion () {
73 return this.version;
74 };
75
76
77 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +000078 * Set the string representation of the backend's version.
Nils Diewaldbb33da22015-03-04 16:24:25 +000079 *
80 * @param version
81 * The string representation of the backend's version
Nils Diewald0881e242015-02-27 17:31:01 +000082 * @return Response object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +000083 */
Nils Diewald0881e242015-02-27 17:31:01 +000084 public Response setVersion (String fullVersion) {
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000085 int found = fullVersion.lastIndexOf('-');
86
87 // Is combined name and version
88 if (found > 0 && (found + 1 < fullVersion.length())) {
89 this.setName(fullVersion.substring(0, found));
90 this.version = fullVersion.substring(found + 1);
91 }
92 // Is only version number
93 else {
94 this.version = fullVersion;
95 };
Nils Diewald94870ae2014-12-09 14:35:29 +000096 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +000097 };
98
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000099
Nils Diewaldc471b182014-11-19 22:51:15 +0000100 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000101 * Get string representation of the backend's name.
102 * All nodes in a cluster should have the same backend name.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000103 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000104 * @return String representation of the backend's name
Nils Diewaldc471b182014-11-19 22:51:15 +0000105 */
Akrond504f212015-06-20 00:27:54 +0200106 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000107 public String getName () {
108 return this.name;
Nils Diewaldc471b182014-11-19 22:51:15 +0000109 };
110
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000111
Nils Diewaldc471b182014-11-19 22:51:15 +0000112 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +0000113 * Set the string representation of the backend's name.
114 * All nodes in a cluster should have the same backend name.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000115 *
116 * @param name
117 * The string representation of the backend's name
Nils Diewald0881e242015-02-27 17:31:01 +0000118 * @return Response object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000119 */
Nils Diewald0881e242015-02-27 17:31:01 +0000120 public Response setName (String name) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000121 this.name = name;
122 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000123 };
124
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000125
Nils Diewald2f2b0672014-11-25 20:26:22 +0000126 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000127 * Get string representation of the node's name.
128 * Each node in a cluster has a unique name.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000129 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000130 * @return String representation of the node's name
Nils Diewald2f2b0672014-11-25 20:26:22 +0000131 */
Akrond504f212015-06-20 00:27:54 +0200132 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000133 public String getNode () {
134 return this.node;
Nils Diewaldc471b182014-11-19 22:51:15 +0000135 };
136
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000137
Nils Diewald2f2b0672014-11-25 20:26:22 +0000138 /**
139 * Set the string representation of the node's name.
140 * Each node in a cluster has a unique name.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000141 *
142 * @param version
143 * The string representation of the node's name
Nils Diewald0881e242015-02-27 17:31:01 +0000144 * @return Response object for chaining
Nils Diewald2f2b0672014-11-25 20:26:22 +0000145 */
Nils Diewald0881e242015-02-27 17:31:01 +0000146 public Response setNode (String name) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000147 this.node = name;
148 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000149 };
150
Nils Diewald2f2b0672014-11-25 20:26:22 +0000151
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000152 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000153 * Check if the response time was exceeded.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000154 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000155 * @return <tt>true</tt> in case the response had a timeout,
156 * otherwise <tt>false</tt>
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000157 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000158 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000159 public boolean hasTimeExceeded () {
160 return this.timeExceeded;
Nils Diewald2f2b0672014-11-25 20:26:22 +0000161 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000162
Nils Diewald2f2b0672014-11-25 20:26:22 +0000163
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000164 /**
165 * Set to <tt>true</tt> if time is exceeded
166 * based on a timeout.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000167 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000168 * <p>
169 * Will add a warning (682) to the output.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000170 *
171 * @param timeout
172 * Either <tt>true</tt> or <tt>false</tt>,
173 * in case the response timed out
Nils Diewald0881e242015-02-27 17:31:01 +0000174 * @return Response object for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000175 */
Nils Diewald0881e242015-02-27 17:31:01 +0000176 public Response setTimeExceeded (boolean timeout) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000177 if (timeout)
178 this.addWarning(682, "Response time exceeded");
179 this.timeExceeded = timeout;
180 return this;
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000181 };
182
183
184 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000185 * Get the benchmark time as a string.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000186 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000187 * @return String representation of the benchmark
188 * (including trailing time unit)
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000189 */
Akrond504f212015-06-20 00:27:54 +0200190 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000191 public String getBenchmark () {
192 return this.benchmark;
Nils Diewald2f2b0672014-11-25 20:26:22 +0000193 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000194
Nils Diewald2f2b0672014-11-25 20:26:22 +0000195
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000196 /**
197 * Set the benchmark as timestamp differences.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000198 *
199 * @param ts1
200 * Starting time of the benchmark
201 * @param ts2
202 * Ending time of the benchmark
Nils Diewald0881e242015-02-27 17:31:01 +0000203 * @return Response object for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000204 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000205 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000206 public Response setBenchmark (long ts1, long ts2) {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000207 this.benchmark = (ts2 - ts1) < 100_000_000 ?
208 // Store as miliseconds
Eliza Margaretha6f989202016-10-14 21:48:29 +0200209 (((double) (ts2 - ts1) * 1e-6) + " ms") :
Nils Diewaldbb33da22015-03-04 16:24:25 +0000210 // Store as seconds
211 (((double) (ts2 - ts1) / 1000000000.0) + " s");
Nils Diewald94870ae2014-12-09 14:35:29 +0000212 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000213 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000214
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000215
216 /**
217 * Set the benchmark as a string representation.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000218 *
219 * @param bm
220 * String representation of a benchmark
221 * (including trailing time unit)
Nils Diewald0881e242015-02-27 17:31:01 +0000222 * @return Response for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000223 */
Nils Diewald0881e242015-02-27 17:31:01 +0000224 public Response setBenchmark (String bm) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000225 this.benchmark = bm;
226 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000227 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000228
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000229
230 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000231 * Get the listener URI as a string.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000232 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000233 * @return The listener URI as a string representation
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000234 */
Akrond504f212015-06-20 00:27:54 +0200235 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000236 public String getListener () {
237 return this.listener;
Nils Diewaldc471b182014-11-19 22:51:15 +0000238 };
Nils Diewald44d5fa12015-01-15 21:31:52 +0000239
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000240
241 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000242 * Set the listener URI as a String. This is probably the
243 * localhost
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000244 * with an arbitrary port, like
Nils Diewaldbb33da22015-03-04 16:24:25 +0000245 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000246 * <p>
247 * <blockquote><pre>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000248 * http://localhost:8080/
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000249 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000250 *
251 * @param listener
252 * String representation of the listener URI
Nils Diewald0881e242015-02-27 17:31:01 +0000253 * @return Response object for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000254 */
Nils Diewald0881e242015-02-27 17:31:01 +0000255 public Response setListener (String listener) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000256 this.listener = listener;
257 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000258 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000259
Nils Diewaldc471b182014-11-19 22:51:15 +0000260
261 /**
Nils Diewaldafab8f32015-01-26 19:11:32 +0000262 * Get the total number of results.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000263 *
Nils Diewaldafab8f32015-01-26 19:11:32 +0000264 * @return The total number of results.
265 */
Akrond504f212015-06-20 00:27:54 +0200266 @JsonIgnore
Nils Diewaldafab8f32015-01-26 19:11:32 +0000267 public long getTotalResults () {
268 if (this.totalResults == -2)
269 return (long) 0;
270 return this.totalResults;
271 };
272
273
274 /**
275 * Set the total number of results.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000276 *
277 * @param results
278 * The total number of results.
Nils Diewald0881e242015-02-27 17:31:01 +0000279 * @return {link Response} object for chaining.
Nils Diewaldafab8f32015-01-26 19:11:32 +0000280 */
Nils Diewald0881e242015-02-27 17:31:01 +0000281 public Response setTotalResults (long results) {
Nils Diewaldafab8f32015-01-26 19:11:32 +0000282 this.totalResults = results;
283 return this;
284 };
285
286
287 /**
288 * Increment the total number of results by a certain value.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000289 *
290 * @param incr
291 * The number of results the total number should
292 * be incremented by.
Nils Diewald0881e242015-02-27 17:31:01 +0000293 * @return {@link Response} object for chaining.
Nils Diewaldafab8f32015-01-26 19:11:32 +0000294 */
Nils Diewald0881e242015-02-27 17:31:01 +0000295 public Response incrTotalResults (int incr) {
Nils Diewaldafab8f32015-01-26 19:11:32 +0000296 if (this.totalResults < 0)
297 this.totalResults = incr;
298 else
299 this.totalResults += incr;
300 return this;
301 };
302
303
304 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000305 * Get the total number of resources the total number of
Nils Diewaldafab8f32015-01-26 19:11:32 +0000306 * results occur in.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000307 *
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000308 * @return The total number of resources the total number of
Nils Diewaldafab8f32015-01-26 19:11:32 +0000309 * results occur in.
310 */
Akrond504f212015-06-20 00:27:54 +0200311 @JsonIgnore
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000312 public long getTotalResources () {
313 if (this.totalResources == -2)
Nils Diewaldafab8f32015-01-26 19:11:32 +0000314 return (long) 0;
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000315 return this.totalResources;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000316 };
317
318
319 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000320 * Set the total number of resources the total number of
Nils Diewaldafab8f32015-01-26 19:11:32 +0000321 * results occur in.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000322 *
323 * @param resources
324 * The total number of resources the total
325 * number of results occur in.
Nils Diewald0881e242015-02-27 17:31:01 +0000326 * @return {@link Response} object for chaining.
Nils Diewaldafab8f32015-01-26 19:11:32 +0000327 */
Nils Diewald0881e242015-02-27 17:31:01 +0000328 public Response setTotalResources (long resources) {
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000329 this.totalResources = resources;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000330 return this;
331 };
332
333
334 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000335 * Increment the total number of resources the total number
Nils Diewaldafab8f32015-01-26 19:11:32 +0000336 * of results occur in by a certain value.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000337 *
338 * @param incr
339 * The number of resources the total number of
340 * results occur in should be incremented by.
341 * (I don't care that this isn't English!)
Nils Diewald0881e242015-02-27 17:31:01 +0000342 * @return {@link Response} object for chaining.
Nils Diewaldafab8f32015-01-26 19:11:32 +0000343 */
Nils Diewald0881e242015-02-27 17:31:01 +0000344 public Response incrTotalResources (int i) {
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000345 if (this.totalResources < 0)
346 this.totalResources = i;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000347 else
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000348 this.totalResources += i;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000349 return this;
350 };
351
352
353 /**
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000354 * Get the KoralQuery query object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000355 *
Nils Diewald0339d462015-02-26 14:53:56 +0000356 * @return The {@link KrillQuery} object,
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000357 * representing the KoralQuery query object.
358 */
Nils Diewald8904c1d2015-02-26 16:13:18 +0000359 // TODO: "tokens" shouldn't be fixed.
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000360 @JsonIgnore
Nils Diewald0339d462015-02-26 14:53:56 +0000361 public KrillQuery getQuery () {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000362 if (this.query == null)
Nils Diewald8904c1d2015-02-26 16:13:18 +0000363 this.query = new KrillQuery("tokens");
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000364 return this.query;
365 };
366
367
368 /**
369 * Set the KoralQuery query object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000370 *
371 * @param query
372 * The {@link KrillQuery} object,
373 * representing the KoralQuery query object.
Nils Diewald0881e242015-02-27 17:31:01 +0000374 * @return The {@link Response} object for chaining
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000375 */
376 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000377 public Response setQuery (KrillQuery query) {
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000378 this.query = query;
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000379
380 // Move messages from the query
Nils Diewald0881e242015-02-27 17:31:01 +0000381 return (Response) this.moveNotificationsFrom(query);
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000382 };
383
384
385 /**
386 * Get the associated collection object.
387 * In case no collection information was defined yet,
Nils Diewald2d5f8102015-02-26 21:07:54 +0000388 * a new {@link KrillCollection} object will be created.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000389 *
Nils Diewald2d5f8102015-02-26 21:07:54 +0000390 * @return The attached {@link KrillCollection} object.
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000391 */
392 @JsonIgnore
Nils Diewald2d5f8102015-02-26 21:07:54 +0000393 public KrillCollection getCollection () {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000394 if (this.collection == null)
Nils Diewald2d5f8102015-02-26 21:07:54 +0000395 this.collection = new KrillCollection();
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000396 return this.collection;
397 };
398
399
400 /**
Nils Diewald2d5f8102015-02-26 21:07:54 +0000401 * Set a new {@link KrillCollection} object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000402 *
403 * @param collection
404 * A {@link KrillCollection} object.
Nils Diewald0881e242015-02-27 17:31:01 +0000405 * @return The {@link Response} object for chaining
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000406 */
407 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000408 public Response setCollection (KrillCollection collection) {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000409 this.collection = collection;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000410
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000411 // Move messages from the collection
Akronbb5d1732015-06-22 01:22:40 +0200412 Response resp = (Response) this.moveNotificationsFrom(collection);
413
414 return resp;
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000415 };
416
Nils Diewaldbb33da22015-03-04 16:24:25 +0000417
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000418 /**
419 * Get the associated meta object.
420 * In case no meta information was defined yet,
421 * a new {@link KrillMeta} object will be created.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000422 *
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000423 * @return The attached {@link KrillMeta} object.
424 */
425 @JsonIgnore
426 public KrillMeta getMeta () {
427 if (this.meta == null)
428 this.meta = new KrillMeta();
429 return this.meta;
430 };
431
432
433 /**
434 * Set a new {@link KrillMeta} object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000435 *
436 * @param meta
437 * A {@link KrillMeta} object.
Nils Diewald0881e242015-02-27 17:31:01 +0000438 * @return The {@link Response} object for chaining
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000439 */
440 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000441 public Response setMeta (KrillMeta meta) {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000442 this.meta = meta;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000443
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000444 // Move messages from the collection
Nils Diewald0881e242015-02-27 17:31:01 +0000445 return (Response) this.moveNotificationsFrom(meta);
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000446 };
447
Akron42993552016-02-04 13:24:24 +0100448
Akrond7d7b1f2016-06-25 00:31:16 +0200449 /**
450 * Get the associated statistics object.
451 * In case no statistics information was defined yet,
452 * a new {@link KrillStats} object will be created.
453 *
454 * @return The attached {@link KrillStats} object.
455 */
456 @JsonIgnore
457 public KrillStats getStats () {
458 if (this.stats == null)
459 this.stats = new KrillStats();
460 return this.stats;
461 };
462
463
464 /**
465 * Set a new {@link KrillStats} object.
466 *
467 * @param stats
468 * A {@link KrillStats} object.
469 * @return The {@link Response} object for chaining
470 */
471 @JsonIgnore
472 public Response setStats (KrillStats stats) {
473 this.stats = stats;
474
475 // Move messages from the stats
476 return (Response) this.moveNotificationsFrom(stats);
477 };
478
479
Akron91c60112015-09-24 22:05:40 +0200480 public void addJsonNode (String key, ObjectNode value) {
481 if (this.jsonFields == null)
482 this.jsonFields = new HashMap<String, ObjectNode>(4);
483 this.jsonFields.put(key, value);
484 };
485
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000486
487 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000488 * Serialize response as a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000489 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000490 * @return {@link JsonNode} representation of the response
Nils Diewaldc471b182014-11-19 22:51:15 +0000491 */
492 @Override
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000493 public JsonNode toJsonNode () {
Nils Diewald94870ae2014-12-09 14:35:29 +0000494 // Get notifications json response
495 ObjectNode json = (ObjectNode) super.toJsonNode();
Nils Diewaldc471b182014-11-19 22:51:15 +0000496
Akron74992bc2015-08-05 22:30:57 +0200497 json.put("@context", KORAL_VERSION);
498
Nils Diewald94870ae2014-12-09 14:35:29 +0000499 StringBuilder sb = new StringBuilder();
Nils Diewaldc471b182014-11-19 22:51:15 +0000500 if (this.getName() != null) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000501 sb.append(this.getName());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000502
Nils Diewald94870ae2014-12-09 14:35:29 +0000503 if (this.getVersion() != null)
504 sb.append("-");
505 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000506
Nils Diewald94870ae2014-12-09 14:35:29 +0000507 // No name but version is given
508 if (this.getVersion() != null)
509 sb.append(this.getVersion());
Nils Diewaldc471b182014-11-19 22:51:15 +0000510
Akrond504f212015-06-20 00:27:54 +0200511 // KoralQuery meta object
512 if (this.meta != null) {
513 JsonNode metaNode = this.meta.toJsonNode();
514 if (metaNode != null)
515 json.put("meta", metaNode);
516 };
517
Akronbb5d1732015-06-22 01:22:40 +0200518 ObjectNode meta = json.has("meta") ? (ObjectNode) json.get("meta")
519 : (ObjectNode) json.putObject("meta");
Akrond504f212015-06-20 00:27:54 +0200520
Nils Diewald94870ae2014-12-09 14:35:29 +0000521 if (sb.length() > 0)
Akrond504f212015-06-20 00:27:54 +0200522 meta.put("version", sb.toString());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000523
Nils Diewald94870ae2014-12-09 14:35:29 +0000524 if (this.timeExceeded)
Akrond504f212015-06-20 00:27:54 +0200525 meta.put("timeExceeded", true);
Nils Diewaldbb33da22015-03-04 16:24:25 +0000526
Nils Diewaldc471b182014-11-19 22:51:15 +0000527 if (this.getNode() != null)
Akrond504f212015-06-20 00:27:54 +0200528 meta.put("node", this.getNode());
Nils Diewaldc471b182014-11-19 22:51:15 +0000529
530 if (this.getListener() != null)
Akrond504f212015-06-20 00:27:54 +0200531 meta.put("listener", this.getListener());
Nils Diewaldc471b182014-11-19 22:51:15 +0000532
533 if (this.getBenchmark() != null)
Akrond504f212015-06-20 00:27:54 +0200534 meta.put("benchmark", this.getBenchmark());
Nils Diewaldc471b182014-11-19 22:51:15 +0000535
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000536 // totalResources is set
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000537 if (this.totalResources != -2)
Akrond504f212015-06-20 00:27:54 +0200538 meta.put("totalResources", this.totalResources);
Nils Diewaldbb33da22015-03-04 16:24:25 +0000539
Nils Diewaldafab8f32015-01-26 19:11:32 +0000540 // totalResults is set
541 if (this.totalResults != -2)
Akrond504f212015-06-20 00:27:54 +0200542 meta.put("totalResults", this.totalResults);
Nils Diewaldafab8f32015-01-26 19:11:32 +0000543
Akron91c60112015-09-24 22:05:40 +0200544 // Add json fields as passed to the object
545 if (this.jsonFields != null) {
546 json.putAll(this.jsonFields);
547 };
548
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000549 // KoralQuery query object
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000550 if (this.query != null) {
551 JsonNode queryNode = this.getQuery().toJsonNode();
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000552 if (queryNode != null)
553 json.put("query", queryNode);
554 };
555
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000556 // KoralQuery collection object
Akronbb5d1732015-06-22 01:22:40 +0200557 if (this.collection != null) {
558 // && this.collection.getFilters().toArray().length > 0) {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000559 JsonNode collNode = this.collection.toJsonNode();
560 if (collNode != null)
561 json.put("collection", collNode);
562 };
563
Nils Diewald94870ae2014-12-09 14:35:29 +0000564 return (JsonNode) json;
Nils Diewaldc471b182014-11-19 22:51:15 +0000565 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000566
Nils Diewald44d5fa12015-01-15 21:31:52 +0000567
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000568 /**
569 * Serialize response as a JSON string.
570 * <p>
571 * <blockquote><pre>
572 * {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000573 * "version" : "Lucene-Backend-0.49.1",
574 * "timeExceeded" : true,
575 * "node" : "Tanja",
576 * "listener" : "http://localhost:8080/",
577 * "benchmark" : "12.3s",
578 * "errors": [
579 * [123, "You are not allowed to serialize these messages"],
580 * [124, "Your request was invalid"]
581 * ],
582 * "messages" : [
583 * [125, "Class is deprecated", "Notifications"]
584 * ]
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000585 * }
586 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000587 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000588 * @return String representation of the response
589 */
590 public String toJsonString () {
Nils Diewald94870ae2014-12-09 14:35:29 +0000591 String msg = "";
592 try {
593 return mapper.writeValueAsString(this.toJsonNode());
594 }
595 catch (Exception e) {
596 // Bad in case the message contains quotes!
Akron74748c62016-06-29 00:22:43 +0200597 msg = ", " + quote(e.getLocalizedMessage());
Nils Diewald94870ae2014-12-09 14:35:29 +0000598 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000599
Nils Diewaldbb33da22015-03-04 16:24:25 +0000600 return "{\"errors\":[" + "[620, " + "\"Unable to generate JSON\"" + msg
601 + "]" + "]}";
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000602 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000603};