From 30ed2fe83e54d70f1fb361747194d1de8c99b84e Mon Sep 17 00:00:00 2001 From: Jeff Nickoloff Date: Wed, 6 Mar 2024 10:34:51 -0700 Subject: [PATCH] Java 8 support (#13) * [Java8] Support Java 8 Changes: 1. Move from modern HttpClient (Java9+) to HttpURLConnection 2. Remove use of Map.of 3. Update Gradle to target runtime level 1.8 4. Bumped version to 1.1 Signed-off-by: Jeff Nickoloff Co-authored-by: Sam --- build.gradle | 6 ++- .../failureflags/GremlinFailureFlags.java | 48 +++++++++++-------- .../failureflags/FailureFlagsTest.java | 2 +- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 8852334..a226878 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'com.gremlin' -version '1.0' +version '1.1' repositories { mavenCentral() @@ -12,6 +12,8 @@ repositories { } java { + sourceCompatibility = '1.8' + targetCompatibility = '1.8' withSourcesJar() withJavadocJar() } @@ -45,7 +47,7 @@ publishing { maven(MavenPublication) { groupId = "com.gremlin" artifactId = "failure-flags-java" - version = "1.0" + version = "1.1" from components.java pom { diff --git a/src/main/java/com/gremlin/failureflags/GremlinFailureFlags.java b/src/main/java/com/gremlin/failureflags/GremlinFailureFlags.java index de3e5a1..764cd60 100644 --- a/src/main/java/com/gremlin/failureflags/GremlinFailureFlags.java +++ b/src/main/java/com/gremlin/failureflags/GremlinFailureFlags.java @@ -4,15 +4,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.gremlin.failureflags.behaviors.DelayedException; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpRequest.BodyPublishers; -import java.net.http.HttpResponse; -import java.net.http.HttpResponse.BodyHandlers; -import java.time.Duration; -import java.time.temporal.ChronoUnit; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -157,26 +155,38 @@ public Experiment[] fetch(FailureFlag flag) { } flag.setLabels(augmentedLabels); - HttpClient client = HttpClient.newBuilder().build(); try { - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("http://localhost:5032/experiment")) - .header("Content-Type", "application/json") - .POST(BodyPublishers.ofString(MAPPER.writeValueAsString(flag))) - .build(); - - HttpResponse response = client.send(request, BodyHandlers.ofString()); - int statusCode = response.statusCode(); + HttpURLConnection con = (HttpURLConnection) URI.create("http://localhost:5032/experiment").toURL().openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json"); + con.setDoOutput(true); + String jsonInputString = MAPPER.writeValueAsString(flag); + try(OutputStream os = con.getOutputStream()) { + byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + int statusCode = con.getResponseCode(); if (statusCode == 204) { return null; } else if (statusCode >= 200 && statusCode < 300) { Experiment[] experiments = null; + String output; + StringBuilder responseBody = new StringBuilder(); + try (BufferedReader in = new BufferedReader( + new InputStreamReader( + con.getInputStream()))) { + + while ((output = in.readLine()) != null) { + responseBody.append(output); + } + } + try { - experiments = MAPPER.readValue(response.body(), Experiment[].class); + experiments = MAPPER.readValue(responseBody.toString(), Experiment[].class); } catch (JsonProcessingException e) { try { - experiments = new Experiment[]{MAPPER.readValue(response.body(), Experiment.class)}; + experiments = new Experiment[]{MAPPER.readValue(responseBody.toString(), Experiment.class)}; } catch (JsonProcessingException innerE) { // it actually broke } @@ -188,7 +198,7 @@ public Experiment[] fetch(FailureFlag flag) { LOGGER.error("Unable to serialize or deserialize", e); } catch(IOException e) { LOGGER.error(IOEXCEPTION_MESSAGE, e); - } catch(InterruptedException e) { + } catch(RuntimeException e) { LOGGER.error("Something went wrong when sending request", e); } return null; diff --git a/src/test/java/com/gremlin/failureflags/FailureFlagsTest.java b/src/test/java/com/gremlin/failureflags/FailureFlagsTest.java index 3e87cf9..1ef1dfe 100644 --- a/src/test/java/com/gremlin/failureflags/FailureFlagsTest.java +++ b/src/test/java/com/gremlin/failureflags/FailureFlagsTest.java @@ -64,7 +64,7 @@ public void invoke_doesNothing_WhenNoExperimentReturnedWhenBehaviorPassed() { (experiments) -> { fail("default behavior must not be called"); }); failureFlags.enabled = true; failureFlags.invoke( - new FailureFlag("test-1", Map.of("method", "POST"), true), + new FailureFlag("test-1", new HashMap(){{put("method", "POST");}}, true), (experiments) -> { fail("custom behavior must not be called"); }); }