blob: 576d2ad69b8c826d5386a8072c46f10b08f203d4 [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;
Nils Diewaldc471b182014-11-19 22:51:15 +000015import de.ids_mannheim.korap.response.Notifications;
16
Nils Diewald2f2b0672014-11-25 20:26:22 +000017/**
18 * Base class for objects meant to be responded by the server.
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000019 * This inherits KoralQuery requests.
Nils Diewaldbb33da22015-03-04 16:24:25 +000020 *
Nils Diewald2f2b0672014-11-25 20:26:22 +000021 * <p>
22 * <blockquote><pre>
Nils Diewaldbb33da22015-03-04 16:24:25 +000023 * Response km = new Response();
24 * System.out.println(
25 * km.toJsonString()
26 * );
Nils Diewald2f2b0672014-11-25 20:26:22 +000027 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +000028 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000029 * @author diewald
30 * @see Notifications
Nils Diewald2f2b0672014-11-25 20:26:22 +000031 */
Nils Diewald8904c1d2015-02-26 16:13:18 +000032// Todo: Use configuration file to get default token field "tokens"
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000033@JsonInclude(Include.NON_NULL)
34@JsonIgnoreProperties(ignoreUnknown = true)
Nils Diewald0881e242015-02-27 17:31:01 +000035public class Response extends Notifications {
Nils Diewaldc471b182014-11-19 22:51:15 +000036 ObjectMapper mapper = new ObjectMapper();
37
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000038 private KrillMeta meta;
Nils Diewald2d5f8102015-02-26 21:07:54 +000039 private KrillCollection collection;
Nils Diewaldf5ab4b22015-02-25 20:55:16 +000040
Nils Diewald2f2b0672014-11-25 20:26:22 +000041 private String version, name, node, listener;
Nils Diewald0339d462015-02-26 14:53:56 +000042 private KrillQuery query;
Nils Diewaldbbd39a52015-02-23 19:56:57 +000043
Nils Diewaldbb33da22015-03-04 16:24:25 +000044 private long totalResources = -2, // Not set
45 totalResults = -2; // Not set
Nils Diewaldc471b182014-11-19 22:51:15 +000046 private String benchmark;
Nils Diewald2f2b0672014-11-25 20:26:22 +000047 private boolean timeExceeded = false;
Nils Diewaldc471b182014-11-19 22:51:15 +000048
Nils Diewald01ff7af2015-02-04 22:54:26 +000049
Nils Diewald2f2b0672014-11-25 20:26:22 +000050 /**
Nils Diewald0881e242015-02-27 17:31:01 +000051 * Construct a new Response object.
Nils Diewald2f2b0672014-11-25 20:26:22 +000052 */
Nils Diewald0881e242015-02-27 17:31:01 +000053 public Response () {};
Nils Diewaldc471b182014-11-19 22:51:15 +000054
Nils Diewaldc471b182014-11-19 22:51:15 +000055
56 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +000057 * Get string representation of the backend's version.
Nils Diewaldbb33da22015-03-04 16:24:25 +000058 *
Nils Diewald44d5fa12015-01-15 21:31:52 +000059 * @return String representation of the backend's version
60 */
Nils Diewald44d5fa12015-01-15 21:31:52 +000061 public String getVersion () {
62 return this.version;
63 };
64
65
66 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +000067 * Set the string representation of the backend's version.
Nils Diewaldbb33da22015-03-04 16:24:25 +000068 *
69 * @param version
70 * The string representation of the backend's version
Nils Diewald0881e242015-02-27 17:31:01 +000071 * @return Response object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +000072 */
Nils Diewald0881e242015-02-27 17:31:01 +000073 public Response setVersion (String fullVersion) {
Nils Diewaldc99ed5b2015-01-21 22:08:53 +000074 int found = fullVersion.lastIndexOf('-');
75
76 // Is combined name and version
77 if (found > 0 && (found + 1 < fullVersion.length())) {
78 this.setName(fullVersion.substring(0, found));
79 this.version = fullVersion.substring(found + 1);
80 }
81 // Is only version number
82 else {
83 this.version = fullVersion;
84 };
Nils Diewald94870ae2014-12-09 14:35:29 +000085 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +000086 };
87
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000088
Nils Diewaldc471b182014-11-19 22:51:15 +000089 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +000090 * Get string representation of the backend's name.
91 * All nodes in a cluster should have the same backend name.
Nils Diewaldbb33da22015-03-04 16:24:25 +000092 *
Nils Diewald44d5fa12015-01-15 21:31:52 +000093 * @return String representation of the backend's name
Nils Diewaldc471b182014-11-19 22:51:15 +000094 */
Nils Diewald44d5fa12015-01-15 21:31:52 +000095 public String getName () {
96 return this.name;
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 Diewald2f2b0672014-11-25 20:26:22 +0000101 * Set the 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 *
104 * @param name
105 * The string representation of the backend's name
Nils Diewald0881e242015-02-27 17:31:01 +0000106 * @return Response object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +0000107 */
Nils Diewald0881e242015-02-27 17:31:01 +0000108 public Response setName (String name) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000109 this.name = name;
110 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000111 };
112
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000113
Nils Diewald2f2b0672014-11-25 20:26:22 +0000114 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000115 * Get string representation of the node's name.
116 * Each node in a cluster has a unique name.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000117 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000118 * @return String representation of the node's name
Nils Diewald2f2b0672014-11-25 20:26:22 +0000119 */
Nils Diewald44d5fa12015-01-15 21:31:52 +0000120 public String getNode () {
121 return this.node;
Nils Diewaldc471b182014-11-19 22:51:15 +0000122 };
123
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000124
Nils Diewald2f2b0672014-11-25 20:26:22 +0000125 /**
126 * Set the string representation of the node's name.
127 * Each node in a cluster has a unique name.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000128 *
129 * @param version
130 * The string representation of the node's name
Nils Diewald0881e242015-02-27 17:31:01 +0000131 * @return Response object for chaining
Nils Diewald2f2b0672014-11-25 20:26:22 +0000132 */
Nils Diewald0881e242015-02-27 17:31:01 +0000133 public Response setNode (String name) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000134 this.node = name;
135 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000136 };
137
Nils Diewald2f2b0672014-11-25 20:26:22 +0000138
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000139 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000140 * Check if the response time was exceeded.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000141 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000142 * @return <tt>true</tt> in case the response had a timeout,
143 * otherwise <tt>false</tt>
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000144 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000145 @JsonIgnore
Nils Diewald44d5fa12015-01-15 21:31:52 +0000146 public boolean hasTimeExceeded () {
147 return this.timeExceeded;
Nils Diewald2f2b0672014-11-25 20:26:22 +0000148 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000149
Nils Diewald2f2b0672014-11-25 20:26:22 +0000150
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000151 /**
152 * Set to <tt>true</tt> if time is exceeded
153 * based on a timeout.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000154 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000155 * <p>
156 * Will add a warning (682) to the output.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000157 *
158 * @param timeout
159 * Either <tt>true</tt> or <tt>false</tt>,
160 * in case the response timed out
Nils Diewald0881e242015-02-27 17:31:01 +0000161 * @return Response object for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000162 */
Nils Diewald0881e242015-02-27 17:31:01 +0000163 public Response setTimeExceeded (boolean timeout) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000164 if (timeout)
165 this.addWarning(682, "Response time exceeded");
166 this.timeExceeded = timeout;
167 return this;
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000168 };
169
170
171 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000172 * Get the benchmark time as a string.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000173 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000174 * @return String representation of the benchmark
175 * (including trailing time unit)
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000176 */
Nils Diewald44d5fa12015-01-15 21:31:52 +0000177 public String getBenchmark () {
178 return this.benchmark;
Nils Diewald2f2b0672014-11-25 20:26:22 +0000179 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000180
Nils Diewald2f2b0672014-11-25 20:26:22 +0000181
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000182 /**
183 * Set the benchmark as timestamp differences.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000184 *
185 * @param ts1
186 * Starting time of the benchmark
187 * @param ts2
188 * Ending time of the benchmark
Nils Diewald0881e242015-02-27 17:31:01 +0000189 * @return Response object for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000190 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000191 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000192 public Response setBenchmark (long ts1, long ts2) {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000193 this.benchmark = (ts2 - ts1) < 100_000_000 ?
194 // Store as miliseconds
195 (((double) (ts2 - ts1) * 1e-6) + " ms")
196 :
197 // Store as seconds
198 (((double) (ts2 - ts1) / 1000000000.0) + " s");
Nils Diewald94870ae2014-12-09 14:35:29 +0000199 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000200 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000201
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000202
203 /**
204 * Set the benchmark as a string representation.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000205 *
206 * @param bm
207 * String representation of a benchmark
208 * (including trailing time unit)
Nils Diewald0881e242015-02-27 17:31:01 +0000209 * @return Response for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000210 */
Nils Diewald0881e242015-02-27 17:31:01 +0000211 public Response setBenchmark (String bm) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000212 this.benchmark = bm;
213 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000214 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000215
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000216
217 /**
Nils Diewald44d5fa12015-01-15 21:31:52 +0000218 * Get the listener URI as a string.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000219 *
Nils Diewald44d5fa12015-01-15 21:31:52 +0000220 * @return The listener URI as a string representation
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000221 */
Nils Diewald44d5fa12015-01-15 21:31:52 +0000222 public String getListener () {
223 return this.listener;
Nils Diewaldc471b182014-11-19 22:51:15 +0000224 };
Nils Diewald44d5fa12015-01-15 21:31:52 +0000225
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000226
227 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +0000228 * Set the listener URI as a String. This is probably the
229 * localhost
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000230 * with an arbitrary port, like
Nils Diewaldbb33da22015-03-04 16:24:25 +0000231 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000232 * <p>
233 * <blockquote><pre>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000234 * http://localhost:8080/
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000235 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000236 *
237 * @param listener
238 * String representation of the listener URI
Nils Diewald0881e242015-02-27 17:31:01 +0000239 * @return Response object for chaining
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000240 */
Nils Diewald0881e242015-02-27 17:31:01 +0000241 public Response setListener (String listener) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000242 this.listener = listener;
243 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000244 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000245
Nils Diewaldc471b182014-11-19 22:51:15 +0000246
247 /**
Nils Diewaldafab8f32015-01-26 19:11:32 +0000248 * Get the total number of results.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000249 *
Nils Diewaldafab8f32015-01-26 19:11:32 +0000250 * @return The total number of results.
251 */
252 public long getTotalResults () {
253 if (this.totalResults == -2)
254 return (long) 0;
255 return this.totalResults;
256 };
257
258
259 /**
260 * Set the total number of results.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000261 *
262 * @param results
263 * The total number of results.
Nils Diewald0881e242015-02-27 17:31:01 +0000264 * @return {link Response} object for chaining.
Nils Diewaldafab8f32015-01-26 19:11:32 +0000265 */
Nils Diewald0881e242015-02-27 17:31:01 +0000266 public Response setTotalResults (long results) {
Nils Diewaldafab8f32015-01-26 19:11:32 +0000267 this.totalResults = results;
268 return this;
269 };
270
271
272 /**
273 * Increment the total number of results by a certain value.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000274 *
275 * @param incr
276 * The number of results the total number should
277 * be incremented by.
Nils Diewald0881e242015-02-27 17:31:01 +0000278 * @return {@link Response} object for chaining.
Nils Diewaldafab8f32015-01-26 19:11:32 +0000279 */
Nils Diewald0881e242015-02-27 17:31:01 +0000280 public Response incrTotalResults (int incr) {
Nils Diewaldafab8f32015-01-26 19:11:32 +0000281 if (this.totalResults < 0)
282 this.totalResults = incr;
283 else
284 this.totalResults += incr;
285 return this;
286 };
287
288
289 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000290 * Get the total number of resources the total number of
Nils Diewaldafab8f32015-01-26 19:11:32 +0000291 * results occur in.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000292 *
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000293 * @return The total number of resources the total number of
Nils Diewaldafab8f32015-01-26 19:11:32 +0000294 * results occur in.
295 */
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000296 public long getTotalResources () {
297 if (this.totalResources == -2)
Nils Diewaldafab8f32015-01-26 19:11:32 +0000298 return (long) 0;
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000299 return this.totalResources;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000300 };
301
302
303 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000304 * Set the total number of resources the total number of
Nils Diewaldafab8f32015-01-26 19:11:32 +0000305 * results occur in.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000306 *
307 * @param resources
308 * The total number of resources the total
309 * number of results occur in.
Nils Diewald0881e242015-02-27 17:31:01 +0000310 * @return {@link Response} object for chaining.
Nils Diewaldafab8f32015-01-26 19:11:32 +0000311 */
Nils Diewald0881e242015-02-27 17:31:01 +0000312 public Response setTotalResources (long resources) {
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000313 this.totalResources = resources;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000314 return this;
315 };
316
317
318 /**
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000319 * Increment the total number of resources the total number
Nils Diewaldafab8f32015-01-26 19:11:32 +0000320 * of results occur in by a certain value.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000321 *
322 * @param incr
323 * The number of resources the total number of
324 * results occur in should be incremented by.
325 * (I don't care that this isn't English!)
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 incrTotalResources (int i) {
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000329 if (this.totalResources < 0)
330 this.totalResources = i;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000331 else
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000332 this.totalResources += i;
Nils Diewaldafab8f32015-01-26 19:11:32 +0000333 return this;
334 };
335
336
337 /**
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000338 * Get the KoralQuery query object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000339 *
Nils Diewald0339d462015-02-26 14:53:56 +0000340 * @return The {@link KrillQuery} object,
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000341 * representing the KoralQuery query object.
342 */
Nils Diewald8904c1d2015-02-26 16:13:18 +0000343 // TODO: "tokens" shouldn't be fixed.
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000344 @JsonIgnore
Nils Diewald0339d462015-02-26 14:53:56 +0000345 public KrillQuery getQuery () {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000346 if (this.query == null)
Nils Diewald8904c1d2015-02-26 16:13:18 +0000347 this.query = new KrillQuery("tokens");
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000348 return this.query;
349 };
350
351
352 /**
353 * Set the KoralQuery query object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000354 *
355 * @param query
356 * The {@link KrillQuery} object,
357 * representing the KoralQuery query object.
Nils Diewald0881e242015-02-27 17:31:01 +0000358 * @return The {@link Response} object for chaining
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000359 */
360 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000361 public Response setQuery (KrillQuery query) {
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000362 this.query = query;
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000363
364 // Move messages from the query
Nils Diewald0881e242015-02-27 17:31:01 +0000365 return (Response) this.moveNotificationsFrom(query);
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000366 };
367
368
369 /**
370 * Get the associated collection object.
371 * In case no collection information was defined yet,
Nils Diewald2d5f8102015-02-26 21:07:54 +0000372 * a new {@link KrillCollection} object will be created.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000373 *
Nils Diewald2d5f8102015-02-26 21:07:54 +0000374 * @return The attached {@link KrillCollection} object.
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000375 */
376 @JsonIgnore
Nils Diewald2d5f8102015-02-26 21:07:54 +0000377 public KrillCollection getCollection () {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000378 if (this.collection == null)
Nils Diewald2d5f8102015-02-26 21:07:54 +0000379 this.collection = new KrillCollection();
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000380 return this.collection;
381 };
382
383
384 /**
Nils Diewald2d5f8102015-02-26 21:07:54 +0000385 * Set a new {@link KrillCollection} object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000386 *
387 * @param collection
388 * A {@link KrillCollection} object.
Nils Diewald0881e242015-02-27 17:31:01 +0000389 * @return The {@link Response} object for chaining
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000390 */
391 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000392 public Response setCollection (KrillCollection collection) {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000393 this.collection = collection;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000394
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000395 // Move messages from the collection
Nils Diewald0881e242015-02-27 17:31:01 +0000396 return (Response) this.moveNotificationsFrom(collection);
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000397 };
398
Nils Diewaldbb33da22015-03-04 16:24:25 +0000399
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000400 /**
401 * Get the associated meta object.
402 * In case no meta information was defined yet,
403 * a new {@link KrillMeta} object will be created.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000404 *
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000405 * @return The attached {@link KrillMeta} object.
406 */
407 @JsonIgnore
408 public KrillMeta getMeta () {
409 if (this.meta == null)
410 this.meta = new KrillMeta();
411 return this.meta;
412 };
413
414
415 /**
416 * Set a new {@link KrillMeta} object.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000417 *
418 * @param meta
419 * A {@link KrillMeta} object.
Nils Diewald0881e242015-02-27 17:31:01 +0000420 * @return The {@link Response} object for chaining
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000421 */
422 @JsonIgnore
Nils Diewald0881e242015-02-27 17:31:01 +0000423 public Response setMeta (KrillMeta meta) {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000424 this.meta = meta;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000425
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000426 // Move messages from the collection
Nils Diewald0881e242015-02-27 17:31:01 +0000427 return (Response) this.moveNotificationsFrom(meta);
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000428 };
429
430
431 /**
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000432 * Serialize response as a {@link JsonNode}.
Nils Diewaldbb33da22015-03-04 16:24:25 +0000433 *
Nils Diewaldc99ed5b2015-01-21 22:08:53 +0000434 * @return {@link JsonNode} representation of the response
Nils Diewaldc471b182014-11-19 22:51:15 +0000435 */
436 @Override
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000437 public JsonNode toJsonNode () {
Nils Diewaldc471b182014-11-19 22:51:15 +0000438
Nils Diewald94870ae2014-12-09 14:35:29 +0000439 // Get notifications json response
440 ObjectNode json = (ObjectNode) super.toJsonNode();
Nils Diewaldc471b182014-11-19 22:51:15 +0000441
Nils Diewald94870ae2014-12-09 14:35:29 +0000442 StringBuilder sb = new StringBuilder();
Nils Diewaldc471b182014-11-19 22:51:15 +0000443 if (this.getName() != null) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000444 sb.append(this.getName());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000445
Nils Diewald94870ae2014-12-09 14:35:29 +0000446 if (this.getVersion() != null)
447 sb.append("-");
448 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000449
Nils Diewald94870ae2014-12-09 14:35:29 +0000450 // No name but version is given
451 if (this.getVersion() != null)
452 sb.append(this.getVersion());
Nils Diewaldc471b182014-11-19 22:51:15 +0000453
Nils Diewald94870ae2014-12-09 14:35:29 +0000454 if (sb.length() > 0)
455 json.put("version", sb.toString());
Nils Diewaldbb33da22015-03-04 16:24:25 +0000456
Nils Diewald94870ae2014-12-09 14:35:29 +0000457 if (this.timeExceeded)
458 json.put("timeExceeded", true);
Nils Diewaldbb33da22015-03-04 16:24:25 +0000459
Nils Diewaldc471b182014-11-19 22:51:15 +0000460 if (this.getNode() != null)
461 json.put("node", this.getNode());
462
463 if (this.getListener() != null)
464 json.put("listener", this.getListener());
465
466 if (this.getBenchmark() != null)
467 json.put("benchmark", this.getBenchmark());
468
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000469 // totalResources is set
Nils Diewaldd75e6f62015-01-28 23:44:56 +0000470 if (this.totalResources != -2)
471 json.put("totalResources", this.totalResources);
Nils Diewaldbb33da22015-03-04 16:24:25 +0000472
Nils Diewaldafab8f32015-01-26 19:11:32 +0000473 // totalResults is set
474 if (this.totalResults != -2)
475 json.put("totalResults", this.totalResults);
476
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000477 // KoralQuery query object
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000478 if (this.query != null) {
479 JsonNode queryNode = this.getQuery().toJsonNode();
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000480 if (queryNode != null)
481 json.put("query", queryNode);
482 };
483
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000484 // KoralQuery meta object
485 if (this.meta != null) {
486 JsonNode metaNode = this.meta.toJsonNode();
487 if (metaNode != null)
488 json.put("meta", metaNode);
489 };
490
491 // KoralQuery collection object
Nils Diewaldbb33da22015-03-04 16:24:25 +0000492 if (this.collection != null
493 && this.collection.getFilters().toArray().length > 0) {
Nils Diewaldf5ab4b22015-02-25 20:55:16 +0000494 JsonNode collNode = this.collection.toJsonNode();
495 if (collNode != null)
496 json.put("collection", collNode);
497 };
498
Nils Diewald94870ae2014-12-09 14:35:29 +0000499 return (JsonNode) json;
Nils Diewaldc471b182014-11-19 22:51:15 +0000500 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000501
Nils Diewald44d5fa12015-01-15 21:31:52 +0000502
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000503 /**
504 * Serialize response as a JSON string.
505 * <p>
506 * <blockquote><pre>
507 * {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000508 * "version" : "Lucene-Backend-0.49.1",
509 * "timeExceeded" : true,
510 * "node" : "Tanja",
511 * "listener" : "http://localhost:8080/",
512 * "benchmark" : "12.3s",
513 * "errors": [
514 * [123, "You are not allowed to serialize these messages"],
515 * [124, "Your request was invalid"]
516 * ],
517 * "messages" : [
518 * [125, "Class is deprecated", "Notifications"]
519 * ]
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000520 * }
521 * </pre></blockquote>
Nils Diewaldbb33da22015-03-04 16:24:25 +0000522 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000523 * @return String representation of the response
524 */
525 public String toJsonString () {
Nils Diewald94870ae2014-12-09 14:35:29 +0000526 String msg = "";
527 try {
528 return mapper.writeValueAsString(this.toJsonNode());
529 }
530 catch (Exception e) {
531 // Bad in case the message contains quotes!
532 msg = ", \"" + e.getLocalizedMessage() + "\"";
533 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000534
Nils Diewaldbb33da22015-03-04 16:24:25 +0000535 return "{\"errors\":[" + "[620, " + "\"Unable to generate JSON\"" + msg
536 + "]" + "]}";
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000537 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000538};