| 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.*; |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 7 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 8 | import com.fasterxml.jackson.databind.JsonNode; |
| 9 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 11 | |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 12 | import de.ids_mannheim.korap.KrillCollection; |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 13 | import de.ids_mannheim.korap.KrillMeta; |
| Nils Diewald | 0339d46 | 2015-02-26 14:53:56 +0000 | [diff] [blame] | 14 | import de.ids_mannheim.korap.KrillQuery; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 15 | import de.ids_mannheim.korap.response.Notifications; |
| 16 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Base class for objects meant to be responded by the server. |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 19 | * This inherits KoralQuery requests. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 20 | * |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 21 | * <p> |
| 22 | * <blockquote><pre> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 23 | * Response km = new Response(); |
| 24 | * System.out.println( |
| 25 | * km.toJsonString() |
| 26 | * ); |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 27 | * </pre></blockquote> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 28 | * |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 29 | * @author diewald |
| 30 | * @see Notifications |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 31 | */ |
| Nils Diewald | 8904c1d | 2015-02-26 16:13:18 +0000 | [diff] [blame] | 32 | // Todo: Use configuration file to get default token field "tokens" |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 33 | @JsonInclude(Include.NON_NULL) |
| 34 | @JsonIgnoreProperties(ignoreUnknown = true) |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 35 | public class Response extends Notifications { |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 36 | ObjectMapper mapper = new ObjectMapper(); |
| 37 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 38 | private KrillMeta meta; |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 39 | private KrillCollection collection; |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 40 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 41 | private String version, name, node, listener; |
| Nils Diewald | 0339d46 | 2015-02-26 14:53:56 +0000 | [diff] [blame] | 42 | private KrillQuery query; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 43 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 44 | private long totalResources = -2, // Not set |
| 45 | totalResults = -2; // Not set |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 46 | private String benchmark; |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 47 | private boolean timeExceeded = false; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 48 | |
| Nils Diewald | 01ff7af | 2015-02-04 22:54:26 +0000 | [diff] [blame] | 49 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 50 | /** |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 51 | * Construct a new Response object. |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 52 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 53 | public Response () {}; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 54 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 57 | * Get string representation of the backend's version. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 58 | * |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 59 | * @return String representation of the backend's version |
| 60 | */ |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 61 | public String getVersion () { |
| 62 | return this.version; |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 67 | * Set the string representation of the backend's version. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 68 | * |
| 69 | * @param version |
| 70 | * The string representation of the backend's version |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 71 | * @return Response object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 72 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 73 | public Response setVersion (String fullVersion) { |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 74 | 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 Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 85 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 88 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 89 | /** |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 90 | * Get string representation of the backend's name. |
| 91 | * All nodes in a cluster should have the same backend name. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 92 | * |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 93 | * @return String representation of the backend's name |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 94 | */ |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 95 | public String getName () { |
| 96 | return this.name; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 99 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 100 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 101 | * Set the string representation of the backend's name. |
| 102 | * All nodes in a cluster should have the same backend name. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 103 | * |
| 104 | * @param name |
| 105 | * The string representation of the backend's name |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 106 | * @return Response object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 107 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 108 | public Response setName (String name) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 109 | this.name = name; |
| 110 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 113 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 114 | /** |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 115 | * Get string representation of the node's name. |
| 116 | * Each node in a cluster has a unique name. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 117 | * |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 118 | * @return String representation of the node's name |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 119 | */ |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 120 | public String getNode () { |
| 121 | return this.node; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 124 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Set the string representation of the node's name. |
| 127 | * Each node in a cluster has a unique name. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 128 | * |
| 129 | * @param version |
| 130 | * The string representation of the node's name |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 131 | * @return Response object for chaining |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 132 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 133 | public Response setNode (String name) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 134 | this.node = name; |
| 135 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 138 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 139 | /** |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 140 | * Check if the response time was exceeded. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 141 | * |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 142 | * @return <tt>true</tt> in case the response had a timeout, |
| 143 | * otherwise <tt>false</tt> |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 144 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 145 | @JsonIgnore |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 146 | public boolean hasTimeExceeded () { |
| 147 | return this.timeExceeded; |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 148 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 149 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 150 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 151 | /** |
| 152 | * Set to <tt>true</tt> if time is exceeded |
| 153 | * based on a timeout. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 154 | * |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 155 | * <p> |
| 156 | * Will add a warning (682) to the output. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 157 | * |
| 158 | * @param timeout |
| 159 | * Either <tt>true</tt> or <tt>false</tt>, |
| 160 | * in case the response timed out |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 161 | * @return Response object for chaining |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 162 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 163 | public Response setTimeExceeded (boolean timeout) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 164 | if (timeout) |
| 165 | this.addWarning(682, "Response time exceeded"); |
| 166 | this.timeExceeded = timeout; |
| 167 | return this; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | |
| 171 | /** |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 172 | * Get the benchmark time as a string. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 173 | * |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 174 | * @return String representation of the benchmark |
| 175 | * (including trailing time unit) |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 176 | */ |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 177 | public String getBenchmark () { |
| 178 | return this.benchmark; |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 179 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 180 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 181 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 182 | /** |
| 183 | * Set the benchmark as timestamp differences. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 184 | * |
| 185 | * @param ts1 |
| 186 | * Starting time of the benchmark |
| 187 | * @param ts2 |
| 188 | * Ending time of the benchmark |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 189 | * @return Response object for chaining |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 190 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 191 | @JsonIgnore |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 192 | public Response setBenchmark (long ts1, long ts2) { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 193 | 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 Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 199 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 200 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 201 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Set the benchmark as a string representation. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 205 | * |
| 206 | * @param bm |
| 207 | * String representation of a benchmark |
| 208 | * (including trailing time unit) |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 209 | * @return Response for chaining |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 210 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 211 | public Response setBenchmark (String bm) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 212 | this.benchmark = bm; |
| 213 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 214 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 215 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 216 | |
| 217 | /** |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 218 | * Get the listener URI as a string. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 219 | * |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 220 | * @return The listener URI as a string representation |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 221 | */ |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 222 | public String getListener () { |
| 223 | return this.listener; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 224 | }; |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 225 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 226 | |
| 227 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 228 | * Set the listener URI as a String. This is probably the |
| 229 | * localhost |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 230 | * with an arbitrary port, like |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 231 | * |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 232 | * <p> |
| 233 | * <blockquote><pre> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 234 | * http://localhost:8080/ |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 235 | * </pre></blockquote> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 236 | * |
| 237 | * @param listener |
| 238 | * String representation of the listener URI |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 239 | * @return Response object for chaining |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 240 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 241 | public Response setListener (String listener) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 242 | this.listener = listener; |
| 243 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 244 | }; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 245 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 246 | |
| 247 | /** |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 248 | * Get the total number of results. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 249 | * |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 250 | * @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 Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 261 | * |
| 262 | * @param results |
| 263 | * The total number of results. |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 264 | * @return {link Response} object for chaining. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 265 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 266 | public Response setTotalResults (long results) { |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 267 | this.totalResults = results; |
| 268 | return this; |
| 269 | }; |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * Increment the total number of results by a certain value. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 274 | * |
| 275 | * @param incr |
| 276 | * The number of results the total number should |
| 277 | * be incremented by. |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 278 | * @return {@link Response} object for chaining. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 279 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 280 | public Response incrTotalResults (int incr) { |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 281 | if (this.totalResults < 0) |
| 282 | this.totalResults = incr; |
| 283 | else |
| 284 | this.totalResults += incr; |
| 285 | return this; |
| 286 | }; |
| 287 | |
| 288 | |
| 289 | /** |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 290 | * Get the total number of resources the total number of |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 291 | * results occur in. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 292 | * |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 293 | * @return The total number of resources the total number of |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 294 | * results occur in. |
| 295 | */ |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 296 | public long getTotalResources () { |
| 297 | if (this.totalResources == -2) |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 298 | return (long) 0; |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 299 | return this.totalResources; |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | |
| 303 | /** |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 304 | * Set the total number of resources the total number of |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 305 | * results occur in. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 306 | * |
| 307 | * @param resources |
| 308 | * The total number of resources the total |
| 309 | * number of results occur in. |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 310 | * @return {@link Response} object for chaining. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 311 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 312 | public Response setTotalResources (long resources) { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 313 | this.totalResources = resources; |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 314 | return this; |
| 315 | }; |
| 316 | |
| 317 | |
| 318 | /** |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 319 | * Increment the total number of resources the total number |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 320 | * of results occur in by a certain value. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 321 | * |
| 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 Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 326 | * @return {@link Response} object for chaining. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 327 | */ |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 328 | public Response incrTotalResources (int i) { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 329 | if (this.totalResources < 0) |
| 330 | this.totalResources = i; |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 331 | else |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 332 | this.totalResources += i; |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 333 | return this; |
| 334 | }; |
| 335 | |
| 336 | |
| 337 | /** |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 338 | * Get the KoralQuery query object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 339 | * |
| Nils Diewald | 0339d46 | 2015-02-26 14:53:56 +0000 | [diff] [blame] | 340 | * @return The {@link KrillQuery} object, |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 341 | * representing the KoralQuery query object. |
| 342 | */ |
| Nils Diewald | 8904c1d | 2015-02-26 16:13:18 +0000 | [diff] [blame] | 343 | // TODO: "tokens" shouldn't be fixed. |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 344 | @JsonIgnore |
| Nils Diewald | 0339d46 | 2015-02-26 14:53:56 +0000 | [diff] [blame] | 345 | public KrillQuery getQuery () { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 346 | if (this.query == null) |
| Nils Diewald | 8904c1d | 2015-02-26 16:13:18 +0000 | [diff] [blame] | 347 | this.query = new KrillQuery("tokens"); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 348 | return this.query; |
| 349 | }; |
| 350 | |
| 351 | |
| 352 | /** |
| 353 | * Set the KoralQuery query object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 354 | * |
| 355 | * @param query |
| 356 | * The {@link KrillQuery} object, |
| 357 | * representing the KoralQuery query object. |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 358 | * @return The {@link Response} object for chaining |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 359 | */ |
| 360 | @JsonIgnore |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 361 | public Response setQuery (KrillQuery query) { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 362 | this.query = query; |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 363 | |
| 364 | // Move messages from the query |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 365 | return (Response) this.moveNotificationsFrom(query); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | |
| 369 | /** |
| 370 | * Get the associated collection object. |
| 371 | * In case no collection information was defined yet, |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 372 | * a new {@link KrillCollection} object will be created. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 373 | * |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 374 | * @return The attached {@link KrillCollection} object. |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 375 | */ |
| 376 | @JsonIgnore |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 377 | public KrillCollection getCollection () { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 378 | if (this.collection == null) |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 379 | this.collection = new KrillCollection(); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 380 | return this.collection; |
| 381 | }; |
| 382 | |
| 383 | |
| 384 | /** |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 385 | * Set a new {@link KrillCollection} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 386 | * |
| 387 | * @param collection |
| 388 | * A {@link KrillCollection} object. |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 389 | * @return The {@link Response} object for chaining |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 390 | */ |
| 391 | @JsonIgnore |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 392 | public Response setCollection (KrillCollection collection) { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 393 | this.collection = collection; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 394 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 395 | // Move messages from the collection |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 396 | return (Response) this.moveNotificationsFrom(collection); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 397 | }; |
| 398 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 399 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 400 | /** |
| 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 Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 404 | * |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 405 | * @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 Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 417 | * |
| 418 | * @param meta |
| 419 | * A {@link KrillMeta} object. |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 420 | * @return The {@link Response} object for chaining |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 421 | */ |
| 422 | @JsonIgnore |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 423 | public Response setMeta (KrillMeta meta) { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 424 | this.meta = meta; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 425 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 426 | // Move messages from the collection |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 427 | return (Response) this.moveNotificationsFrom(meta); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | |
| 431 | /** |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 432 | * Serialize response as a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 433 | * |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 434 | * @return {@link JsonNode} representation of the response |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 435 | */ |
| 436 | @Override |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 437 | public JsonNode toJsonNode () { |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 438 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 439 | // Get notifications json response |
| 440 | ObjectNode json = (ObjectNode) super.toJsonNode(); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 441 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 442 | StringBuilder sb = new StringBuilder(); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 443 | if (this.getName() != null) { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 444 | sb.append(this.getName()); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 445 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 446 | if (this.getVersion() != null) |
| 447 | sb.append("-"); |
| 448 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 449 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 450 | // No name but version is given |
| 451 | if (this.getVersion() != null) |
| 452 | sb.append(this.getVersion()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 453 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 454 | if (sb.length() > 0) |
| 455 | json.put("version", sb.toString()); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 456 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 457 | if (this.timeExceeded) |
| 458 | json.put("timeExceeded", true); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 459 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 460 | 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 Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 469 | // totalResources is set |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 470 | if (this.totalResources != -2) |
| 471 | json.put("totalResources", this.totalResources); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 472 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 473 | // totalResults is set |
| 474 | if (this.totalResults != -2) |
| 475 | json.put("totalResults", this.totalResults); |
| 476 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 477 | // KoralQuery query object |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 478 | if (this.query != null) { |
| 479 | JsonNode queryNode = this.getQuery().toJsonNode(); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 480 | if (queryNode != null) |
| 481 | json.put("query", queryNode); |
| 482 | }; |
| 483 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 484 | // 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 Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 492 | if (this.collection != null |
| 493 | && this.collection.getFilters().toArray().length > 0) { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 494 | JsonNode collNode = this.collection.toJsonNode(); |
| 495 | if (collNode != null) |
| 496 | json.put("collection", collNode); |
| 497 | }; |
| 498 | |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 499 | return (JsonNode) json; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 500 | }; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 501 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 502 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 503 | /** |
| 504 | * Serialize response as a JSON string. |
| 505 | * <p> |
| 506 | * <blockquote><pre> |
| 507 | * { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 508 | * "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 Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 520 | * } |
| 521 | * </pre></blockquote> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 522 | * |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 523 | * @return String representation of the response |
| 524 | */ |
| 525 | public String toJsonString () { |
| Nils Diewald | 94870ae | 2014-12-09 14:35:29 +0000 | [diff] [blame] | 526 | 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 Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 534 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 535 | return "{\"errors\":[" + "[620, " + "\"Unable to generate JSON\"" + msg |
| 536 | + "]" + "]}"; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 537 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 538 | }; |