From 7af8fa50e187d9e69e9587919e7c41c007d3814e Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sun, 22 Jul 2018 15:47:34 -0400 Subject: [PATCH] Better error handling for valhalla --- .../voxelmodpack/hdskins/gui/GuiSkins.java | 6 +-- .../hdskins/skins/LegacySkinServer.java | 4 +- .../hdskins/skins/SkinUploadResponse.java | 9 +--- .../hdskins/skins/ValhallaSkinServer.java | 46 +++++++++++-------- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java b/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java index 103454ee..966d6400 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/gui/GuiSkins.java @@ -602,11 +602,7 @@ public class GuiSkins extends GuiScreen { private void onUploadComplete(SkinUploadResponse response) { LiteLoaderLogger.info("Upload completed with: %s", response); this.uploadingSkin = false; - if (!response.isSuccess()) { - this.setUploadError(response.getMessage()); - } else { - this.pendingRemoteSkinRefresh = true; - } + this.pendingRemoteSkinRefresh = true; } static { diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java index a15b4557..36b3310e 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java @@ -109,7 +109,9 @@ public class LegacySkinServer implements SkinServer { if (response.startsWith("ERROR: ")) { response = response.substring(7); } - return new SkinUploadResponse(response.equalsIgnoreCase("OK"), response); + if (response.equalsIgnoreCase("OK") || !response.endsWith("OK")) + throw new IOException(response); + return new SkinUploadResponse(response); }, HDSkinManager.skinUploadExecutor); } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinUploadResponse.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinUploadResponse.java index 680d915e..fb37fe1d 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinUploadResponse.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinUploadResponse.java @@ -4,18 +4,12 @@ import com.google.common.base.MoreObjects; public class SkinUploadResponse { - private final boolean success; private final String message; - public SkinUploadResponse(boolean success, String message) { - this.success = success; + public SkinUploadResponse(String message) { this.message = message; } - public boolean isSuccess() { - return success; - } - public String getMessage() { return message; } @@ -23,7 +17,6 @@ public class SkinUploadResponse { @Override public String toString() { return MoreObjects.toStringHelper(this) - .add("success", success) .add("message", message) .toString(); } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java index ffd4c8c4..f66021df 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java @@ -15,6 +15,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.util.Session; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.http.HttpHeaders; +import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.methods.CloseableHttpResponse; @@ -29,7 +30,6 @@ import org.apache.http.message.BasicNameValuePair; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URI; @@ -46,7 +46,7 @@ public class ValhallaSkinServer implements SkinServer { @Expose private final String address; - private final Gson gson = new GsonBuilder() + private static final Gson gson = new GsonBuilder() .registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) .create(); @@ -64,7 +64,7 @@ public class ValhallaSkinServer implements SkinServer { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { - return Optional.of(readJson(response.getEntity().getContent(), MinecraftTexturesPayload.class)); + return Optional.of(readJson(response, MinecraftTexturesPayload.class)); } } catch (IOException e) { e.printStackTrace(); @@ -132,16 +132,13 @@ public class ValhallaSkinServer implements SkinServer { private SkinUploadResponse upload(CloseableHttpClient client, HttpUriRequest request) throws IOException { try (CloseableHttpResponse response = client.execute(request)) { - int code = response.getStatusLine().getStatusCode(); - JsonObject json = readJson(response.getEntity().getContent(), JsonObject.class); - - return new SkinUploadResponse(code == HttpStatus.SC_OK, json.get("message").getAsString()); + return readJson(response, SkinUploadResponse.class); } } private void authorize(CloseableHttpClient client, Session session) throws IOException, AuthenticationException { - if (accessToken != null) { + if (this.accessToken != null) { return; } GameProfile profile = session.getProfile(); @@ -160,11 +157,19 @@ public class ValhallaSkinServer implements SkinServer { throw new IOException("UUID mismatch!"); // probably won't ever throw } this.accessToken = response.accessToken; - } - private T readJson(InputStream in, Class cl) throws IOException { - try (Reader r = new InputStreamReader(in)) { + private T readJson(HttpResponse resp, Class cl) throws IOException { + String type = resp.getEntity().getContentType().getValue(); + String enc = resp.getEntity().getContentEncoding().getValue(); + if (!"application/json".equals(type)) { + throw new IOException("Server returned a non-json response!"); + } + try (Reader r = new InputStreamReader(resp.getEntity().getContent(), enc)) { + if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { + // TODO specific error handling + throw new IOException(gson.fromJson(r, JsonObject.class).get("message").getAsString()); + } return gson.fromJson(r, cl); } } @@ -174,7 +179,7 @@ public class ValhallaSkinServer implements SkinServer { .setUri(getHandshakeURI()) .addParameter("name", name) .build())) { - return readJson(resp.getEntity().getContent(), AuthHandshake.class); + return readJson(resp, AuthHandshake.class); } } @@ -184,7 +189,7 @@ public class ValhallaSkinServer implements SkinServer { .addParameter("name", name) .addParameter("verifyToken", String.valueOf(verifyToken)) .build())) { - return readJson(resp.getEntity().getContent(), AuthResponse.class); + return readJson(resp, AuthResponse.class); } } @@ -215,17 +220,18 @@ public class ValhallaSkinServer implements SkinServer { } @SuppressWarnings("WeakerAccess") - static class AuthHandshake { + private static class AuthHandshake { - boolean offline; - String serverId; - long verifyToken; + private boolean offline; + private String serverId; + private long verifyToken; } @SuppressWarnings("WeakerAccess") - static class AuthResponse { + private static class AuthResponse { + + private String accessToken; + private UUID userId; - String accessToken; - UUID userId; } }