Skip to content

Commit

Permalink
Remove Unirest and replace with Java's native HttpClient (#4068)
Browse files Browse the repository at this point in the history
Co-authored-by: J3fftw <[email protected]>
  • Loading branch information
WalshyDev and J3fftw1 authored Feb 25, 2024
1 parent 070d72f commit b841828
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 112 deletions.
18 changes: 0 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,6 @@
<pattern>io.papermc.lib</pattern>
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.paperlib</shadedPattern>
</relocation>
<relocation>
<pattern>kong.unirest</pattern>
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.unirest</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.lang</pattern>
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.commons.lang</shadedPattern>
Expand Down Expand Up @@ -357,20 +353,6 @@
<version>1.0.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.14.5</version>
<scope>compile</scope>

<exclusions>
<exclusion>
<!-- No need to shade Gson, Spigot does that already -->
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Testing dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodySubscriber;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.atomic.AtomicInteger;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Flow.Subscription;
import java.util.logging.Level;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.plugin.Plugin;

import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;

import io.github.bakedlibs.dough.common.CommonPatterns;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import kong.unirest.GetRequest;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;

/**
* This Class represents a Metrics Service that sends data to https://bstats.org/
* This data is used to analyse the usage of this {@link Plugin}.
Expand Down Expand Up @@ -66,22 +75,12 @@ public class MetricsService {
private final Slimefun plugin;
private final File parentFolder;
private final File metricsModuleFile;
private final HttpClient client = HttpClient.newHttpClient();

private URLClassLoader moduleClassLoader;
private String metricVersion = null;
private boolean hasDownloadedUpdate = false;

static {
// @formatter:off (We want this to stay this nicely aligned :D )
Unirest.config()
.concurrency(2, 1)
.setDefaultHeader("User-Agent", "MetricsModule Auto-Updater")
.setDefaultHeader("Accept", "application/vnd.github.v3+json")
.enableCookieManagement(false)
.cookieSpec("ignoreCookies");
// @formatter:on
}

/**
* This constructs a new instance of our {@link MetricsService}.
*
Expand Down Expand Up @@ -199,20 +198,16 @@ public boolean checkForUpdate(@Nullable String currentVersion) {
*/
private int getLatestVersion() {
try {
HttpResponse<JsonNode> response = Unirest.get(RELEASES_URL).asJson();
HttpResponse<String> response = client.send(buildBaseRequest(URI.create(RELEASES_URL)), HttpResponse.BodyHandlers.ofString());

if (!response.isSuccess()) {
if (response.statusCode() < 200 || response.statusCode() >= 300) {
return -1;
}

JsonNode node = response.getBody();
JsonElement element = JsonParser.parseString(response.body());

if (node == null) {
return -1;
}

return node.getObject().getInt("tag_name");
} catch (UnirestException e) {
return element.getAsJsonObject().get("tag_name").getAsInt();
} catch (IOException | InterruptedException | JsonParseException e) {
plugin.getLogger().log(Level.WARNING, "Failed to fetch latest builds for Metrics: {0}", e.getMessage());
return -1;
}
Expand All @@ -235,19 +230,13 @@ private boolean download(int version) {
Files.delete(file.toPath());
}

AtomicInteger lastPercentPosted = new AtomicInteger();
GetRequest request = Unirest.get(DOWNLOAD_URL + "/" + version + "/" + JAR_NAME + ".jar");
HttpResponse<Path> response = client.send(
buildBaseRequest(URI.create(DOWNLOAD_URL + "/" + version + "/" + JAR_NAME + ".jar")),
downloadMonitor(HttpResponse.BodyHandlers.ofFile(file.toPath()))
);

HttpResponse<File> response = request.downloadMonitor((b, fileName, bytesWritten, totalBytes) -> {
int percent = (int) (20 * (Math.round((((double) bytesWritten / totalBytes) * 100) / 20)));

if (percent != 0 && percent != lastPercentPosted.get()) {
plugin.getLogger().info("# Downloading... " + percent + "% " + "(" + bytesWritten + "/" + totalBytes + " bytes)");
lastPercentPosted.set(percent);
}
}).asFile(file.getPath());

if (response.isSuccess()) {
if (response.statusCode() >= 200 && response.statusCode() < 300) {
plugin.getLogger().log(Level.INFO, "Successfully downloaded {0} build: #{1}", new Object[] { JAR_NAME, version });

// Replace the metric file with the new one
Expand All @@ -258,7 +247,7 @@ private boolean download(int version) {
hasDownloadedUpdate = true;
return true;
}
} catch (UnirestException e) {
} catch (InterruptedException | JsonParseException e) {
plugin.getLogger().log(Level.WARNING, "Failed to fetch the latest jar file from the builds page. Perhaps GitHub is down? Response: {0}", e.getMessage());
} catch (IOException e) {
plugin.getLogger().log(Level.WARNING, "Failed to replace the old metric file with the new one. Please do this manually! Error: {0}", e.getMessage());
Expand Down Expand Up @@ -287,4 +276,58 @@ public String getVersion() {
public boolean hasAutoUpdates() {
return Slimefun.instance().getConfig().getBoolean("metrics.auto-update");
}

private HttpRequest buildBaseRequest(@Nonnull URI uri) {
return HttpRequest.newBuilder()
.uri(uri)
.timeout(Duration.ofSeconds(5))
.header("User-Agent", "MetricsModule Auto-Updater")
.header("Accept", "application/vnd.github.v3+json")
.build();
}

private <T> BodyHandler<T> downloadMonitor(BodyHandler<T> h) {
return info -> new BodySubscriber<T>() {

private BodySubscriber<T> delegateSubscriber = h.apply(info);
private int lastPercentPosted = 0;
private long bytesWritten = 0;

@Override
public void onSubscribe(Subscription subscription) {
delegateSubscriber.onSubscribe(subscription);
}

@Override
public void onNext(List<ByteBuffer> item) {
bytesWritten += item.stream().mapToLong(ByteBuffer::capacity).sum();
long totalBytes = info.headers().firstValue("Content-Length").map(Long::parseLong).orElse(-1L);

int percent = (int) (20 * (Math.round((((double) bytesWritten / totalBytes) * 100) / 20)));

if (percent != 0 && percent != lastPercentPosted) {
plugin.getLogger().info("# Downloading... " + percent + "% " + "(" + bytesWritten + "/" + totalBytes + " bytes)");
lastPercentPosted = percent;
}

delegateSubscriber.onNext(item);
}

@Override
public void onError(Throwable throwable) {
delegateSubscriber.onError(throwable);

}

@Override
public void onComplete() {
delegateSubscriber.onComplete();
}

@Override
public CompletionStage<T> getBody() {
return delegateSubscriber.getBody();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import kong.unirest.JsonNode;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

class ContributionsConnector extends GitHubConnector {

Expand Down Expand Up @@ -90,11 +90,11 @@ public boolean hasFinished() {
}

@Override
public void onSuccess(@Nonnull JsonNode response) {
public void onSuccess(@Nonnull JsonElement response) {
finished = true;

if (response.isArray()) {
computeContributors(response.getArray());
if (response.isJsonArray()) {
computeContributors(response.getAsJsonArray());
} else {
Slimefun.logger().log(Level.WARNING, "Received an unusual answer from GitHub, possibly a timeout? ({0})", response);
}
Expand Down Expand Up @@ -123,13 +123,13 @@ public Map<String, Object> getParameters() {
return parameters;
}

private void computeContributors(@Nonnull JSONArray array) {
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
private void computeContributors(@Nonnull JsonArray array) {
for (JsonElement element : array) {
JsonObject object = element.getAsJsonObject();

String name = object.getString("login");
int commits = object.getInt("contributions");
String profile = object.getString("html_url");
String name = object.get("login").getAsString();
int commits = object.get("contributions").getAsInt();
String profile = object.get("html_url").getAsString();

if (!ignoredAccounts.contains(name)) {
String username = aliases.getOrDefault(name, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import kong.unirest.JsonNode;
import kong.unirest.json.JSONObject;
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;

class GitHubActivityConnector extends GitHubConnector {

Expand All @@ -23,11 +23,11 @@ class GitHubActivityConnector extends GitHubConnector {
}

@Override
public void onSuccess(@Nonnull JsonNode response) {
JSONObject object = response.getObject();
int forks = object.getInt("forks");
int stars = object.getInt("stargazers_count");
LocalDateTime lastPush = NumberUtils.parseGitHubDate(object.getString("pushed_at"));
public void onSuccess(@Nonnull JsonElement response) {
JsonObject object = response.getAsJsonObject();
int forks = object.get("forks").getAsInt();
int stars = object.get("stargazers_count").getAsInt();
LocalDateTime lastPush = NumberUtils.parseGitHubDate(object.get("pushed_at").getAsString());

callback.accept(forks, stars, lastPush);
}
Expand Down
Loading

0 comments on commit b841828

Please sign in to comment.