From 85dff2f17c268bfc095c3f9e57e9afdaa5fe2b32 Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 30 Aug 2022 10:37:56 +0200 Subject: [PATCH] untested jdk8 port --- pom.xml | 42 +---------- .../java_api/NamelessApiBuilder.java | 44 +---------- .../namelessmc/java_api/RequestHandler.java | 75 +++++++++++-------- 3 files changed, 50 insertions(+), 111 deletions(-) diff --git a/pom.xml b/pom.xml index 429099fe..efe8978c 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 4.0.0 com.namelessmc java-api - canary + jdk8 UTF-8 @@ -22,38 +22,8 @@ maven-compiler-plugin 3.10.1 - 11 - 11 - true - - 100 - 100 - - - - org.checkerframework - checker - 3.21.4 - - - - org.checkerframework.checker.nullness.NullnessChecker - org.checkerframework.checker.optional.OptionalChecker - org.checkerframework.checker.regex.RegexChecker - org.checkerframework.checker.formatter.FormatterChecker - - - -Awarns - -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED - -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED - -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED - + 8 + 8 @@ -86,12 +56,6 @@ 3.24.0 - - com.github.mizosoft.methanol - methanol - 1.7.0 - - diff --git a/src/com/namelessmc/java_api/NamelessApiBuilder.java b/src/com/namelessmc/java_api/NamelessApiBuilder.java index 4634a9c6..ea4aa240 100644 --- a/src/com/namelessmc/java_api/NamelessApiBuilder.java +++ b/src/com/namelessmc/java_api/NamelessApiBuilder.java @@ -1,6 +1,5 @@ package com.namelessmc.java_api; -import com.github.mizosoft.methanol.Methanol; import com.google.gson.GsonBuilder; import com.namelessmc.java_api.logger.ApiLogger; import com.namelessmc.java_api.logger.PrintStreamLogger; @@ -8,11 +7,8 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import java.net.Authenticator; import java.net.MalformedURLException; -import java.net.ProxySelector; import java.net.URL; -import java.net.http.HttpClient; import java.time.Duration; import java.util.Objects; @@ -25,9 +21,6 @@ public class NamelessApiBuilder { private int responseSizeLimit = 32*1024*1024; private String userAgent = "Nameless-Java-API"; private @Nullable ApiLogger debugLogger = null; - private @Nullable ProxySelector proxy = null; - private @Nullable Authenticator authenticator = null; - private HttpClient.@Nullable Version httpVersion = null; private boolean pettyJsonRequests = false; @@ -66,16 +59,6 @@ public NamelessApiBuilder timeout(final Duration timeout) { return this; } - public NamelessApiBuilder withProxy(final @Nullable ProxySelector proxy) { - this.proxy = proxy; - return this; - } - - public NamelessApiBuilder authenticator(final @Nullable Authenticator authenticator) { - this.authenticator = authenticator; - return this; - } - public NamelessApiBuilder pettyJsonRequests() { this.pettyJsonRequests = true; return this; @@ -86,30 +69,7 @@ public NamelessApiBuilder responseSizeLimit(int responseSizeLimitBytes) { return this; } - public NamelessApiBuilder httpversion(final HttpClient. @Nullable Version httpVersion) { - this.httpVersion = httpVersion; - return this; - } - public NamelessAPI build() { - final Methanol.Builder methanolBuilder = Methanol.newBuilder() - .defaultHeader("Authorization", "Bearer " + this.apiKey) - .userAgent(this.userAgent) - .autoAcceptEncoding(true) - .readTimeout(this.timeout) - .requestTimeout(this.timeout) - .connectTimeout(this.timeout) - .headersTimeout(this.timeout); - if (this.proxy != null) { - methanolBuilder.proxy(this.proxy); - } - if (this.authenticator != null) { - methanolBuilder.authenticator(this.authenticator); - } - if (this.httpVersion != null) { - methanolBuilder.version(this.httpVersion); - } - GsonBuilder gsonBuilder = new GsonBuilder() .disableHtmlEscaping(); @@ -120,7 +80,9 @@ public NamelessAPI build() { return new NamelessAPI( new RequestHandler( this.apiUrl, - methanolBuilder.build(), + this.apiKey, + this.userAgent, + this.timeout, gsonBuilder.create(), this.debugLogger, this.responseSizeLimit diff --git a/src/com/namelessmc/java_api/RequestHandler.java b/src/com/namelessmc/java_api/RequestHandler.java index 34e81fb4..ff2e7366 100644 --- a/src/com/namelessmc/java_api/RequestHandler.java +++ b/src/com/namelessmc/java_api/RequestHandler.java @@ -1,7 +1,5 @@ package com.namelessmc.java_api; -import com.github.mizosoft.methanol.Methanol; -import com.github.mizosoft.methanol.MutableRequest; import com.google.common.base.Ascii; import com.google.common.base.Preconditions; import com.google.common.io.ByteStreams; @@ -18,13 +16,14 @@ import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import java.net.URLEncoder; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.util.Arrays; import java.util.Objects; import java.util.function.Supplier; @@ -33,18 +32,24 @@ public class RequestHandler { private final @NonNull URL apiUrl; - private final @NonNull Methanol httpClient; + private final String apiKey; + private final String userAgent; + private final int timeoutMillis; private final @Nullable ApiLogger debugLogger; private final @NonNull Gson gson; private final int responseLengthLimit; RequestHandler(final @NonNull URL apiUrl, - final @NonNull Methanol httpClient, + String apiKey, + String userAgent, + Duration timeout, final @NonNull Gson gson, final @Nullable ApiLogger debugLogger, final int responseLengthLimit) { this.apiUrl = Objects.requireNonNull(apiUrl, "API URL is null"); - this.httpClient = Objects.requireNonNull(httpClient, "http client is null"); + this.apiKey = apiKey; + this.userAgent = userAgent; + this.timeoutMillis = (int) timeout.toMillis(); this.gson = gson; this.debugLogger = debugLogger; this.responseLengthLimit = responseLengthLimit; @@ -108,31 +113,42 @@ private void debug(final @NonNull Supplier messageSupplier) { "legal in domain names, the Java URI class (and the Java HttpClient) does not accept them, because it uses the specification " + "for 'host names' not 'domain names'."); } - final MutableRequest request = MutableRequest.create(uri); - debug(() -> "Making connection " + (postBody != null ? "POST" : "GET") + " to " + request.uri()); + debug(() -> "Making connection " + (postBody != null ? "POST" : "GET") + " to " + uri); final long requestStartTime = System.currentTimeMillis(); - if (postBody != null) { - byte[] postBytes = gson.toJson(postBody).getBytes(StandardCharsets.UTF_8); - request.POST(HttpRequest.BodyPublishers.ofByteArray(postBytes)); - request.header("Content-Type", "application/json"); - - debug(() -> "POST request body:\n" + new String(postBytes, StandardCharsets.UTF_8)); - } else { - request.GET(); - } - - request.header("Accept", "application/json"); - int statusCode; String responseBody; try { - HttpResponse httpResponse = httpClient.send(request, - HttpResponse.BodyHandlers.ofInputStream()); - statusCode = httpResponse.statusCode(); - responseBody = getBodyAsString(httpResponse); + HttpURLConnection http = (HttpURLConnection) uri.toURL().openConnection(); + + http.setRequestProperty("Accept", "application/json"); + http.setRequestProperty("Authorization", "Bearer " + this.apiKey); + http.setRequestProperty("User-Agent", this.userAgent); + http.setConnectTimeout(timeoutMillis); + http.setReadTimeout(timeoutMillis); + + if (postBody != null) { + http.setRequestMethod("POST"); + byte[] postBytes = gson.toJson(postBody).getBytes(StandardCharsets.UTF_8); + http.setRequestProperty("Content-Type", "application/json"); + http.setRequestProperty("Content-Length", String.valueOf(postBytes.length)); + http.setDoOutput(true); + + try (OutputStream output = http.getOutputStream()) { + output.write(postBytes); + } + + debug(() -> "POST request body:\n" + new String(postBytes, StandardCharsets.UTF_8)); + } else { + http.setRequestMethod("GET"); + } + + http.setDoInput(true); + + statusCode = http.getResponseCode(); + responseBody = getBodyAsString(http.getInputStream()); } catch (final IOException e) { final @Nullable String exceptionMessage = e.getMessage(); final StringBuilder message = new StringBuilder(); @@ -157,8 +173,6 @@ private void debug(final @NonNull Supplier messageSupplier) { } throw new NamelessException(message.toString(), e); - } catch (InterruptedException e) { - throw new RuntimeException(e); } debug(() -> "Website response body, after " + (System.currentTimeMillis() - requestStartTime) + "ms:\n" + regularAsciiOnly(responseBody)); @@ -230,10 +244,9 @@ private void debug(final @NonNull Supplier messageSupplier) { return json; } - private String getBodyAsString(HttpResponse response) throws IOException { - try (InputStream in = response.body(); - InputStream limited = ByteStreams.limit(in, this.responseLengthLimit)) { - byte[] bytes = limited.readAllBytes(); + private String getBodyAsString(InputStream in) throws IOException { + try (InputStream limited = ByteStreams.limit(in, this.responseLengthLimit)) { + byte[] bytes = ByteStreams.toByteArray(limited); if (bytes.length == this.responseLengthLimit) { throw new IOException("Response larger than limit of " + this.responseLengthLimit + " bytes."); }