blob: c826f714cc4ec3ad83501e8d5af92cd62432b167 [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.*;
7import com.fasterxml.jackson.databind.JsonNode;
8import com.fasterxml.jackson.databind.ObjectMapper;
9import com.fasterxml.jackson.databind.node.ObjectNode;
Nils Diewald94870ae2014-12-09 14:35:29 +000010import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
11
Nils Diewaldc471b182014-11-19 22:51:15 +000012import de.ids_mannheim.korap.response.Notifications;
Nils Diewald94870ae2014-12-09 14:35:29 +000013import de.ids_mannheim.korap.response.serialize.KorapResponseDeserializer;
Nils Diewaldc471b182014-11-19 22:51:15 +000014
Nils Diewald2f2b0672014-11-25 20:26:22 +000015/**
16 * Base class for objects meant to be responded by the server.
17 *
18 * <p>
19 * <blockquote><pre>
20 * KorapResponse km = new KorapResponse();
21 * System.out.println(
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000022 * km.toJsonString()
Nils Diewald2f2b0672014-11-25 20:26:22 +000023 * );
24 * </pre></blockquote>
25 *
26 * @author Nils Diewald
27 * @see de.ids_mannheim.korap.response.Notifications
28 */
Nils Diewald94870ae2014-12-09 14:35:29 +000029@JsonDeserialize(using = KorapResponseDeserializer.class)
Nils Diewaldc471b182014-11-19 22:51:15 +000030public class KorapResponse extends Notifications {
31 ObjectMapper mapper = new ObjectMapper();
32
Nils Diewald2f2b0672014-11-25 20:26:22 +000033 private String version, name, node, listener;
Nils Diewaldc471b182014-11-19 22:51:15 +000034 private String benchmark;
Nils Diewald2f2b0672014-11-25 20:26:22 +000035 private boolean timeExceeded = false;
Nils Diewaldc471b182014-11-19 22:51:15 +000036
Nils Diewald2f2b0672014-11-25 20:26:22 +000037 /**
38 * Construct a new KorapResponse object.
39 *
40 * @return The new KorapResponse object
41 */
Nils Diewaldc471b182014-11-19 22:51:15 +000042 public KorapResponse () {};
43
Nils Diewaldc471b182014-11-19 22:51:15 +000044
45 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +000046 * Set the string representation of the backend's version.
Nils Diewaldc471b182014-11-19 22:51:15 +000047 *
Nils Diewald2f2b0672014-11-25 20:26:22 +000048 * @param version The string representation of the backend's version
49 * @return KorapResponse object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +000050 */
51 @JsonIgnore
Nils Diewald2f2b0672014-11-25 20:26:22 +000052 public KorapResponse setVersion (String version) {
Nils Diewald94870ae2014-12-09 14:35:29 +000053 this.version = version;
54 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +000055 };
56
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000057
Nils Diewaldc471b182014-11-19 22:51:15 +000058 /**
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000059 * Get string representation of the backend's version.
Nils Diewald2f2b0672014-11-25 20:26:22 +000060 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000061 * @return String representation of the backend's version
Nils Diewaldc471b182014-11-19 22:51:15 +000062 */
63 @JsonIgnore
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000064 public String getVersion () {
Nils Diewald94870ae2014-12-09 14:35:29 +000065 return this.version;
Nils Diewaldc471b182014-11-19 22:51:15 +000066 };
67
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000068
Nils Diewaldc471b182014-11-19 22:51:15 +000069 /**
Nils Diewald2f2b0672014-11-25 20:26:22 +000070 * Set the string representation of the backend's name.
71 * All nodes in a cluster should have the same backend name.
Nils Diewaldc471b182014-11-19 22:51:15 +000072 *
Nils Diewald2f2b0672014-11-25 20:26:22 +000073 * @param version The string representation of the backend's name
74 * @return KorapResponse object for chaining
Nils Diewaldc471b182014-11-19 22:51:15 +000075 */
76 @JsonIgnore
Nils Diewald2f2b0672014-11-25 20:26:22 +000077 public KorapResponse setName (String name) {
Nils Diewald94870ae2014-12-09 14:35:29 +000078 this.name = name;
79 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +000080 };
81
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000082
Nils Diewald2f2b0672014-11-25 20:26:22 +000083 /**
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000084 * Get string representation of the backend's name.
85 * All nodes in a cluster should have the same backend name.
Nils Diewald2f2b0672014-11-25 20:26:22 +000086 *
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000087 * @return String representation of the backend's name
Nils Diewald2f2b0672014-11-25 20:26:22 +000088 */
Nils Diewaldc471b182014-11-19 22:51:15 +000089 @JsonIgnore
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000090 public String getName () {
Nils Diewald94870ae2014-12-09 14:35:29 +000091 return this.name;
Nils Diewaldc471b182014-11-19 22:51:15 +000092 };
93
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000094
Nils Diewald2f2b0672014-11-25 20:26:22 +000095 /**
96 * Set the string representation of the node's name.
97 * Each node in a cluster has a unique name.
98 *
99 * @param version The string representation of the node's name
100 * @return KorapResponse object for chaining
101 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000102 @JsonIgnore
103 public KorapResponse setNode (String name) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000104 this.node = name;
105 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000106 };
107
Nils Diewald2f2b0672014-11-25 20:26:22 +0000108
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000109 /**
110 * Get string representation of the node's name.
111 * Each node in a cluster has a unique name.
112 *
113 * @return String representation of the node's name
114 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000115 @JsonIgnore
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000116 public String getNode () {
Nils Diewald94870ae2014-12-09 14:35:29 +0000117 return this.node;
Nils Diewald2f2b0672014-11-25 20:26:22 +0000118 };
Nils Diewald94870ae2014-12-09 14:35:29 +0000119
Nils Diewald2f2b0672014-11-25 20:26:22 +0000120
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000121 /**
122 * Set to <tt>true</tt> if time is exceeded
123 * based on a timeout.
124 *
125 * <p>
126 * Will add a warning (682) to the output.
127 *
128 * @param timeout Either <tt>true</tt> or <tt>false</tt>, in case the response
129 * timed out
130 * @return KorapResponse object for chaining
131 */
132 @JsonIgnore
133 public KorapResponse setTimeExceeded (boolean timeout) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000134 if (timeout)
135 this.addWarning(682, "Response time exceeded");
136 this.timeExceeded = timeout;
137 return this;
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000138 };
139
140
141 /**
142 * Check if the response time was exceeded.
143 *
144 * @return <tt>true</tt> in case the response had a timeout,
145 * otherwise <tt>false</tt>
146 */
Nils Diewald2f2b0672014-11-25 20:26:22 +0000147 @JsonIgnore
Nils Diewald94870ae2014-12-09 14:35:29 +0000148 public boolean hasTimeExceeded () {
149 return this.timeExceeded;
Nils Diewald2f2b0672014-11-25 20:26:22 +0000150 };
151
152
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000153 /**
154 * Set the benchmark as timestamp differences.
155 *
156 * @param ts1 Starting time of the benchmark
157 * @param ts2 Ending time of the benchmark
158 * @return KorapResponse object for chaining
159 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000160 @JsonIgnore
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000161 public KorapResponse setBenchmark (long ts1, long ts2) {
Nils Diewaldc471b182014-11-19 22:51:15 +0000162 this.benchmark =
Nils Diewald94870ae2014-12-09 14:35:29 +0000163 (ts2 - ts1) < 100_000_000 ?
164 // Store as miliseconds
165 (((double) (ts2 - ts1) * 1e-6) + " ms") :
166 // Store as seconds
167 (((double) (ts2 - ts1) / 1000000000.0) + " s");
168 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000169 };
Nils Diewald94870ae2014-12-09 14:35:29 +0000170
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000171
172 /**
173 * Set the benchmark as a string representation.
174 *
175 * @param bm String representation of a benchmark
176 * (including trailing time unit)
177 * @return KorapResponse for chaining
178 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000179 @JsonIgnore
180 public KorapResponse setBenchmark (String bm) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000181 this.benchmark = bm;
182 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000183 };
184
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000185
186 /**
187 * Get the benchmark time as a string.
188 *
189 * @return String representation of the benchmark
190 * (including trailing time unit)
191 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000192 @JsonIgnore
193 public String getBenchmark () {
194 return this.benchmark;
195 };
Nils Diewald94870ae2014-12-09 14:35:29 +0000196
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000197
198 /**
199 * Set the listener URI as a String. This is probably the localhost
200 * with an arbitrary port, like
201 *
202 * <p>
203 * <blockquote><pre>
204 * http://localhost:8080/
205 * </pre></blockquote>
206 *
207 * @param listener String representation of the listener URI
208 * @return KorapResponse object for chaining
209 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000210 @JsonIgnore
211 public KorapResponse setListener (String listener) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000212 this.listener = listener;
213 return this;
Nils Diewaldc471b182014-11-19 22:51:15 +0000214 };
Nils Diewald94870ae2014-12-09 14:35:29 +0000215
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000216
217 /**
218 * Get the listener URI as a string.
219 *
220 * @return The listener URI as a string representation
221 */
Nils Diewaldc471b182014-11-19 22:51:15 +0000222 @JsonIgnore
223 public String getListener () {
Nils Diewald94870ae2014-12-09 14:35:29 +0000224 return this.listener;
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000225 };
226
Nils Diewaldc471b182014-11-19 22:51:15 +0000227
228 /**
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000229 * Serialize response as a JsonNode.
230 *
231 * @return JsonNode representation of the response
Nils Diewaldc471b182014-11-19 22:51:15 +0000232 */
233 @Override
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000234 public JsonNode toJsonNode () {
Nils Diewaldc471b182014-11-19 22:51:15 +0000235
Nils Diewald94870ae2014-12-09 14:35:29 +0000236 // Get notifications json response
237 ObjectNode json = (ObjectNode) super.toJsonNode();
Nils Diewaldc471b182014-11-19 22:51:15 +0000238
Nils Diewald94870ae2014-12-09 14:35:29 +0000239 StringBuilder sb = new StringBuilder();
Nils Diewaldc471b182014-11-19 22:51:15 +0000240 if (this.getName() != null) {
Nils Diewald94870ae2014-12-09 14:35:29 +0000241 sb.append(this.getName());
242
243 if (this.getVersion() != null)
244 sb.append("-");
245 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000246
Nils Diewald94870ae2014-12-09 14:35:29 +0000247 // No name but version is given
248 if (this.getVersion() != null)
249 sb.append(this.getVersion());
Nils Diewaldc471b182014-11-19 22:51:15 +0000250
Nils Diewald94870ae2014-12-09 14:35:29 +0000251 if (sb.length() > 0)
252 json.put("version", sb.toString());
253
254 if (this.timeExceeded)
255 json.put("timeExceeded", true);
256
Nils Diewaldc471b182014-11-19 22:51:15 +0000257 if (this.getNode() != null)
258 json.put("node", this.getNode());
259
260 if (this.getListener() != null)
261 json.put("listener", this.getListener());
262
263 if (this.getBenchmark() != null)
264 json.put("benchmark", this.getBenchmark());
265
Nils Diewald94870ae2014-12-09 14:35:29 +0000266 return (JsonNode) json;
Nils Diewaldc471b182014-11-19 22:51:15 +0000267 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000268
269 /**
270 * Serialize response as a JSON string.
271 * <p>
272 * <blockquote><pre>
273 * {
274 * "version" : "Lucene-Backend-0.49.1",
275 * "timeExceeded" : true,
276 * "node" : "Tanja",
277 * "listener" : "http://localhost:8080/",
278 * "benchmark" : "12.3s",
279 * "errors": [
280 * [123, "You are not allowed to serialize these messages"],
281 * [124, "Your request was invalid"]
282 * ],
283 * "messages" : [
284 * [125, "Class is deprecated", "Notifications"]
285 * ]
286 * }
287 * </pre></blockquote>
288 *
289 * @return String representation of the response
290 */
291 public String toJsonString () {
Nils Diewald94870ae2014-12-09 14:35:29 +0000292 String msg = "";
293 try {
294 return mapper.writeValueAsString(this.toJsonNode());
295 }
296 catch (Exception e) {
297 // Bad in case the message contains quotes!
298 msg = ", \"" + e.getLocalizedMessage() + "\"";
299 };
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000300
Nils Diewald94870ae2014-12-09 14:35:29 +0000301 return
302 "{\"errors\":["+
303 "[620, " +
304 "\"Unable to generate JSON\"" + msg + "]" +
305 "]}";
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000306 };
Nils Diewaldc471b182014-11-19 22:51:15 +0000307};