From fea3e6e3711017b4b19fd61e4c2318fe51f7dd45 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Mon, 8 Jan 2024 15:57:42 -0800 Subject: [PATCH] Eliminate Google Guava dependency (#383) * Eliminate Google Guava dependency Signed-off-by: Daniel Widdis * Fix classpath Signed-off-by: Daniel Widdis --------- Signed-off-by: Daniel Widdis Co-authored-by: Owais Kazi --- build.gradle | 3 +-- .../indices/FlowFrameworkIndicesHandler.java | 7 ++---- .../model/WorkflowValidator.java | 6 +---- .../flowframework/util/ParseUtils.java | 22 +++++++++++++++++++ .../opensearch/flowframework/TestHelpers.java | 7 ++---- .../flowframework/util/ParseUtilsTests.java | 11 ++++++++++ 6 files changed, 39 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index 79cb04a04..8a957fe1c 100644 --- a/build.gradle +++ b/build.gradle @@ -146,7 +146,6 @@ configurations { dependencies { implementation "org.opensearch:opensearch:${opensearch_version}" implementation 'org.junit.jupiter:junit-jupiter:5.10.1' - implementation "com.google.guava:guava:33.0.0-jre" api group: 'org.opensearch', name:'opensearch-ml-client', version: "${opensearch_build}" api group: 'org.opensearch.client', name: 'opensearch-rest-client', version: "${opensearch_version}" implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.14.0' @@ -160,7 +159,7 @@ dependencies { configurations.all { resolutionStrategy { - force("com.google.guava:guava:33.0.0-jre") // CVE for 31.1 + force("com.google.guava:guava:33.0.0-jre") // CVE for 31.1, keep to force transitive dependencies force("org.eclipse.platform:org.eclipse.core.runtime:3.30.0") // CVE for < 3.29.0 force("com.fasterxml.jackson.core:jackson-core:2.16.1") // Dependency Jar Hell } diff --git a/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndicesHandler.java b/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndicesHandler.java index 989c0da44..f5dd8698b 100644 --- a/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndicesHandler.java +++ b/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndicesHandler.java @@ -8,7 +8,6 @@ */ package org.opensearch.flowframework.indices; -import com.google.common.io.Resources; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.ExceptionsHelper; @@ -39,12 +38,11 @@ import org.opensearch.flowframework.model.Template; import org.opensearch.flowframework.model.WorkflowState; import org.opensearch.flowframework.util.EncryptorUtils; +import org.opensearch.flowframework.util.ParseUtils; import org.opensearch.script.Script; import org.opensearch.script.ScriptType; import java.io.IOException; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -292,8 +290,7 @@ private void shouldUpdateIndex(String indexName, Integer newVersion, ActionListe * @throws IOException IOException if mapping file can't be read correctly */ public static String getIndexMappings(String mapping) throws IOException { - URL url = FlowFrameworkIndicesHandler.class.getClassLoader().getResource(mapping); - return Resources.toString(url, StandardCharsets.UTF_8); + return ParseUtils.resourceToString("/" + mapping); } /** diff --git a/src/main/java/org/opensearch/flowframework/model/WorkflowValidator.java b/src/main/java/org/opensearch/flowframework/model/WorkflowValidator.java index 70bd833bc..d2373045b 100644 --- a/src/main/java/org/opensearch/flowframework/model/WorkflowValidator.java +++ b/src/main/java/org/opensearch/flowframework/model/WorkflowValidator.java @@ -8,13 +8,10 @@ */ package org.opensearch.flowframework.model; -import com.google.common.io.Resources; import org.opensearch.core.xcontent.XContentParser; import org.opensearch.flowframework.util.ParseUtils; import java.io.IOException; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -62,8 +59,7 @@ public static WorkflowValidator parse(XContentParser parser) throws IOException * @throws IOException on failure to read and parse the json file */ public static WorkflowValidator parse(String file) throws IOException { - URL url = WorkflowValidator.class.getClassLoader().getResource(file); - String json = Resources.toString(url, StandardCharsets.UTF_8); + String json = ParseUtils.resourceToString("/" + file); return parse(ParseUtils.jsonToParser(json)); } diff --git a/src/main/java/org/opensearch/flowframework/util/ParseUtils.java b/src/main/java/org/opensearch/flowframework/util/ParseUtils.java index b452a33a9..fc7536177 100644 --- a/src/main/java/org/opensearch/flowframework/util/ParseUtils.java +++ b/src/main/java/org/opensearch/flowframework/util/ParseUtils.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.Client; +import org.opensearch.common.io.Streams; import org.opensearch.common.xcontent.LoggingDeprecationHandler; import org.opensearch.common.xcontent.XContentHelper; import org.opensearch.common.xcontent.XContentType; @@ -26,7 +27,9 @@ import org.opensearch.flowframework.workflow.WorkflowData; import org.opensearch.ml.common.agent.LLMSpec; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.time.Instant; import java.util.HashMap; import java.util.HashSet; @@ -71,6 +74,25 @@ public static XContentParser jsonToParser(String json) throws IOException { return parser; } + /** + * Reads a file from the classpath into a String. Useful for reading JSON mapping files. + * + * @param path A string identifying the resource on the class path + * @return A string containing the contents of the file as UTF-8 + * @throws IOException if file is not found or error reading + */ + public static String resourceToString(String path) throws IOException { + try (InputStream is = ParseUtils.class.getResourceAsStream(path)) { + if (is == null) { + throw new FileNotFoundException("Resource [" + path + "] not found in classpath"); + } + final StringBuilder sb = new StringBuilder(); + // Read as UTF-8 + Streams.readAllLines(is, sb::append); + return sb.toString(); + } + } + /** * Builds an XContent object representing a map of String keys to String values. * diff --git a/src/test/java/org/opensearch/flowframework/TestHelpers.java b/src/test/java/org/opensearch/flowframework/TestHelpers.java index b3c65498c..3b3373253 100644 --- a/src/test/java/org/opensearch/flowframework/TestHelpers.java +++ b/src/test/java/org/opensearch/flowframework/TestHelpers.java @@ -8,7 +8,6 @@ */ package org.opensearch.flowframework; -import com.google.common.io.Resources; import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.io.entity.StringEntity; @@ -31,13 +30,12 @@ import org.opensearch.core.xcontent.ToXContentObject; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.flowframework.model.Template; +import org.opensearch.flowframework.util.ParseUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -51,8 +49,7 @@ public class TestHelpers { public static Template createTemplateFromFile(String fileName) throws IOException { - URL url = TestHelpers.class.getClassLoader().getResource("template/" + fileName); - String json = Resources.toString(url, StandardCharsets.UTF_8); + String json = ParseUtils.resourceToString("/template/" + fileName); return Template.parse(json); } diff --git a/src/test/java/org/opensearch/flowframework/util/ParseUtilsTests.java b/src/test/java/org/opensearch/flowframework/util/ParseUtilsTests.java index 1ed556445..3431568e6 100644 --- a/src/test/java/org/opensearch/flowframework/util/ParseUtilsTests.java +++ b/src/test/java/org/opensearch/flowframework/util/ParseUtilsTests.java @@ -12,6 +12,7 @@ import org.opensearch.core.rest.RestStatus; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; +import org.opensearch.core.xcontent.XContentParser.Token; import org.opensearch.flowframework.exception.FlowFrameworkException; import org.opensearch.flowframework.workflow.WorkflowData; import org.opensearch.test.OpenSearchTestCase; @@ -23,6 +24,16 @@ import java.util.Set; public class ParseUtilsTests extends OpenSearchTestCase { + public void testResourceToStringToJson() throws IOException { + String json = ParseUtils.resourceToString("/template/finaltemplate.json"); + assertTrue(json.startsWith("{")); + assertTrue(json.contains("name")); + try (XContentParser parser = ParseUtils.jsonToParser(json)) { + assertEquals(Token.FIELD_NAME, parser.nextToken()); + assertEquals("name", parser.currentName()); + } + } + public void testToInstant() throws IOException { long epochMilli = Instant.now().toEpochMilli(); XContentBuilder builder = XContentFactory.jsonBuilder().value(epochMilli);