diff --git a/src/main/java/netneutrality/http/WebModule.java b/src/main/java/netneutrality/http/WebModule.java index ce17a78..ab9d508 100644 --- a/src/main/java/netneutrality/http/WebModule.java +++ b/src/main/java/netneutrality/http/WebModule.java @@ -170,11 +170,17 @@ public class WebModule { * * @return the response as a JSONArray */ - public JSONArray getAsArray() throws JSONException, IOException { + public JSONArray getAsArray() { if (response == null) throw new RuntimeException("Request has not been sent"); - - return new JSONArray(response.body().string()); + + try { + ResponseBody body = response.body(); + return new JSONArray(body.string()); + } catch (IOException | NullPointerException e) { + LogHandler.log(e); + return new JSONArray(); + } } /** diff --git a/src/main/java/netneutrality/utils/Settings.java b/src/main/java/netneutrality/utils/Settings.java index 21738d4..4c73597 100644 --- a/src/main/java/netneutrality/utils/Settings.java +++ b/src/main/java/netneutrality/utils/Settings.java @@ -6,11 +6,15 @@ package netneutrality.utils; * https://discord.gg/qUa6zNFZsd */ +import netneutrality.http.WebModule; import org.apache.logging.log4j.LogManager; +import org.json.JSONArray; +import org.json.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Map; import java.util.Properties; @@ -69,4 +73,47 @@ public class Settings { return property; } + public String getMavenVersion() { + String version = null; + + // try to load from maven properties first + try { + Properties p = new Properties(); + InputStream is = getClass().getResourceAsStream("/META-INF/maven/me.bobert/netneutrality/pom.properties"); + if (is != null) { + p.load(is); + version = p.getProperty("version", ""); + } + } catch (Exception ignored) {} + + // fallback to using Java API + if (version == null) { + Package aPackage = getClass().getPackage(); + if (aPackage != null) { + version = aPackage.getImplementationVersion(); + if (version == null) { + version = aPackage.getSpecificationVersion(); + } + } + } + + if (version == null) { + version = "0.0.0"; + } + + return version; + } + + public String getGitVersion() { + String gitlabHost = "https://git.bobert.me"; + String gitlabProjectId = "22"; + String latestReleaseUrl = String.format("%s/api/v4/projects/%s/releases/", gitlabHost, gitlabProjectId); + + JSONArray json = new WebModule(latestReleaseUrl).request().getAsArray(); + if (json.isNull(0)) { + return "0.0.0"; + } + + return json.getJSONObject(0).getString("tag_name"); + } } diff --git a/src/test/java/netneutrality/utils/SettingsTest.java b/src/test/java/netneutrality/utils/SettingsTest.java new file mode 100644 index 0000000..732b3b7 --- /dev/null +++ b/src/test/java/netneutrality/utils/SettingsTest.java @@ -0,0 +1,30 @@ +package netneutrality.utils; + +/* + * Copyright (c) anthorob + * + * https://discord.gg/qUa6zNFZsd + */ + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SettingsTest { + + @Test + public void getMavenVersion() { + String version = Settings.getInstance().getMavenVersion(); + System.out.println(version); + + assertNotNull(version); + } + + @Test + public void getGitVersion() { + String version = Settings.getInstance().getGitVersion(); + System.out.println(version); + + assertNotNull(version); + } +} \ No newline at end of file