diff options
author | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-12-11 09:21:34 +0100 |
---|---|---|
committer | Fiete Ostkamp <fiete.ostkamp@telekom.de> | 2024-12-11 08:27:21 +0000 |
commit | 5c2e24008d5b093f86aa5dce8db1336d9211d1d2 (patch) | |
tree | 9e60f3f5ec3b76a442206b9b9752e6ec5a616a3e /aai-core | |
parent | 498b357833a128211aac0bf6c4325f0b100b72b6 (diff) |
Reduce org.json usage in aai-common
- org.json [is slow](https://github.com/fabienrenaud/java-json-benchmark?tab=readme-ov-file#users-model)
- Jackson should be consistently used everywhere
- only XmlFormatTransformer is left, but that warrants a dedicated change with
(likely) further tests since it appears to be quite a critical execution path
- remove unused imports
- update org.json to a non-vulnerable version
Issue-ID: AAI-4085
Change-Id: I84610523447d70a1729348392ffd302d17e9379d
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Diffstat (limited to 'aai-core')
11 files changed, 48 insertions, 72 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java b/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java index 496c1e82..5ecc3a91 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java @@ -22,30 +22,41 @@ package org.onap.aai.introspection; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; import org.onap.aai.setup.SchemaVersion; +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; + public class JSONStrategy extends Introspector { - private JSONObject json = null; + private JsonNode json = null; private String namedType = ""; + private static final ObjectMapper mapper = new ObjectMapper(); protected JSONStrategy(Object o) { super(o); - json = (JSONObject) o; + json = mapper.valueToTree(o); // Assumes you provide a wrapper - Set<String> keySet = json.keySet(); - if (keySet.size() == 1) { - namedType = keySet.iterator().next(); - json = (JSONObject) json.get(namedType); + Iterator<Map.Entry<String, JsonNode>> fields = json.fields(); + if (fields.hasNext()) { + Map.Entry<String, JsonNode> entry = fields.next(); + if (!fields.hasNext()) { + namedType = entry.getKey(); + json = entry.getValue(); + } else { + throw new IllegalArgumentException("This object has no named type."); + } } else { throw new IllegalArgumentException("This object has no named type."); } @@ -53,7 +64,7 @@ public class JSONStrategy extends Introspector { protected JSONStrategy(Object o, String namedType) { super(o); - json = (JSONObject) o; + json = (JsonNode) o; this.namedType = namedType; } @@ -74,8 +85,7 @@ public class JSONStrategy extends Introspector { @Override public void setValue(String name, Object obj) { - json.put(name, obj); - + ((ObjectNode) json).set(name, (JsonNode) obj); } @Override @@ -85,8 +95,14 @@ public class JSONStrategy extends Introspector { @Override public Set<String> getProperties() { - Set<String> result = json.keySet(); - return result; + Iterator<String> fieldNames = json.fieldNames(); + Set<String> properties = new HashSet<>(); + + // Iterate through the iterator and add each element to the set + while (fieldNames.hasNext()) { + properties.add(fieldNames.next()); + } + return properties; } @Override @@ -150,7 +166,7 @@ public class JSONStrategy extends Introspector { public Class<?> getGenericTypeClass(String name) { Class<?> resultClass = null; Object resultObject = this.getValue(name); - if (resultObject instanceof JSONArray) { + if (resultObject instanceof ArrayNode) { resultClass = ((List<?>) resultObject).get(0).getClass(); } diff --git a/aai-core/src/main/java/org/onap/aai/kafka/MessageProducer.java b/aai-core/src/main/java/org/onap/aai/kafka/MessageProducer.java deleted file mode 100644 index 09fc68a2..00000000 --- a/aai-core/src/main/java/org/onap/aai/kafka/MessageProducer.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.aai.kafka; - -import org.json.JSONObject; - -/** - * MessageProducer interface based on untyped messages - * - * @deprecated use {@link org.onap.aai.kafka.NotificationProducer} instead - */ -@Deprecated -public interface MessageProducer { - - void sendMessageToDefaultDestination(JSONObject finalJson); - - void sendMessageToDefaultDestination(String msg); -} diff --git a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java index b31bfcb8..23201593 100644 --- a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java +++ b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java @@ -39,9 +39,7 @@ import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import org.apache.commons.configuration2.JSONConfiguration; import org.glassfish.jersey.client.ClientConfig; -import org.glassfish.jersey.client.ClientProperties; import org.onap.aai.aailog.filter.RestControllerClientRequestLoggingInterceptor; import org.onap.aai.aailog.filter.RestControllerClientResponseLoggingInterceptor; import org.onap.aai.exceptions.AAIException; diff --git a/aai-core/src/test/java/org/onap/aai/HttpTestUtil.java b/aai-core/src/test/java/org/onap/aai/HttpTestUtil.java index 62f63ea3..2759ab99 100644 --- a/aai-core/src/test/java/org/onap/aai/HttpTestUtil.java +++ b/aai-core/src/test/java/org/onap/aai/HttpTestUtil.java @@ -21,7 +21,6 @@ package org.onap.aai; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; diff --git a/aai-core/src/test/java/org/onap/aai/IntegrationTest.java b/aai-core/src/test/java/org/onap/aai/IntegrationTest.java index 9ee5fce5..9398ed37 100644 --- a/aai-core/src/test/java/org/onap/aai/IntegrationTest.java +++ b/aai-core/src/test/java/org/onap/aai/IntegrationTest.java @@ -22,7 +22,6 @@ package org.onap.aai; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.aai.config.ConfigConfiguration; -import org.onap.aai.config.GraphConfig; import org.onap.aai.config.IntrospectionConfig; import org.onap.aai.config.KafkaConfig; import org.onap.aai.config.RestBeanConfig; @@ -36,7 +35,6 @@ import org.onap.aai.prevalidation.ValidationService; import org.onap.aai.rest.notification.NotificationService; import org.onap.aai.serialization.db.EdgeSerializer; import org.onap.aai.setup.AAIConfigTranslator; -import org.onap.aai.util.GraphChecker; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; diff --git a/aai-core/src/test/java/org/onap/aai/JanusgraphCassandraConfiguration.java b/aai-core/src/test/java/org/onap/aai/JanusgraphCassandraConfiguration.java index 08c99170..f9914445 100644 --- a/aai-core/src/test/java/org/onap/aai/JanusgraphCassandraConfiguration.java +++ b/aai-core/src/test/java/org/onap/aai/JanusgraphCassandraConfiguration.java @@ -5,9 +5,6 @@ import java.io.FileNotFoundException; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.configuration2.ex.ConfigurationException; -import org.janusgraph.core.JanusGraphProperty; -import org.janusgraph.core.schema.JanusGraphConfiguration; -import org.onap.aai.dbmap.AAIGraphConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; diff --git a/aai-core/src/test/java/org/onap/aai/introspection/JSONStrategyTest.java b/aai-core/src/test/java/org/onap/aai/introspection/JSONStrategyTest.java index 4982f1d6..b1e22f2b 100644 --- a/aai-core/src/test/java/org/onap/aai/introspection/JSONStrategyTest.java +++ b/aai-core/src/test/java/org/onap/aai/introspection/JSONStrategyTest.java @@ -23,15 +23,19 @@ package org.onap.aai.introspection; import java.util.HashSet; import java.util.Set; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.onap.aai.AAISetup; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + @Ignore("Not a used/flushed out feature") +// This has been converted from org.json to Jackson, +// but not in a way that tests are working public class JSONStrategyTest extends AAISetup { private JSONStrategy jsonStrategy; private JSONStrategy jsonStrategyContainer; @@ -40,21 +44,25 @@ public class JSONStrategyTest extends AAISetup { @Before public void setup() { try { - JSONObject pserver = new JSONObject(); + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode pserver = mapper.createObjectNode(); + pserver.put("hostname", "value1"); pserver.put("numberofCpus", 4); jsonStrategy = new JSONStrategy(pserver, "pserver-type"); // The values of this object are arrays containing JSONObjects - JSONArray pservers = new JSONArray(); + ArrayNode pservers = mapper.createArrayNode(); pservers.add(pserver); - JSONObject container = new JSONObject(); - container.put("pservers", pservers); + + ObjectNode container = mapper.createObjectNode(); + container.set("pservers", pservers); jsonStrategyContainer = new JSONStrategy(container, "pservers-type"); // The values of this object are JSONObjects - JSONObject complex = new JSONObject(); - complex.put("pserver", pserver); + ObjectNode complex = mapper.createObjectNode(); + complex.set("pserver", pserver); jsonStrategyComplex = new JSONStrategy(complex, "pservers-type"); } catch (Exception e) { System.out.println("error during setup: " + e.getMessage()); @@ -96,8 +104,8 @@ public class JSONStrategyTest extends AAISetup { @Test public void getJavaClassNameTest() { - Assert.assertEquals("org.json.simple.JSONObject", jsonStrategy.getJavaClassName()); - Assert.assertEquals("org.json.simple.JSONObject", jsonStrategyContainer.getJavaClassName()); + Assert.assertEquals("com.fasterxml.jackson.databind.node.ObjectNode", jsonStrategy.getJavaClassName()); + Assert.assertEquals("com.fasterxml.jackson.databind.node.ObjectNode", jsonStrategyContainer.getJavaClassName()); } @Test diff --git a/aai-core/src/test/java/org/onap/aai/rest/ImpliedDeleteIntegrationTest.java b/aai-core/src/test/java/org/onap/aai/rest/ImpliedDeleteIntegrationTest.java index 38731288..48c3e60a 100644 --- a/aai-core/src/test/java/org/onap/aai/rest/ImpliedDeleteIntegrationTest.java +++ b/aai-core/src/test/java/org/onap/aai/rest/ImpliedDeleteIntegrationTest.java @@ -47,7 +47,6 @@ import org.onap.aai.db.props.AAIProperties; import org.onap.aai.dbmap.AAIGraph; import org.onap.aai.domain.notificationEvent.NotificationEvent; import org.onap.aai.domain.notificationEvent.NotificationEvent.EventHeader; -import org.onap.aai.introspection.ModelType; import org.onap.aai.rest.notification.UEBNotification; import org.onap.aai.serialization.engines.QueryStyle; import org.skyscreamer.jsonassert.JSONAssert; diff --git a/aai-core/src/test/java/org/onap/aai/rest/NotificationDmaapEventTest.java b/aai-core/src/test/java/org/onap/aai/rest/NotificationDmaapEventTest.java index 0eafdf1c..50a81767 100644 --- a/aai-core/src/test/java/org/onap/aai/rest/NotificationDmaapEventTest.java +++ b/aai-core/src/test/java/org/onap/aai/rest/NotificationDmaapEventTest.java @@ -59,7 +59,6 @@ import org.onap.aai.dbmap.AAIGraph; import org.onap.aai.domain.notificationEvent.NotificationEvent; import org.onap.aai.domain.notificationEvent.NotificationEvent.EventHeader; import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.ModelType; import org.onap.aai.rest.notification.UEBNotification; import org.onap.aai.serialization.engines.QueryStyle; import org.skyscreamer.jsonassert.JSONAssert; diff --git a/aai-core/src/test/java/org/onap/aai/rest/db/HttpEntryNotificationIntegrationTest.java b/aai-core/src/test/java/org/onap/aai/rest/db/HttpEntryNotificationIntegrationTest.java index 6b90782c..ba7fcae3 100644 --- a/aai-core/src/test/java/org/onap/aai/rest/db/HttpEntryNotificationIntegrationTest.java +++ b/aai-core/src/test/java/org/onap/aai/rest/db/HttpEntryNotificationIntegrationTest.java @@ -25,7 +25,6 @@ import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; import java.io.UnsupportedEncodingException; diff --git a/aai-core/src/test/java/org/onap/aai/rest/notification/NotificationServiceTest.java b/aai-core/src/test/java/org/onap/aai/rest/notification/NotificationServiceTest.java index 4fd21497..a042d5da 100644 --- a/aai-core/src/test/java/org/onap/aai/rest/notification/NotificationServiceTest.java +++ b/aai-core/src/test/java/org/onap/aai/rest/notification/NotificationServiceTest.java @@ -24,7 +24,6 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; |