From 29614b75b116932e9fe7a59c0987710fb74d5c44 Mon Sep 17 00:00:00 2001 From: Big Andy <8012398+big-andy-coates@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:29:47 +0100 Subject: [PATCH] Get schema draft loading working again. Looks like json org added redirects to https, which isn't supported in old code. --- build.gradle.kts | 2 +- .../kafka/test/perf/testsuite/SchemaSpec.java | 51 ++++++++++++++++--- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 40cb8f1..bf2f35a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -131,7 +131,7 @@ tasks.register("runBenchmarks") { val benchmarkSmokeTest = tasks.register("runBenchmarkSmokeTest") { classpath = sourceSets.main.get().runtimeClasspath mainClass.set("org.creekservice.kafka.test.perf.BenchmarkRunner") - args(listOf("-wi", "0", "-i", "1", "-t", "1", "-r", "1s")) + args(listOf("-wi", "0", "-i", "1", "-t", "1", "-r", "1s", "-f", "0")) dependsOn(pullTask) } diff --git a/src/main/java/org/creekservice/kafka/test/perf/testsuite/SchemaSpec.java b/src/main/java/org/creekservice/kafka/test/perf/testsuite/SchemaSpec.java index dcaedff..54e7bbc 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/testsuite/SchemaSpec.java +++ b/src/main/java/org/creekservice/kafka/test/perf/testsuite/SchemaSpec.java @@ -20,14 +20,17 @@ import static java.util.stream.Collectors.toMap; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.UncheckedIOException; +import java.net.HttpURLConnection; import java.net.URI; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Map; import java.util.Optional; -import java.util.Scanner; import java.util.Set; import java.util.function.Function; @@ -61,13 +64,13 @@ public enum SchemaSpec { private final String dirName; private final URI uri; private final String content; - private final Map additonal; + private final Map additional; SchemaSpec(final String dirName, final String uri, final Set additional) { this.dirName = requireNonNull(dirName, "dirName"); this.uri = URI.create(uri); this.content = loadContent(this.uri); - this.additonal = + this.additional = additional.stream() .map(URI::create) .collect(toMap(Function.identity(), SchemaSpec::loadContent)); @@ -97,7 +100,7 @@ private Optional getContentFromUri(final URI uri) { if (normalize(this.uri).equals(normalized)) { return Optional.of(content); } - final String content = additonal.get(normalized); + final String content = additional.get(normalized); return content == null ? Optional.empty() : Optional.of(content); } @@ -109,11 +112,43 @@ private static URI normalize(final URI uri) { return uri; } - @SuppressFBWarnings("URLCONNECTION_SSRF_FD") + @SuppressFBWarnings(value = "URLCONNECTION_SSRF_FD", justification = "only called with hardcoded urls") private static String loadContent(final URI uri) { - try (Scanner scanner = new Scanner(uri.toURL().openStream(), StandardCharsets.UTF_8)) { - scanner.useDelimiter("\\A"); - return scanner.hasNext() ? scanner.next() : ""; + try { + // Always load from https, as non-secure http redirect to https: + final URL url = + uri.getScheme().equals("http") + ? new URL("https" + uri.toString().substring(4)) + : uri.toURL(); + + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setInstanceFollowRedirects(true); + final int responseCode = connection.getResponseCode(); + + if (responseCode != HttpURLConnection.HTTP_OK) { + throw new UncheckedIOException( + new IOException( + "Failed to load content from " + uri + ", code: " + responseCode)); + } + + try (BufferedReader reader = + new BufferedReader( + new InputStreamReader( + connection.getInputStream(), StandardCharsets.UTF_8))) { + final StringBuilder builder = new StringBuilder(); + + String line; + while ((line = reader.readLine()) != null) { + builder.append(line).append(System.lineSeparator()); + } + + final String content = builder.toString(); + if (content.isBlank()) { + throw new UncheckedIOException( + new IOException("Blank content loaded from " + uri)); + } + return content; + } } catch (IOException e) { throw new UncheckedIOException(e); }