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.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());
}
}
}

View file

@ -5,6 +5,7 @@ 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;
@ -32,14 +33,13 @@ public class BethlehemSkinServer extends AbstractSkinServer {
@Override
protected 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,11 +53,11 @@ public class BethlehemSkinServer extends AbstractSkinServer {
client.putFile(type.toString().toLowerCase(Locale.US), "image/png", image);
}
if (!client.send()) {
throw new IOException(client.getResponseText());
if (client.getResponseCode() == HttpStatus.SC_OK) {
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 org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -66,7 +67,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());
}

View file

@ -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<String, ?> 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;
}