Add response HTTP status code check to update checker to improve efficiency

This commit is contained in:
Focusvity 2023-03-09 17:16:14 +11:00
parent 37649f1fdc
commit a62bdef5b4
No known key found for this signature in database
GPG Key ID: 85AD157561ABE94B

View File

@ -5,16 +5,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import dev.plex.PlexBase; import dev.plex.PlexBase;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -28,6 +18,17 @@ import org.bukkit.command.CommandSender;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
public class UpdateChecker implements PlexBase public class UpdateChecker implements PlexBase
{ {
/* /*
@ -164,6 +165,10 @@ public class UpdateChecker implements PlexBase
try try
{ {
HttpResponse response = client.execute(get); HttpResponse response = client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpURLConnection.HTTP_OK)
{
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0); JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0);
String jarFile = artifact.getString("fileName"); String jarFile = artifact.getString("fileName");
@ -193,13 +198,25 @@ public class UpdateChecker implements PlexBase
} }
}); });
} }
else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND)
{
sender.sendMessage(PlexUtils.mmDeserialize("<red>Could not update " + name + " as it can't be found on Jenkins."));
}
else
{
sender.sendMessage(PlexUtils.mmDeserialize("<red>Something went wrong while trying to update " + name + ". Please check the log for more information."));
PlexLog.error("Unable to update module {0} due to unexpected status code returned from Jenkins - Status Code: {1}", name, statusCode);
}
}
catch (IOException e) catch (IOException e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
catch (JSONException e) catch (JSONException e)
{ {
sender.sendMessage(PlexUtils.mmDeserialize("<red>The file could not be downloaded because Jenkins returned an invalid response. (Is it on Jenkins?)")); sender.sendMessage(PlexUtils.mmDeserialize("<red>Something went wrong while trying to gather information from Jenkins for " + name + ". Please check the log for more information"));
PlexLog.error("Unable to parse JSON information received from Jenkins - see below for more information...");
e.printStackTrace();
} }
} }
} }