summaryrefslogtreecommitdiffstats
path: root/container-disc
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2021-02-02 09:23:35 +0100
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2021-02-02 09:23:52 +0100
commit5a9bd89646bb4f3558896793304f7419b96bf1c5 (patch)
tree67617ecff3fb2216a554a2483feb5604815a6568 /container-disc
parent82decc80ef372dfdcde493c5d84b4a11ff0655bf (diff)
Remove usage of org.json
Diffstat (limited to 'container-disc')
-rw-r--r--container-disc/src/main/java/com/yahoo/container/usability/BindingsOverviewHandler.java81
1 files changed, 35 insertions, 46 deletions
diff --git a/container-disc/src/main/java/com/yahoo/container/usability/BindingsOverviewHandler.java b/container-disc/src/main/java/com/yahoo/container/usability/BindingsOverviewHandler.java
index 709441999d0..df7cacdc768 100644
--- a/container-disc/src/main/java/com/yahoo/container/usability/BindingsOverviewHandler.java
+++ b/container-disc/src/main/java/com/yahoo/container/usability/BindingsOverviewHandler.java
@@ -1,9 +1,13 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.usability;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.inject.Inject;
import com.yahoo.component.ComponentId;
-import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.container.Container;
import com.yahoo.container.jdisc.JdiscBindingsConfig;
import com.yahoo.jdisc.handler.AbstractRequestHandler;
@@ -15,16 +19,11 @@ import com.yahoo.jdisc.handler.ResponseDispatch;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.jdisc.http.HttpRequest.Method;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import java.nio.ByteBuffer;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -33,6 +32,8 @@ import java.util.Map;
*/
public class BindingsOverviewHandler extends AbstractRequestHandler {
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+
private final JdiscBindingsConfig bindingsConfig;
@Inject
@@ -42,7 +43,7 @@ public class BindingsOverviewHandler extends AbstractRequestHandler {
@Override
public ContentChannel handleRequest(com.yahoo.jdisc.Request request, ResponseHandler handler) {
- JSONObject json;
+ JsonNode json;
int statusToReturn;
if (request instanceof HttpRequest && ((HttpRequest) request).getMethod() != Method.GET) {
@@ -63,7 +64,9 @@ public class BindingsOverviewHandler extends AbstractRequestHandler {
}.connect(handler));
try {
- writer.write(json.toString());
+ writer.write(jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(json));
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
} finally {
writer.close();
}
@@ -71,63 +74,58 @@ public class BindingsOverviewHandler extends AbstractRequestHandler {
return new IgnoredContent();
}
- private JSONObject errorMessageInJson() {
- JSONObject error = new JSONObject();
- try {
- error.put("error", "This API, "
- + this.getClass().getSimpleName()
- + ", only supports HTTP GET."
- + " You are probably looking for another API/path.");
- } catch (org.json.JSONException e) {
- // just ignore it
- }
+ private static JsonNode errorMessageInJson() {
+ ObjectNode error = jsonMapper.createObjectNode();
+ error.put("error", "This API, "
+ + BindingsOverviewHandler.class.getSimpleName()
+ + ", only supports HTTP GET."
+ + " You are probably looking for another API/path.");
return error;
}
- static JSONArray renderRequestHandlers(JdiscBindingsConfig bindingsConfig,
+ static ArrayNode renderRequestHandlers(JdiscBindingsConfig bindingsConfig,
Map<ComponentId, ? extends RequestHandler> handlersById) {
- JSONArray ret = new JSONArray();
+ ArrayNode ret = jsonMapper.createArrayNode();
for (Map.Entry<ComponentId, ? extends RequestHandler> handlerEntry : handlersById.entrySet()) {
String id = handlerEntry.getKey().stringValue();
RequestHandler handler = handlerEntry.getValue();
- JSONObject handlerJson = renderComponent(handler, handlerEntry.getKey());
+ ObjectNode handlerJson = renderComponent(handler, handlerEntry.getKey());
addBindings(bindingsConfig, id, handlerJson);
- ret.put(handlerJson);
+ ret.add(handlerJson);
}
return ret;
}
- private static void addBindings(JdiscBindingsConfig bindingsConfig, String id, JSONObject handlerJson) {
+ private static void addBindings(JdiscBindingsConfig bindingsConfig, String id, ObjectNode handlerJson) {
List<String> serverBindings = new ArrayList<>();
JdiscBindingsConfig.Handlers handlerConfig = bindingsConfig.handlers(id);
if (handlerConfig != null) {
serverBindings = handlerConfig.serverBindings();
}
- putJson(handlerJson, "serverBindings", renderBindings(serverBindings));
+ handlerJson.set("serverBindings", renderBindings(serverBindings));
}
- private static JSONArray renderBindings(List<String> bindings) {
- JSONArray array = new JSONArray();
+ private static JsonNode renderBindings(List<String> bindings) {
+ ArrayNode array = jsonMapper.createArrayNode();
for (String binding : bindings)
- array.put(binding);
+ array.add(binding);
return array;
}
- private static JSONObject renderComponent(Object component, ComponentId id) {
- JSONObject jc = new JSONObject();
- putJson(jc, "id", id.stringValue());
+ private static ObjectNode renderComponent(Object component, ComponentId id) {
+ ObjectNode jc = jsonMapper.createObjectNode();
+ jc.put("id", id.stringValue());
addBundleInfo(jc, component);
return jc;
}
- private static void addBundleInfo(JSONObject jsonObject, Object component) {
+ private static void addBundleInfo(ObjectNode jsonObject, Object component) {
BundleInfo bundleInfo = bundleInfo(component);
- putJson(jsonObject, "class", bundleInfo.className);
- putJson(jsonObject, "bundle", bundleInfo.bundleName);
-
+ jsonObject.put("class", bundleInfo.className);
+ jsonObject.put("bundle", bundleInfo.bundleName);
}
private static BundleInfo bundleInfo(Object component) {
@@ -143,15 +141,6 @@ public class BindingsOverviewHandler extends AbstractRequestHandler {
}
}
- private static void putJson(JSONObject json, String key, Object value) {
- try {
- json.put(key, value);
- } catch (JSONException e) {
- // The original JSONException lacks key-value info.
- throw new RuntimeException("Trying to add invalid JSON object with key '" + key + "' and value '" + value + "' - " + e.getMessage(), e);
- }
- }
-
static final class BundleInfo {
public final String className;
@@ -172,10 +161,10 @@ public class BindingsOverviewHandler extends AbstractRequestHandler {
this.bindingsConfig = bindingsConfig;
}
- public JSONObject render() {
- JSONObject root = new JSONObject();
+ public JsonNode render() {
+ ObjectNode root = jsonMapper.createObjectNode();
- putJson(root, "handlers",
+ root.set("handlers",
renderRequestHandlers(bindingsConfig, Container.get().getRequestHandlerRegistry().allComponentsById()));
return root;