quick-e2e: Implement PaperDownloadService and configuration management for server file downloads

This commit is contained in:
Oliver
2025-03-16 17:50:00 +01:00
parent b2fc904441
commit 4c4e617bf5
6 changed files with 105 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ group = "de.oliver"
description = "Tool to setup a complete environment for testing"
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
toolchain.languageVersion.set(JavaLanguageVersion.of(23))
}
repositories {
@@ -42,7 +42,7 @@ tasks {
// Set the release flag. This configures what version bytecode the compiler will emit, as well as what JDK APIs are usable.
// See https://openjdk.java.net/jeps/247 for more information.
options.release.set(21)
options.release.set(23)
}
java {

View File

@@ -1,9 +1,23 @@
package de.oliver.quicke2e;
import de.oliver.quicke2e.config.Configuration;
import de.oliver.quicke2e.paper.PaperDownloadService;
public class Main {
public static void main(String[] args) {
Configuration config = new Configuration(
"paper",
"1.21.4",
"latest",
new String[]{},
true,
new String[]{"OliverHD"},
"25565"
);
PaperDownloadService paper = new PaperDownloadService();
paper.downloadServerFile(config.type(), config.version(), config.build());
}
}

View File

@@ -0,0 +1,12 @@
package de.oliver.quicke2e.config;
public record Configuration(
String type,
String version,
String build,
String[] plugins,
boolean eula,
String[] opPlayers,
String port
) {
}

View File

@@ -0,0 +1,11 @@
package de.oliver.quicke2e.paper;
import com.google.gson.annotations.SerializedName;
public record BuildsResponse(
@SerializedName("project_id") String projectID,
@SerializedName("project_name") String projectName,
String version,
String[] builds
) {
}

View File

@@ -0,0 +1,63 @@
package de.oliver.quicke2e.paper;
import com.google.gson.Gson;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PaperDownloadService {
private static final Gson GSON = new Gson();
private static final String BASE_URL = "https://api.papermc.io/v2";
private static final String destination = "servers";
private final HttpClient client;
public PaperDownloadService() {
this.client = HttpClient.newHttpClient();
}
public void downloadServerFile(
String project,
String version,
String build
) {
Path folderPath = Paths.get(String.format("%s/%s_%s_%s/", destination, project, version, build));
if (!folderPath.toFile().exists()) {
folderPath.toFile().mkdirs();
}
String buildNumber = build.equals("latest") ? getLatestBuildNumber(project, version) : build;
Path filePath = Paths.get(String.format("%s/%s_%s_%s/%s-%s-%s.jar", destination, project, version, build, project, version, buildNumber));
HttpRequest req = HttpRequest.newBuilder()
.GET()
.uri(URI.create(String.format("%s/projects/%s/versions/%s/builds/%s/downloads/%s-%s-%s.jar", BASE_URL, project, version, buildNumber, project, version, buildNumber)))
.build();
client.sendAsync(req, HttpResponse.BodyHandlers.ofFile(filePath))
.thenAccept(_ -> System.out.println("Downloaded server file to " + filePath))
.join();
}
private String getLatestBuildNumber(
String project,
String version
) {
HttpRequest req = HttpRequest.newBuilder()
.GET()
.uri(URI.create(String.format("%s/projects/%s/versions/%s", BASE_URL, project, version)))
.build();
HttpResponse<String> resp = client.sendAsync(req, HttpResponse.BodyHandlers.ofString()).join();
BuildsResponse builds = GSON.fromJson(resp.body(), BuildsResponse.class);
return builds.builds()[builds.builds().length - 1];
}
}