Document and fixup NetClient to be more flexible

This commit is contained in:
Sollace 2018-07-21 16:17:55 +02:00
parent e071242fd3
commit 890b9650f4
4 changed files with 67 additions and 33 deletions

View file

@ -2,6 +2,8 @@ package com.voxelmodpack.hdskins;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.voxelmodpack.hdskins.util.NetClient;
import net.minecraft.client.renderer.IImageBuffer; import net.minecraft.client.renderer.IImageBuffer;
import net.minecraft.client.renderer.texture.SimpleTexture; import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.client.renderer.texture.TextureUtil; 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.HttpHeaders;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -33,6 +33,7 @@ public class ThreadDownloadImageETag extends SimpleTexture {
@Nonnull @Nonnull
private final File cacheFile; private final File cacheFile;
private final File eTagFile; private final File eTagFile;
private final String imageUrl; private final String imageUrl;
@ -41,6 +42,7 @@ public class ThreadDownloadImageETag extends SimpleTexture {
@Nullable @Nullable
private BufferedImage bufferedImage; private BufferedImage bufferedImage;
@Nullable @Nullable
private Thread imageThread; private Thread imageThread;
private boolean textureUploaded; private boolean textureUploaded;
@ -94,12 +96,10 @@ public class ThreadDownloadImageETag extends SimpleTexture {
} }
private void loadTexture() { private void loadTexture() {
HttpResponse response = null; try (NetClient client = new NetClient("GET", imageUrl)) {
try { CloseableHttpResponse response = client.getResponse();
response = HttpClientBuilder.create().build().execute(new HttpGet(imageUrl));
int status = response.getStatusLine().getStatusCode(); if (client.getResponseCode() == HttpStatus.SC_NOT_FOUND) {
if (status == HttpStatus.SC_NOT_FOUND) {
// delete the cache files in case we can't connect in the future // delete the cache files in case we can't connect in the future
clearCache(); clearCache();
} else if (checkETag(response)) { } else if (checkETag(response)) {
@ -129,10 +129,6 @@ public class ThreadDownloadImageETag extends SimpleTexture {
} }
} }
LOGGER.error("Couldn't load skin {} ", imageUrl, e); LOGGER.error("Couldn't load skin {} ", imageUrl, e);
} finally {
if (response != null) {
EntityUtils.consumeQuietly(response.getEntity());
}
} }
} }

View file

@ -5,6 +5,7 @@ import java.net.URI;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.HttpStatus;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
@ -32,14 +33,13 @@ public class BethlehemSkinServer extends AbstractSkinServer {
@Override @Override
protected MinecraftTexturesPayload getProfileData(GameProfile profile) { protected MinecraftTexturesPayload getProfileData(GameProfile profile) {
try (NetClient client = new NetClient("GET", getPath(profile))) { try (NetClient client = new NetClient("GET", getPath(profile))) {
if (!client.send()) { if (client.getResponseCode() == HttpStatus.SC_OK) {
return null; return gson.fromJson(client.getResponseText(), MinecraftTexturesPayload.class);
} }
return gson.fromJson(client.getResponseText(), MinecraftTexturesPayload.class);
} catch (IOException e) { } catch (IOException e) {
return null;
} }
return null;
} }
@Override @Override
@ -53,11 +53,11 @@ public class BethlehemSkinServer extends AbstractSkinServer {
client.putFile(type.toString().toLowerCase(Locale.US), "image/png", image); client.putFile(type.toString().toLowerCase(Locale.US), "image/png", image);
} }
if (!client.send()) { if (client.getResponseCode() == HttpStatus.SC_OK) {
throw new IOException(client.getResponseText()); return new SkinUploadResponse(client.getResponseText());
} }
return new SkinUploadResponse(client.getResponseText()); throw new IOException(client.getResponseText());
} }
} }

View file

@ -13,6 +13,7 @@ import com.mojang.util.UUIDTypeAdapter;
import net.minecraft.util.Session; import net.minecraft.util.Session;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -66,7 +67,7 @@ public class LegacySkinServer extends AbstractSkinServer {
String url = getPath(address, type, profile); String url = getPath(address, type, profile);
try (NetClient client = new NetClient("GET", url)) { try (NetClient client = new NetClient("GET", url)) {
if (!client.send()) { if (client.getResponseCode() != HttpStatus.SC_OK) {
throw new IOException("Bad response code: " + client.getResponseCode()); throw new IOException("Bad response code: " + client.getResponseCode());
} }

View file

@ -18,6 +18,7 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/** /**
* Ew. Why so many builders? >.< * Ew. Why so many builders? >.<
@ -36,18 +37,34 @@ public class NetClient implements Closeable {
start(method, uri); 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) { public NetClient start(String method, String uri) {
rqBuilder = RequestBuilder.create(method).setUri(uri); rqBuilder = RequestBuilder.create(method).setUri(uri);
headers = null; headers = null;
if (response != null) { if (response != null) {
IOUtils.closeQuietly(response); EntityUtils.consumeQuietly(response.getEntity());
response = null; response = null;
} }
return this; 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) { public NetClient putFile(String key, String contentType, URI file) {
File f = new File(file); File f = new File(file);
HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody(key, f, ContentType.create(contentType), f.getName()).build(); HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody(key, f, ContentType.create(contentType), f.getName()).build();
@ -57,13 +74,22 @@ public class NetClient implements Closeable {
return this; return this;
} }
/**
* Sets the headers to be included with this request.
* @param headers Headers to send
*
* @return itself for chaining
*/
public NetClient putHeaders(Map<String, ?> headers) { public NetClient putHeaders(Map<String, ?> headers) {
this.headers = headers; this.headers = headers;
return this; return this;
} }
public boolean send() { /**
* Commits and sends the request.
*/
private void send() {
HttpUriRequest request = rqBuilder.build(); HttpUriRequest request = rqBuilder.build();
if (headers != null) { if (headers != null) {
@ -78,26 +104,37 @@ public class NetClient implements Closeable {
try { try {
response = client.execute(request); response = client.execute(request);
return getResponseCode() == HttpStatus.SC_OK;
} catch (IOException e) { } } catch (IOException e) { }
return false;
} }
public int getResponseCode() { /**
* Gets or obtains the http response body.
*/
public CloseableHttpResponse getResponse() {
if (response == null) { if (response == null) {
send(); 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(); return response.getStatusLine().getStatusCode();
} }
/**
* Consumes and returns the entire response body.
*/
public String getResponseText() { public String getResponseText() {
if (response == null) { if (getResponse() == null || response.getEntity() == null) {
if (!send()) { return "";
return "";
}
} }
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
@ -119,7 +156,7 @@ public class NetClient implements Closeable {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (response != null) { if (response != null) {
IOUtils.closeQuietly(response); EntityUtils.consumeQuietly(response.getEntity());
response = null; response = null;
} }