From 12466bbbeda56386579522690716664ea7c7e10b Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Sat, 2 Dec 2017 19:25:00 +0800 Subject: [PATCH] add HttpRequester --- .../authlibinjector/AuthlibInjector.java | 15 +-- .../authlibinjector/util/HttpRequester.java | 114 ++++++++++++++++++ .../to2mbn/authlibinjector/util/IOUtils.java | 25 ++++ 3 files changed, 142 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/to2mbn/authlibinjector/util/HttpRequester.java create mode 100644 src/main/java/org/to2mbn/authlibinjector/util/IOUtils.java diff --git a/src/main/java/org/to2mbn/authlibinjector/AuthlibInjector.java b/src/main/java/org/to2mbn/authlibinjector/AuthlibInjector.java index f5b3405..55aa754 100644 --- a/src/main/java/org/to2mbn/authlibinjector/AuthlibInjector.java +++ b/src/main/java/org/to2mbn/authlibinjector/AuthlibInjector.java @@ -2,12 +2,12 @@ package org.to2mbn.authlibinjector; import static java.util.Optional.empty; import static java.util.Optional.of; +import static org.to2mbn.authlibinjector.util.HttpRequester.http; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.lang.instrument.ClassFileTransformer; -import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -17,7 +17,6 @@ import java.util.Optional; import java.util.function.Consumer; import org.to2mbn.authlibinjector.internal.org.json.JSONException; import org.to2mbn.authlibinjector.internal.org.json.JSONObject; -import org.to2mbn.authlibinjector.internal.org.json.JSONTokener; import org.to2mbn.authlibinjector.transform.ClassTransformer; import org.yaml.snakeyaml.Yaml; @@ -118,8 +117,8 @@ public final class AuthlibInjector { JSONObject remoteConfig; try { - remoteConfig = jsonGet(url); - } catch (IOException e) { + remoteConfig = new JSONObject(http.request("GET", url)); + } catch (IOException | JSONException e) { log("unable to configure remotely: {0}", e); return empty(); } @@ -136,12 +135,4 @@ public final class AuthlibInjector { return of(config); } - private static JSONObject jsonGet(String url) throws IOException { - try (Reader reader = new InputStreamReader(new URL(url).openStream(), StandardCharsets.UTF_8)) { - return new JSONObject(new JSONTokener(reader)); - } catch (JSONException e) { - throw new IOException("Unresolvable JSON", e); - } - } - } diff --git a/src/main/java/org/to2mbn/authlibinjector/util/HttpRequester.java b/src/main/java/org/to2mbn/authlibinjector/util/HttpRequester.java new file mode 100644 index 0000000..666e7f0 --- /dev/null +++ b/src/main/java/org/to2mbn/authlibinjector/util/HttpRequester.java @@ -0,0 +1,114 @@ +package org.to2mbn.authlibinjector.util; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.to2mbn.authlibinjector.util.IOUtils.asString; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Map; + +public class HttpRequester { + + /** Common http requester */ + public static final HttpRequester http = new HttpRequester(); + + private int timeout = 15000; + + public int getTimeout() { + return timeout; + } + + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + public String request(String method, String url) throws IOException { + return request(method, url, null); + } + + public String request(String method, String url, Map headers) throws IOException { + HttpURLConnection conn = createConnection(url, headers); + conn.setRequestMethod(method); + try { + conn.connect(); + try (InputStream in = conn.getInputStream()) { + return asString(in); + } + } catch (IOException e) { + try (InputStream in = conn.getErrorStream()) { + return readErrorStream(in, e); + } + } finally { + conn.disconnect(); + } + } + + public String requestWithPayload(String method, String url, Object payload, String contentType) throws IOException { + return requestWithPayload(method, url, payload, contentType, null); + } + + public String requestWithPayload(String method, String url, Object payload, String contentType, Map headers) throws IOException { + byte[] bytePayload; + if (payload instanceof byte[]) { + bytePayload = (byte[]) payload; + } else if (payload == null) { + bytePayload = new byte[0]; + } else { + bytePayload = String.valueOf(payload).getBytes(UTF_8); + } + + HttpURLConnection conn = createConnection(url, headers); + conn.setRequestMethod(method); + conn.setRequestProperty("Content-Type", contentType); + conn.setRequestProperty("Content-Length", String.valueOf(bytePayload.length)); + conn.setDoOutput(true); + + try { + conn.connect(); + try (OutputStream out = conn.getOutputStream()) { + out.write(bytePayload); + } + try (InputStream in = conn.getInputStream()) { + return asString(in); + } + } catch (IOException e) { + try (InputStream in = conn.getErrorStream()) { + return readErrorStream(in, e); + } + } finally { + conn.disconnect(); + } + } + + private String readErrorStream(InputStream in, IOException e) throws IOException { + if (in == null) + throw e; + + try { + return asString(in); + } catch (IOException e1) { + if (e != e1) + e1.addSuppressed(e); + + throw e1; + } + } + + private HttpURLConnection createConnection(String url, Map headers) throws IOException { + HttpURLConnection conn = createConnection(new URL(url)); + if (headers != null) + headers.forEach((key, value) -> conn.setRequestProperty(key, value)); + return conn; + } + + private HttpURLConnection createConnection(URL url) throws IOException { + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(timeout); + conn.setReadTimeout(timeout); + conn.setUseCaches(false); + return conn; + } + +} diff --git a/src/main/java/org/to2mbn/authlibinjector/util/IOUtils.java b/src/main/java/org/to2mbn/authlibinjector/util/IOUtils.java new file mode 100644 index 0000000..d0ed02a --- /dev/null +++ b/src/main/java/org/to2mbn/authlibinjector/util/IOUtils.java @@ -0,0 +1,25 @@ +package org.to2mbn.authlibinjector.util; + +import static java.nio.charset.StandardCharsets.UTF_8; +import java.io.CharArrayWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +public final class IOUtils { + + public static String asString(InputStream in) throws IOException { + CharArrayWriter w = new CharArrayWriter(); + Reader reader = new InputStreamReader(in, UTF_8); + char[] buf = new char[4096]; // 8192 bytes + int read; + while ((read = reader.read(buf)) != -1) { + w.write(buf, 0, read); + } + return new String(w.toCharArray()); + } + + private IOUtils() {} + +}