mirror of
https://github.com/GeyserMC/Floodgate.git
synced 2025-12-19 14:59:20 +00:00
Handle (and ignore) http server downtime
This commit is contained in:
@@ -37,21 +37,19 @@ import java.util.concurrent.Executors;
|
|||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
// resources are properly closed and ignoring the original stack trace is intended
|
||||||
|
@SuppressWarnings({"PMD.CloseResource", "PMD.PreserveStackTrace"})
|
||||||
public class HttpUtils {
|
public class HttpUtils {
|
||||||
private static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
|
private static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
private static final Gson GSON = new Gson();
|
private static final Gson GSON = new Gson();
|
||||||
private static final String USER_AGENT = "GeyserMC/Floodgate";
|
private static final String USER_AGENT = "GeyserMC/Floodgate";
|
||||||
private static final String CONNECTION_STRING = "--";
|
|
||||||
private static final String BOUNDARY = "******";
|
|
||||||
private static final String END = "\r\n";
|
|
||||||
|
|
||||||
public static CompletableFuture<DefaultHttpResponse> asyncGet(String urlString) {
|
public static CompletableFuture<DefaultHttpResponse> asyncGet(String urlString) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.supplyAsync(() -> get(urlString), EXECUTOR_SERVICE);
|
||||||
return get(urlString);
|
|
||||||
}, EXECUTOR_SERVICE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DefaultHttpResponse get(String urlString) {
|
public static DefaultHttpResponse get(String urlString) {
|
||||||
@@ -83,8 +81,14 @@ public class HttpUtils {
|
|||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> HttpResponse<T> readResponseSilent(HttpURLConnection connection, Class<T> clazz) {
|
@NonNull
|
||||||
|
private static <T> HttpResponse<T> readResponseSilent(
|
||||||
|
HttpURLConnection connection,
|
||||||
|
Class<T> clazz) {
|
||||||
InputStreamReader streamReader = createReader(connection);
|
InputStreamReader streamReader = createReader(connection);
|
||||||
|
if (streamReader == null) {
|
||||||
|
return new HttpResponse<>(-1, null);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
@@ -100,8 +104,12 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
private static DefaultHttpResponse readDefaultResponse(HttpURLConnection connection) {
|
private static DefaultHttpResponse readDefaultResponse(HttpURLConnection connection) {
|
||||||
InputStreamReader streamReader = createReader(connection);
|
InputStreamReader streamReader = createReader(connection);
|
||||||
|
if (streamReader == null) {
|
||||||
|
return new DefaultHttpResponse(-1, null);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
@@ -117,8 +125,9 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private static InputStreamReader createReader(HttpURLConnection connection) {
|
private static InputStreamReader createReader(HttpURLConnection connection) {
|
||||||
InputStream stream = null;
|
InputStream stream;
|
||||||
try {
|
try {
|
||||||
stream = connection.getInputStream();
|
stream = connection.getInputStream();
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
@@ -128,8 +137,13 @@ public class HttpUtils {
|
|||||||
throw new RuntimeException("Both the input and the error stream failed?!");
|
throw new RuntimeException("Both the input and the error stream failed?!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// it's null for example when it couldn't connect to the server
|
||||||
|
if (stream != null) {
|
||||||
return new InputStreamReader(stream);
|
return new InputStreamReader(stream);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
@@ -139,7 +153,7 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static final class DefaultHttpResponse extends HttpResponse<JsonObject> {
|
public static final class DefaultHttpResponse extends HttpResponse<JsonObject> {
|
||||||
private DefaultHttpResponse(int httpCode, JsonObject response) {
|
DefaultHttpResponse(int httpCode, JsonObject response) {
|
||||||
super(httpCode, response);
|
super(httpCode, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user