Handles an HTTP based suggest query Note: This method must remain backwards compatible with the 1.x API call @throws IOException if there is an error parsing the query or formatting the output @throws BadRequestException if the user supplied bad data
(final TSDB tsdb, final HttpQuery query)
| 38 | * @throws BadRequestException if the user supplied bad data |
| 39 | */ |
| 40 | public void execute(final TSDB tsdb, final HttpQuery query) |
| 41 | throws IOException { |
| 42 | |
| 43 | // only accept GET/POST |
| 44 | if (query.method() != HttpMethod.GET && query.method() != HttpMethod.POST) { |
| 45 | throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED, |
| 46 | "Method not allowed", "The HTTP method [" + query.method().getName() + |
| 47 | "] is not permitted for this endpoint"); |
| 48 | } |
| 49 | |
| 50 | final String type; |
| 51 | final String q; |
| 52 | final String max; |
| 53 | if (query.apiVersion() > 0 && query.method() == HttpMethod.POST) { |
| 54 | final HashMap<String, String> map = query.serializer().parseSuggestV1(); |
| 55 | type = map.get("type"); |
| 56 | if (type == null || type.isEmpty()) { |
| 57 | throw new BadRequestException("Missing 'type' parameter"); |
| 58 | } |
| 59 | q = map.get("q") == null ? "" : map.get("q"); |
| 60 | max = map.get("max"); |
| 61 | } else { |
| 62 | type = query.getRequiredQueryStringParam("type"); |
| 63 | q = query.hasQueryStringParam("q") ? query.getQueryStringParam("q") : ""; |
| 64 | max = query.getQueryStringParam("max"); |
| 65 | } |
| 66 | |
| 67 | final int max_results; |
| 68 | if (max != null && !max.isEmpty()) { |
| 69 | try { |
| 70 | max_results = Integer.parseInt(max); |
| 71 | } catch (NumberFormatException nfe) { |
| 72 | throw new BadRequestException("Unable to parse 'max' as a number"); |
| 73 | } |
| 74 | } else { |
| 75 | max_results = 0; |
| 76 | } |
| 77 | |
| 78 | List<String> suggestions; |
| 79 | if ("metrics".equals(type)) { |
| 80 | suggestions = max_results > 0 ? tsdb.suggestMetrics(q, max_results) : |
| 81 | tsdb.suggestMetrics(q); |
| 82 | } else if ("tagk".equals(type)) { |
| 83 | suggestions = max_results > 0 ? tsdb.suggestTagNames(q, max_results) : |
| 84 | tsdb.suggestTagNames(q); |
| 85 | } else if ("tagv".equals(type)) { |
| 86 | suggestions = max_results > 0 ? tsdb.suggestTagValues(q, max_results) : |
| 87 | tsdb.suggestTagValues(q); |
| 88 | } else { |
| 89 | throw new BadRequestException("Invalid 'type' parameter:" + type); |
| 90 | } |
| 91 | |
| 92 | if (query.apiVersion() > 0) { |
| 93 | query.sendReply(query.serializer().formatSuggestV1(suggestions)); |
| 94 | } else { // deprecated API |
| 95 | query.sendReply(JSON.serializeToBytes(suggestions)); |
| 96 | } |
| 97 | } |
nothing calls this directly
no test coverage detected