aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/http
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2018-06-06 15:39:16 +0100
committerLuck <git@lucko.me>2018-06-06 15:39:16 +0100
commit7d6808cbcfbb0f61f93e536d36968eeda5bd302c (patch)
tree3271db1ffa6e4d6c1fa5ea4ccc1335b1ac746f46 /spark-common/src/main/java/me/lucko/spark/common/http
parent38f0c12483e6eda79ca36dc829ef678a736d2cef (diff)
downloadspark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.tar.gz
spark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.tar.bz2
spark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.zip
Convert to Gradle
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/http')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/http/Bytebin.java54
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/http/HttpClient.java113
2 files changed, 167 insertions, 0 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/http/Bytebin.java b/spark-common/src/main/java/me/lucko/spark/common/http/Bytebin.java
new file mode 100644
index 0000000..3cd5e4c
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/http/Bytebin.java
@@ -0,0 +1,54 @@
+package me.lucko.spark.common.http;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+
+import okhttp3.MediaType;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * Utility for uploading JSON data to bytebin.
+ */
+public final class Bytebin {
+
+ /** Media type for JSON data */
+ private static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
+ /** The URL used to upload sampling data */
+ private static final String UPLOAD_ENDPOINT = "https://bytebin.lucko.me/post";
+
+ public static String postCompressedContent(byte[] buf) throws IOException {
+ RequestBody body = RequestBody.create(JSON_TYPE, buf);
+
+ Request.Builder requestBuilder = new Request.Builder()
+ .url(UPLOAD_ENDPOINT)
+ .header("Content-Encoding", "gzip")
+ .post(body);
+
+ Request request = requestBuilder.build();
+ try (Response response = HttpClient.makeCall(request)) {
+ try (ResponseBody responseBody = response.body()) {
+ if (responseBody == null) {
+ throw new RuntimeException("No response");
+ }
+
+ try (InputStream inputStream = responseBody.byteStream()) {
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
+ JsonObject object = new Gson().fromJson(reader, JsonObject.class);
+ return object.get("key").getAsString();
+ }
+ }
+ }
+ }
+ }
+
+ private Bytebin() {}
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/http/HttpClient.java b/spark-common/src/main/java/me/lucko/spark/common/http/HttpClient.java
new file mode 100644
index 0000000..61db597
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/http/HttpClient.java
@@ -0,0 +1,113 @@
+/*
+ * This file is part of LuckPerms, licensed under the MIT License.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.lucko.spark.common.http;
+
+import okhttp3.Interceptor;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
+import java.io.IOException;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.URI;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Utility class for making http requests.
+ */
+public final class HttpClient {
+ private static OkHttpClient client = null;
+
+ private static synchronized OkHttpClient getClient() {
+ if (client == null) {
+ client = new OkHttpClient.Builder()
+ .proxySelector(new NullSafeProxySelector())
+ .addInterceptor(new UserAgentInterceptor())
+ .build();
+ }
+ return client;
+ }
+
+ public static Response makeCall(Request request) throws IOException {
+ Response response = getClient().newCall(request).execute();
+ if (!response.isSuccessful()) {
+ throw exceptionForUnsuccessfulResponse(response);
+ }
+ return response;
+ }
+
+ private static RuntimeException exceptionForUnsuccessfulResponse(Response response) {
+ String msg = "";
+ try (ResponseBody responseBody = response.body()) {
+ if (responseBody != null) {
+ msg = responseBody.string();
+ }
+ } catch (IOException e) {
+ // ignore
+ }
+ return new RuntimeException("Got response: " + response.code() + " - " + response.message() + " - " + msg);
+ }
+
+ private static final class UserAgentInterceptor implements Interceptor {
+ @Override
+ public Response intercept(Chain chain) throws IOException {
+ Request orig = chain.request();
+ Request modified = orig.newBuilder()
+ .header("User-Agent", "spark-plugin")
+ .build();
+
+ return chain.proceed(modified);
+ }
+ }
+
+ // sometimes ProxySelector#getDefault returns null, and okhttp doesn't like that
+ private static final class NullSafeProxySelector extends ProxySelector {
+ private static final List<Proxy> DIRECT = Collections.singletonList(Proxy.NO_PROXY);
+
+ @Override
+ public List<Proxy> select(URI uri) {
+ ProxySelector def = ProxySelector.getDefault();
+ if (def == null) {
+ return DIRECT;
+ }
+ return def.select(uri);
+ }
+
+ @Override
+ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+ ProxySelector def = ProxySelector.getDefault();
+ if (def != null) {
+ def.connectFailed(uri, sa, ioe);
+ }
+ }
+ }
+
+ private HttpClient() {}
+} \ No newline at end of file