Skip to content

Commit

Permalink
Java 8 support (#13)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
Co-authored-by: Sam <[email protected]>
  • Loading branch information
allingeek and gremsam authored Mar 6, 2024
1 parent a587d32 commit 30ed2fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ plugins {
}

group 'com.gremlin'
version '1.0'
version '1.1'

repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}

java {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
withSourcesJar()
withJavadocJar()
}
Expand Down Expand Up @@ -45,7 +47,7 @@ publishing {
maven(MavenPublication) {
groupId = "com.gremlin"
artifactId = "failure-flags-java"
version = "1.0"
version = "1.1"
from components.java

pom {
Expand Down
48 changes: 29 additions & 19 deletions src/main/java/com/gremlin/failureflags/GremlinFailureFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String>(){{put("method", "POST");}}, true),
(experiments) -> { fail("custom behavior must not be called"); });
}

Expand Down

0 comments on commit 30ed2fe

Please sign in to comment.