| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response; |
| 2 | |
| 3 | import java.util.*; |
| 4 | import java.io.*; |
| 5 | |
| 6 | import com.fasterxml.jackson.annotation.*; |
| 7 | import com.fasterxml.jackson.databind.JsonNode; |
| 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 10 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 11 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 12 | import de.ids_mannheim.korap.response.Notifications; |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 13 | import de.ids_mannheim.korap.response.serialize.KorapResponseDeserializer; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 14 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 15 | /** |
| 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 Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 22 | * km.toJsonString() |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 23 | * ); |
| 24 | * </pre></blockquote> |
| 25 | * |
| 26 | * @author Nils Diewald |
| 27 | * @see de.ids_mannheim.korap.response.Notifications |
| 28 | */ |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 29 | @JsonDeserialize(using = KorapResponseDeserializer.class) |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 30 | public class KorapResponse extends Notifications { |
| 31 | ObjectMapper mapper = new ObjectMapper(); |
| 32 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 33 | private String version, name, node, listener; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 34 | private String benchmark; |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 35 | private boolean timeExceeded = false; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 36 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Construct a new KorapResponse object. |
| 39 | * |
| 40 | * @return The new KorapResponse object |
| 41 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 42 | public KorapResponse () {}; |
| 43 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 46 | * Set the string representation of the backend's version. |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 47 | * |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 48 | * @param version The string representation of the backend's version |
| 49 | * @return KorapResponse object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 50 | */ |
| 51 | @JsonIgnore |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 52 | public KorapResponse setVersion (String version) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 53 | this.version = version; |
| 54 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 57 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 58 | /** |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 59 | * Get string representation of the backend's version. |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 60 | * |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 61 | * @return String representation of the backend's version |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 62 | */ |
| 63 | @JsonIgnore |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 64 | public String getVersion () { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 65 | return this.version; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 68 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 69 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 70 | * Set the string representation of the backend's name. |
| 71 | * All nodes in a cluster should have the same backend name. |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 72 | * |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 73 | * @param version The string representation of the backend's name |
| 74 | * @return KorapResponse object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 75 | */ |
| 76 | @JsonIgnore |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 77 | public KorapResponse setName (String name) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 78 | this.name = name; |
| 79 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 82 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 83 | /** |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 84 | * Get string representation of the backend's name. |
| 85 | * All nodes in a cluster should have the same backend name. |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 86 | * |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 87 | * @return String representation of the backend's name |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 88 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 89 | @JsonIgnore |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 90 | public String getName () { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 91 | return this.name; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 94 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 95 | /** |
| 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 Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 102 | @JsonIgnore |
| 103 | public KorapResponse setNode (String name) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 104 | this.node = name; |
| 105 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 108 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 109 | /** |
| 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 Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 115 | @JsonIgnore |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 116 | public String getNode () { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 117 | return this.node; |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 118 | }; |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 119 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 120 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 121 | /** |
| 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 Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 134 | if (timeout) |
| 135 | this.addWarning(682, "Response time exceeded"); |
| 136 | this.timeExceeded = timeout; |
| 137 | return this; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 138 | }; |
| 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 Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 147 | @JsonIgnore |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 148 | public boolean hasTimeExceeded () { |
| 149 | return this.timeExceeded; |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 153 | /** |
| 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 Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 160 | @JsonIgnore |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 161 | public KorapResponse setBenchmark (long ts1, long ts2) { |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 162 | this.benchmark = |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 163 | (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 Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 169 | }; |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 170 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 171 | |
| 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 Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 179 | @JsonIgnore |
| 180 | public KorapResponse setBenchmark (String bm) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 181 | this.benchmark = bm; |
| 182 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Get the benchmark time as a string. |
| 188 | * |
| 189 | * @return String representation of the benchmark |
| 190 | * (including trailing time unit) |
| 191 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 192 | @JsonIgnore |
| 193 | public String getBenchmark () { |
| 194 | return this.benchmark; |
| 195 | }; |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 196 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 197 | |
| 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 Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 210 | @JsonIgnore |
| 211 | public KorapResponse setListener (String listener) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 212 | this.listener = listener; |
| 213 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 214 | }; |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 215 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * Get the listener URI as a string. |
| 219 | * |
| 220 | * @return The listener URI as a string representation |
| 221 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 222 | @JsonIgnore |
| 223 | public String getListener () { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 224 | return this.listener; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 227 | |
| 228 | /** |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 229 | * Serialize response as a JsonNode. |
| 230 | * |
| 231 | * @return JsonNode representation of the response |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 232 | */ |
| 233 | @Override |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 234 | public JsonNode toJsonNode () { |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 235 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 236 | // Get notifications json response |
| 237 | ObjectNode json = (ObjectNode) super.toJsonNode(); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 238 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 239 | StringBuilder sb = new StringBuilder(); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 240 | if (this.getName() != null) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 241 | sb.append(this.getName()); |
| 242 | |
| 243 | if (this.getVersion() != null) |
| 244 | sb.append("-"); |
| 245 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 246 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 247 | // No name but version is given |
| 248 | if (this.getVersion() != null) |
| 249 | sb.append(this.getVersion()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 250 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 251 | if (sb.length() > 0) |
| 252 | json.put("version", sb.toString()); |
| 253 | |
| 254 | if (this.timeExceeded) |
| 255 | json.put("timeExceeded", true); |
| 256 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 257 | 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 Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 266 | return (JsonNode) json; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 267 | }; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 268 | |
| 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 Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 292 | 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 Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 300 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 301 | return |
| 302 | "{\"errors\":["+ |
| 303 | "[620, " + |
| 304 | "\"Unable to generate JSON\"" + msg + "]" + |
| 305 | "]}"; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 306 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 307 | }; |