diff options
author | gjoranv <gv@verizonmedia.com> | 2019-08-26 11:19:15 +0200 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2019-08-26 12:00:53 +0200 |
commit | abec40773cd964b7244278b96b72748d76385760 (patch) | |
tree | 0f0498cc98a865dd9f16a3d73814c68bd4c95565 /metrics-proxy | |
parent | 6c0ed461c65bbaa90a208049031e9eef33fe6c6a (diff) |
Extract rest api utils to new class
Diffstat (limited to 'metrics-proxy')
-rw-r--r-- | metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/MetricsHandler.java | 38 | ||||
-rw-r--r-- | metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/RestApiUtil.java | 55 |
2 files changed, 58 insertions, 35 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/MetricsHandler.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/MetricsHandler.java index 8fcab6dfcab..ea9bb38245f 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/MetricsHandler.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/MetricsHandler.java @@ -13,20 +13,16 @@ import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; import com.yahoo.restapi.Path; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import java.net.URI; +import java.util.List; import java.util.concurrent.Executor; -import java.util.logging.Level; +import static ai.vespa.metricsproxy.http.RestApiUtil.resourceListResponse; import static com.yahoo.jdisc.Response.Status.INTERNAL_SERVER_ERROR; import static com.yahoo.jdisc.Response.Status.METHOD_NOT_ALLOWED; import static com.yahoo.jdisc.Response.Status.NOT_FOUND; import static com.yahoo.jdisc.Response.Status.OK; import static com.yahoo.jdisc.http.HttpRequest.Method.GET; -import static java.util.logging.Level.WARNING; /** * Http handler for the metrics/v1 rest api. @@ -55,21 +51,12 @@ public class MetricsHandler extends ThreadedHttpRequestHandler { Path path = new Path(request.getUri()); - if (path.matches(V1_PATH)) return v1Response(request.getUri()); + if (path.matches(V1_PATH)) return resourceListResponse(request.getUri(), List.of(VALUES_PATH)); if (path.matches(VALUES_PATH)) return valuesResponse(request); return new ErrorResponse(NOT_FOUND, "No content at given path"); } - private JsonResponse v1Response(URI requestUri) { - try { - return new JsonResponse(OK, v1Content(requestUri)); - } catch (JSONException e) { - log.log(WARNING, "Bad JSON construction in " + V1_PATH + " response", e); - return new ErrorResponse(INTERNAL_SERVER_ERROR, "An error occurred, please try path '" + VALUES_PATH + "'"); - } - } - private JsonResponse valuesResponse(HttpRequest request) { try { return new JsonResponse(OK, valuesFetcher.fetch(request.getProperty("consumer"))); @@ -78,23 +65,4 @@ public class MetricsHandler extends ThreadedHttpRequestHandler { } } - // TODO: Use jackson with a "Resources" class instead of JSONObject - private String v1Content(URI requestUri) throws JSONException { - int port = requestUri.getPort(); - String host = requestUri.getHost(); - StringBuilder base = new StringBuilder("http://"); - base.append(host); - if (port >= 0) { - base.append(":").append(port); - } - String uriBase = base.toString(); - JSONArray linkList = new JSONArray(); - for (String api : new String[] {VALUES_PATH}) { - JSONObject resource = new JSONObject(); - resource.put("url", uriBase + api); - linkList.put(resource); - } - return new JSONObject().put("resources", linkList).toString(4); - } - } diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/RestApiUtil.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/RestApiUtil.java new file mode 100644 index 00000000000..27411b5dbd9 --- /dev/null +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/RestApiUtil.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + */ + +package ai.vespa.metricsproxy.http; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.net.URI; +import java.util.List; +import java.util.logging.Logger; + +import static com.yahoo.jdisc.Response.Status.INTERNAL_SERVER_ERROR; +import static com.yahoo.jdisc.Response.Status.OK; +import static java.util.logging.Level.WARNING; + +/** + * @author gjoranv + */ +public class RestApiUtil { + private static Logger log = Logger.getLogger(RestApiUtil.class.getName()); + + + static JsonResponse resourceListResponse(URI requestUri, List<String> resources) { + try { + return new JsonResponse(OK, RestApiUtil.resourceList(requestUri, resources)); + } catch (JSONException e) { + log.log(WARNING, "Bad JSON construction in generated resource list for " + requestUri.getPath(), e); + return new ErrorResponse(INTERNAL_SERVER_ERROR, + "An error occurred when generating the list of api resources."); + } + } + + // TODO: Use jackson with a "Resources" class instead of JSONObject + private static String resourceList(URI requestUri, List<String> resources) throws JSONException { + int port = requestUri.getPort(); + String host = requestUri.getHost(); + StringBuilder base = new StringBuilder("http://"); + base.append(host); + if (port >= 0) { + base.append(":").append(port); + } + String uriBase = base.toString(); + JSONArray linkList = new JSONArray(); + for (String api : resources) { + JSONObject resource = new JSONObject(); + resource.put("url", uriBase + api); + linkList.put(resource); + } + return new JSONObject().put("resources", linkList).toString(4); + } + +} |