From 6a2de216f927006cb3629b8dcbf04181a7b3a232 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 21 Jul 2018 16:17:55 +0200 Subject: [PATCH] Document and fixup NetClient to be more flexible --- .../hdskins/ThreadDownloadImageETag.java | 20 +++--- .../hdskins/server/BethlehemSkinServer.java | 13 ++-- .../hdskins/server/LegacySkinServer.java | 3 +- .../voxelmodpack/hdskins/util/NetClient.java | 61 +++++++++++++++---- 4 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java b/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java index 96c9fb69..4ac7e4e0 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/ThreadDownloadImageETag.java @@ -2,6 +2,8 @@ package com.voxelmodpack.hdskins; import com.google.common.base.Charsets; import com.google.common.io.Files; +import com.voxelmodpack.hdskins.util.NetClient; + import net.minecraft.client.renderer.IImageBuffer; import net.minecraft.client.renderer.texture.SimpleTexture; import net.minecraft.client.renderer.texture.TextureUtil; @@ -12,9 +14,7 @@ import org.apache.http.Header; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -33,6 +33,7 @@ public class ThreadDownloadImageETag extends SimpleTexture { @Nonnull private final File cacheFile; + private final File eTagFile; private final String imageUrl; @@ -41,6 +42,7 @@ public class ThreadDownloadImageETag extends SimpleTexture { @Nullable private BufferedImage bufferedImage; + @Nullable private Thread imageThread; private boolean textureUploaded; @@ -94,12 +96,10 @@ public class ThreadDownloadImageETag extends SimpleTexture { } private void loadTexture() { - HttpResponse response = null; - try { - response = HttpClientBuilder.create().build().execute(new HttpGet(imageUrl)); + try (NetClient client = new NetClient("GET", imageUrl)) { + CloseableHttpResponse response = client.getResponse(); - int status = response.getStatusLine().getStatusCode(); - if (status == HttpStatus.SC_NOT_FOUND) { + if (client.getResponseCode() == HttpStatus.SC_NOT_FOUND) { // delete the cache files in case we can't connect in the future clearCache(); } else if (checkETag(response)) { @@ -129,10 +129,6 @@ public class ThreadDownloadImageETag extends SimpleTexture { } } LOGGER.error("Couldn't load skin {} ", imageUrl, e); - } finally { - if (response != null) { - EntityUtils.consumeQuietly(response.getEntity()); - } } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/server/BethlehemSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/server/BethlehemSkinServer.java index ffa7232a..3ce4c963 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/server/BethlehemSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/server/BethlehemSkinServer.java @@ -5,6 +5,8 @@ import java.net.URI; import java.util.Locale; import java.util.Map; import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.http.HttpStatus; + import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import com.google.gson.annotations.Expose; @@ -32,14 +34,13 @@ public class BethlehemSkinServer extends AbstractSkinServer { @Override public MinecraftTexturesPayload getProfileData(GameProfile profile) { try (NetClient client = new NetClient("GET", getPath(profile))) { - if (!client.send()) { - return null; + if (client.getResponseCode() == HttpStatus.SC_OK) { + return gson.fromJson(client.getResponseText(), MinecraftTexturesPayload.class); } - - return gson.fromJson(client.getResponseText(), MinecraftTexturesPayload.class); } catch (IOException e) { - return null; + } + return null; } @Override @@ -53,7 +54,7 @@ public class BethlehemSkinServer extends AbstractSkinServer { client.putFile(type.toString().toLowerCase(Locale.US), "image/png", image); } - return new SkinUploadResponse(client.send(), client.getResponseText()); + return new SkinUploadResponse(client.getResponseCode() == HttpStatus.SC_OK, client.getResponseText()); } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/server/LegacySkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/server/LegacySkinServer.java index f9fa7f17..7a0a8522 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/server/LegacySkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/server/LegacySkinServer.java @@ -14,6 +14,7 @@ import com.voxelmodpack.hdskins.util.NetClient; import net.minecraft.util.Session; import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.http.HttpStatus; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -67,7 +68,7 @@ public class LegacySkinServer extends AbstractSkinServer { String url = getPath(address, type, profile); try (NetClient client = new NetClient("GET", url)) { - if (!client.send()) { + if (client.getResponseCode() != HttpStatus.SC_OK) { throw new IOException("Bad response code: " + client.getResponseCode()); } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/util/NetClient.java b/src/hdskins/java/com/voxelmodpack/hdskins/util/NetClient.java index 5f82eaa8..098918b2 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/util/NetClient.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/util/NetClient.java @@ -18,6 +18,7 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; /** * Ew. Why so many builders? >.< @@ -36,18 +37,34 @@ public class NetClient implements Closeable { start(method, uri); } + /** + * Starts a new network request. + * + * @param method The HTTP method verb. GET/PUT/POST/DELETE/OPTIONS + * @param uri Http link to query + * + * @return Itself for chaining + */ public NetClient start(String method, String uri) { rqBuilder = RequestBuilder.create(method).setUri(uri); headers = null; if (response != null) { - IOUtils.closeQuietly(response); + EntityUtils.consumeQuietly(response.getEntity()); response = null; } return this; } + /** + * Adds a file to the request. Typically used with PUT/POST for uploading. + * @param key Key identifier to index the file in the request. + * @param contentType Type of file being sent. Usually the mime-type. + * @param file The file or a link to the file. + * + * @return itself for chaining + */ public NetClient putFile(String key, String contentType, URI file) { File f = new File(file); HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody(key, f, ContentType.create(contentType), f.getName()).build(); @@ -57,13 +74,22 @@ public class NetClient implements Closeable { return this; } + /** + * Sets the headers to be included with this request. + * @param headers Headers to send + * + * @return itself for chaining + */ public NetClient putHeaders(Map headers) { this.headers = headers; return this; } - public boolean send() { + /** + * Commits and sends the request. + */ + private void send() { HttpUriRequest request = rqBuilder.build(); if (headers != null) { @@ -78,26 +104,37 @@ public class NetClient implements Closeable { try { response = client.execute(request); - - return getResponseCode() == HttpStatus.SC_OK; } catch (IOException e) { } - - return false; } - public int getResponseCode() { + /** + * Gets or obtains the http response body. + */ + public CloseableHttpResponse getResponse() { if (response == null) { send(); } + return response; + } + + /** + * Gets or obtains a response status code. + */ + public int getResponseCode() { + if (getResponse() == null) { + return HttpStatus.SC_NOT_FOUND; + } + return response.getStatusLine().getStatusCode(); } + /** + * Consumes and returns the entire response body. + */ public String getResponseText() { - if (response == null) { - if (!send()) { - return ""; - } + if (getResponse() == null || response.getEntity() == null) { + return ""; } try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { @@ -119,7 +156,7 @@ public class NetClient implements Closeable { @Override public void close() throws IOException { if (response != null) { - IOUtils.closeQuietly(response); + EntityUtils.consumeQuietly(response.getEntity()); response = null; }