Skip to content

Commit 807714c

Browse files
committed
remove web dependency
1 parent 5f364dc commit 807714c

14 files changed

Lines changed: 151 additions & 111 deletions

File tree

lib/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
</repositories>
2020

2121
<dependencies>
22-
<dependency>
23-
<groupId>me.hsgamer</groupId>
24-
<artifactId>hscore-web</artifactId>
25-
<version>${core.version}</version>
26-
</dependency>
2722
<dependency>
2823
<groupId>org.json</groupId>
2924
<artifactId>json</artifactId>

lib/src/main/java/io/github/projectunified/mcserverupdater/UpdateBuilder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public final class UpdateBuilder {
6363
};
6464
private DebugConsumer debugConsumer = s -> {
6565
};
66+
private String userAgent;
6667

6768
private UpdateBuilder(String project) {
6869
this.project = project;
@@ -227,6 +228,17 @@ public UpdateBuilder checkOnly(boolean checkOnly) {
227228
return this;
228229
}
229230

231+
/**
232+
* Set the user agent for the update process
233+
*
234+
* @param userAgent the user agent string
235+
* @return the update process
236+
*/
237+
public UpdateBuilder userAgent(String userAgent) {
238+
this.userAgent = userAgent;
239+
return this;
240+
}
241+
230242
/**
231243
* Get the checksum consumer
232244
*
@@ -276,6 +288,15 @@ public DebugConsumer debugConsumer() {
276288
return debugConsumer;
277289
}
278290

291+
/**
292+
* Get the user agent
293+
*
294+
* @return the user agent string
295+
*/
296+
public String userAgent() {
297+
return userAgent;
298+
}
299+
279300
/**
280301
* Debug a message
281302
*

lib/src/main/java/io/github/projectunified/mcserverupdater/api/BibliothekUpdater.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import io.github.projectunified.mcserverupdater.UpdateBuilder;
44
import io.github.projectunified.mcserverupdater.util.VersionQuery;
5-
import me.hsgamer.hscore.web.UserAgent;
6-
import me.hsgamer.hscore.web.WebUtils;
5+
import io.github.projectunified.mcserverupdater.util.WebUtils;
76
import org.json.JSONArray;
87
import org.json.JSONObject;
98
import org.json.JSONTokener;
@@ -12,7 +11,7 @@
1211
import java.io.InputStream;
1312
import java.net.URLConnection;
1413

15-
public abstract class BibliothekUpdater implements UrlInputStreamUpdater, FileDigestChecksum {
14+
public abstract class BibliothekUpdater implements InputStreamUpdater, FileDigestChecksum {
1615
private final UpdateBuilder updateBuilder;
1716
private final String version;
1817
private final String build;
@@ -44,7 +43,7 @@ protected BibliothekUpdater(VersionQuery versionQuery, String project) {
4443
private String getDefaultVersion() {
4544
updateBuilder.debug("Getting default version from " + projectUrl);
4645
try {
47-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(projectUrl));
46+
URLConnection connection = WebUtils.openConnection(projectUrl, updateBuilder);
4847
InputStream inputStream = connection.getInputStream();
4948
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
5049
JSONArray builds = jsonObject.getJSONArray("versions");
@@ -58,7 +57,7 @@ private String getBuild() {
5857
String formattedUrl = String.format(versionUrl, version);
5958
updateBuilder.debug("Getting latest build from " + formattedUrl);
6059
try {
61-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(formattedUrl));
60+
URLConnection connection = WebUtils.openConnection(formattedUrl, updateBuilder);
6261
InputStream inputStream = connection.getInputStream();
6362
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
6463
JSONArray builds = jsonObject.getJSONArray("builds");
@@ -71,7 +70,7 @@ private String getBuild() {
7170
private JSONObject getDownload() throws IOException {
7271
String formattedUrl = String.format(buildUrl, version, build);
7372
updateBuilder.debug("Getting download from " + formattedUrl);
74-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(formattedUrl));
73+
URLConnection connection = WebUtils.openConnection(formattedUrl, updateBuilder);
7574
InputStream inputStream = connection.getInputStream();
7675
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
7776
JSONObject downloads = jsonObject.getJSONObject("downloads");
@@ -84,13 +83,13 @@ public final String getChecksum() {
8483
JSONObject application = getDownload();
8584
return application.getString(getChecksumAlgorithm());
8685
} catch (Exception e) {
87-
e.printStackTrace();
86+
debug("Failed to get checksum", e);
8887
return null;
8988
}
9089
}
9190

9291
@Override
93-
public final String getFileUrl() {
92+
public InputStream getInputStream() {
9493
String downloadName;
9594
try {
9695
JSONObject application = getDownload();
@@ -99,7 +98,8 @@ public final String getFileUrl() {
9998
debug(e);
10099
return null;
101100
}
102-
return String.format(downloadUrl, version, build, downloadName);
101+
String url = String.format(downloadUrl, version, build, downloadName);
102+
return WebUtils.getInputStreamOrNull(url, updateBuilder);
103103
}
104104

105105
@Override

lib/src/main/java/io/github/projectunified/mcserverupdater/api/GithubBranchUpdater.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import io.github.projectunified.mcserverupdater.UpdateBuilder;
44
import io.github.projectunified.mcserverupdater.util.VersionQuery;
5-
import me.hsgamer.hscore.web.UserAgent;
6-
import me.hsgamer.hscore.web.WebUtils;
5+
import io.github.projectunified.mcserverupdater.util.WebUtils;
76
import org.json.JSONArray;
87
import org.json.JSONObject;
98
import org.json.JSONTokener;
@@ -14,7 +13,7 @@
1413
import java.net.URLConnection;
1514
import java.util.regex.Pattern;
1615

17-
public abstract class GithubBranchUpdater implements SimpleChecksum, UrlInputStreamUpdater {
16+
public abstract class GithubBranchUpdater implements SimpleChecksum, InputStreamUpdater {
1817
protected final UpdateBuilder updateBuilder;
1918
protected final String version;
2019
protected final String build;
@@ -42,7 +41,7 @@ private String getBuild() {
4241
String url = String.format(refLatestCommitUrl, getBranch());
4342
debug("Getting latest build from " + url);
4443
try {
45-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
44+
URLConnection connection = WebUtils.openConnection(url, updateBuilder);
4645
InputStream inputStream = connection.getInputStream();
4746
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
4847
String sha = jsonObject.getString("sha");
@@ -57,7 +56,7 @@ private String getFile() {
5756
String url = String.format(filesUrl, build);
5857
debug("Getting files from " + url);
5958
try {
60-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
59+
URLConnection connection = WebUtils.openConnection(url, updateBuilder);
6160
InputStream inputStream = connection.getInputStream();
6261
JSONObject object = new JSONObject(new JSONTokener(inputStream));
6362
JSONArray array = object.getJSONArray("tree");
@@ -88,7 +87,7 @@ public void setChecksum(File file) throws Exception {
8887
}
8988

9089
@Override
91-
public String getFileUrl() {
90+
public InputStream getInputStream() {
9291
String build = getBuild();
9392
if (build == null) {
9493
return null;
@@ -97,7 +96,8 @@ public String getFileUrl() {
9796
if (file == null) {
9897
return null;
9998
}
100-
return String.format(downloadUrl, build, file);
99+
String url = String.format(downloadUrl, build, file);
100+
return WebUtils.getInputStreamOrNull(url, updateBuilder);
101101
}
102102

103103
@Override

lib/src/main/java/io/github/projectunified/mcserverupdater/api/GithubReleaseUpdater.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import io.github.projectunified.mcserverupdater.UpdateBuilder;
44
import io.github.projectunified.mcserverupdater.util.VersionQuery;
5-
import me.hsgamer.hscore.web.UserAgent;
6-
import me.hsgamer.hscore.web.WebUtils;
5+
import io.github.projectunified.mcserverupdater.util.WebUtils;
76
import org.json.JSONArray;
87
import org.json.JSONObject;
98
import org.json.JSONTokener;
@@ -16,7 +15,7 @@
1615
import java.util.function.Predicate;
1716
import java.util.regex.Pattern;
1817

19-
public abstract class GithubReleaseUpdater implements SimpleChecksum, UrlInputStreamUpdater {
18+
public abstract class GithubReleaseUpdater implements SimpleChecksum, InputStreamUpdater {
2019
protected final UpdateBuilder updateBuilder;
2120
protected final String version;
2221
protected final String build;
@@ -44,7 +43,7 @@ protected JSONObject getLatestRelease() {
4443
String url = releasesUrl + "?per_page=1";
4544
debug("Getting release from " + url);
4645
try {
47-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
46+
URLConnection connection = WebUtils.openConnection(url, updateBuilder);
4847
InputStream inputStream = connection.getInputStream();
4948
JSONArray array = new JSONArray(new JSONTokener(inputStream));
5049
return array.getJSONObject(0);
@@ -57,7 +56,7 @@ protected JSONObject getReleaseByTag(String tag) {
5756
String url = String.format(releasesUrl + "/tags/%s", tag);
5857
debug("Getting release from " + url);
5958
try {
60-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
59+
URLConnection connection = WebUtils.openConnection(url, updateBuilder);
6160
InputStream inputStream = connection.getInputStream();
6261
return new JSONObject(new JSONTokener(inputStream));
6362
} catch (IOException e) {
@@ -71,7 +70,7 @@ protected JSONObject getReleaseByPredicate(Predicate<JSONObject> predicate) {
7170
String pageUrl = releasesUrl + "?per_page=100&page=" + page;
7271
debug("Getting release from " + pageUrl);
7372
try {
74-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(pageUrl));
73+
URLConnection connection = WebUtils.openConnection(pageUrl, updateBuilder);
7574
InputStream inputStream = connection.getInputStream();
7675
JSONArray array = new JSONArray(new JSONTokener(inputStream));
7776
for (int i = 0; i < array.length(); i++) {
@@ -105,12 +104,11 @@ private String getBuild() {
105104
return id;
106105
}
107106

108-
@Override
109-
public String getFileUrl() {
107+
private String getFileUrl() {
110108
String assetUrl = String.format(releaseAssetUrl, build);
111109
debug("Getting asset URL from " + assetUrl);
112110
try {
113-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(assetUrl));
111+
URLConnection connection = WebUtils.openConnection(assetUrl, updateBuilder);
114112
InputStream inputStream = connection.getInputStream();
115113
JSONArray array = new JSONArray(new JSONTokener(inputStream));
116114
for (int i = 0; i < array.length(); i++) {
@@ -129,6 +127,11 @@ public String getFileUrl() {
129127
}
130128
}
131129

130+
@Override
131+
public InputStream getInputStream() {
132+
return WebUtils.getInputStreamOrNull(getFileUrl(), updateBuilder);
133+
}
134+
132135
@Override
133136
public String getChecksum() {
134137
return repo + "||" + build;

lib/src/main/java/io/github/projectunified/mcserverupdater/api/JenkinsUpdater.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import io.github.projectunified.mcserverupdater.UpdateBuilder;
44
import io.github.projectunified.mcserverupdater.util.VersionQuery;
5-
import me.hsgamer.hscore.web.UserAgent;
6-
import me.hsgamer.hscore.web.WebUtils;
5+
import io.github.projectunified.mcserverupdater.util.WebUtils;
76
import org.json.JSONArray;
87
import org.json.JSONObject;
98
import org.json.JSONTokener;
@@ -45,7 +44,7 @@ protected String getLatestSuccessfulBuild() {
4544
String treeUrl = api + "?tree=lastSuccessfulBuild[number]";
4645
debug("Getting build from " + treeUrl);
4746
try {
48-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(treeUrl));
47+
URLConnection connection = WebUtils.openConnection(treeUrl, updateBuilder);
4948
InputStream inputStream = connection.getInputStream();
5049
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
5150
JSONObject build = jsonObject.getJSONObject("lastSuccessfulBuild");
@@ -64,7 +63,7 @@ protected String getBuildByPredicate(String treeQuery, Predicate<JSONObject> pre
6463
String treeUrl = api + "?tree=builds[" + finalTreeQuery + "]";
6564
debug("Getting build from " + treeUrl);
6665
try {
67-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(treeUrl));
66+
URLConnection connection = WebUtils.openConnection(treeUrl, updateBuilder);
6867
InputStream inputStream = connection.getInputStream();
6968
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
7069
JSONArray builds = jsonObject.getJSONArray("builds");
@@ -103,7 +102,7 @@ public InputStream getInputStream() {
103102
String url = getArtifactUrl();
104103
debug("Downloading " + url);
105104
try {
106-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
105+
URLConnection connection = WebUtils.openConnection(url, updateBuilder);
107106
return connection.getInputStream();
108107
} catch (IOException e) {
109108
debug(e);
@@ -132,7 +131,7 @@ private String getArtifactUrl() {
132131
String artifact = "INVALID";
133132
debug("Getting artifact from " + artifactListUrl);
134133
try {
135-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(artifactListUrl));
134+
URLConnection connection = WebUtils.openConnection(artifactListUrl, updateBuilder);
136135
InputStream inputStream = connection.getInputStream();
137136
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
138137
JSONArray artifacts = jsonObject.getJSONArray("artifacts");

lib/src/main/java/io/github/projectunified/mcserverupdater/api/UrlInputStreamUpdater.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/src/main/java/io/github/projectunified/mcserverupdater/updater/FabricUpdater.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import io.github.projectunified.mcserverupdater.UpdateBuilder;
44
import io.github.projectunified.mcserverupdater.api.DebugConsumer;
5+
import io.github.projectunified.mcserverupdater.api.InputStreamUpdater;
56
import io.github.projectunified.mcserverupdater.api.SimpleChecksum;
6-
import io.github.projectunified.mcserverupdater.api.UrlInputStreamUpdater;
77
import io.github.projectunified.mcserverupdater.util.VersionQuery;
8-
import me.hsgamer.hscore.web.UserAgent;
9-
import me.hsgamer.hscore.web.WebUtils;
8+
import io.github.projectunified.mcserverupdater.util.WebUtils;
109
import org.json.JSONArray;
1110
import org.json.JSONObject;
1211
import org.json.JSONTokener;
@@ -15,7 +14,7 @@
1514
import java.io.InputStream;
1615
import java.net.URLConnection;
1716

18-
public class FabricUpdater implements UrlInputStreamUpdater, SimpleChecksum {
17+
public class FabricUpdater implements InputStreamUpdater, SimpleChecksum {
1918
private static final String BASE_URL = "https://meta.fabricmc.net/v2/versions";
2019
private static final String GAME_URL = BASE_URL + "/game";
2120
private static final String LOADER_URL = BASE_URL + "/loader";
@@ -50,7 +49,7 @@ private String getBuild() {
5049
private String getLatestVersion(String url) {
5150
updateBuilder.debug("Getting latest version from " + url);
5251
try {
53-
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(url));
52+
URLConnection connection = WebUtils.openConnection(url, updateBuilder);
5453
InputStream inputStream = connection.getInputStream();
5554
JSONArray jsonArray = new JSONArray(new JSONTokener(inputStream));
5655
for (int i = 0; i < jsonArray.length(); i++) {
@@ -62,7 +61,7 @@ private String getLatestVersion(String url) {
6261
}
6362
return null;
6463
} catch (Exception e) {
65-
e.printStackTrace();
64+
debug("Failed to get latest version from " + url, e);
6665
return null;
6766
}
6867
}
@@ -84,14 +83,15 @@ private String getLatestDownloadUrl(String serverVersion, String loaderVersion,
8483
}
8584

8685
@Override
87-
public String getFileUrl() {
86+
public InputStream getInputStream() {
8887
String[] split = build.split(";");
8988
if (split.length != 2) {
9089
return null;
9190
}
9291
String loaderVersion = split[0];
9392
String installerVersion = split[1];
94-
return getLatestDownloadUrl(version, loaderVersion, installerVersion);
93+
String url = getLatestDownloadUrl(version, loaderVersion, installerVersion);
94+
return WebUtils.getInputStreamOrNull(url, updateBuilder);
9595
}
9696

9797
@Override

0 commit comments

Comments
 (0)