From cde23060798fed2f51e1ab943ce28f8189c62e74 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 9 Mar 2018 16:21:45 +0000 Subject: [PATCH] Compress editor/verbose/treeview data using gzip before uploading --- .../impl/generic/other/HolderEditor.java | 2 +- .../commands/impl/misc/EditorCommand.java | 2 +- .../luckperms/common/treeview/TreeView.java | 2 +- .../luckperms/common/utils/web/Pastebin.java | 39 ++++++++++++++++++- .../common/utils/web/StandardPastebin.java | 38 +++++++++++++----- .../common/verbose/VerboseListener.java | 6 +-- 6 files changed, 72 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java index 7b66ee43..ab9a67f3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java @@ -69,7 +69,7 @@ public class HolderEditor extends SubCommand { JsonObject payload = WebEditor.formPayload(Collections.singletonList(holder), sender, label, plugin); // upload the payload data to gist - String pasteId = StandardPastebin.BYTEBIN.postJson(payload).id(); + String pasteId = StandardPastebin.BYTEBIN.postJson(payload, true).id(); if (pasteId == null) { Message.EDITOR_UPLOAD_FAILURE.send(sender); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/EditorCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/EditorCommand.java index 7842f489..894ffaf6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/EditorCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/EditorCommand.java @@ -102,7 +102,7 @@ public class EditorCommand extends SingleCommand { JsonObject payload = WebEditor.formPayload(holders, sender, label, plugin); // upload the payload data to gist - String pasteId = StandardPastebin.BYTEBIN.postJson(payload).id(); + String pasteId = StandardPastebin.BYTEBIN.postJson(payload, true).id(); if (pasteId == null) { Message.EDITOR_UPLOAD_FAILURE.send(sender); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/treeview/TreeView.java b/common/src/main/java/me/lucko/luckperms/common/treeview/TreeView.java index 412d67b0..0da1d37a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/treeview/TreeView.java +++ b/common/src/main/java/me/lucko/luckperms/common/treeview/TreeView.java @@ -167,7 +167,7 @@ public class TreeView { ) .toJson(); - return StandardPastebin.BYTEBIN.postJson(payload).id(); + return StandardPastebin.BYTEBIN.postJson(payload, true).id(); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/web/Pastebin.java b/common/src/main/java/me/lucko/luckperms/common/utils/web/Pastebin.java index a7f99de3..492985c9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/web/Pastebin.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/web/Pastebin.java @@ -27,16 +27,53 @@ package me.lucko.luckperms.common.utils.web; import com.google.gson.JsonElement; +/** + * Represents a pastebin service + */ public interface Pastebin { - Paste postJson(JsonElement element); + /** + * Posts the given json to the pastebin + * + * @param element the json element to post + * @param compress whether to compress and post the data using gzip + * @return a paste + */ + Paste postJson(JsonElement element, boolean compress); + /** + * Posts "plain" content to the pastebin + * + * @param content the content + * @return a paste + */ Paste postPlain(String content); + /** + * Gets the raw url of a paste's data from an id + * + * @param id the id + * @return a url + */ String getRawUrl(String id); + /** + * Encapsulates the properties of a specific "paste" entry + */ interface Paste { + + /** + * Gets the url of the paste + * + * @return the url + */ String url(); + + /** + * Gets the unique id of the paste + * + * @return the id + */ String id(); } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/web/StandardPastebin.java b/common/src/main/java/me/lucko/luckperms/common/utils/web/StandardPastebin.java index 5d11d999..3f652a5d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/web/StandardPastebin.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/web/StandardPastebin.java @@ -37,13 +37,15 @@ import okhttp3.Response; import okhttp3.ResponseBody; import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.util.zip.GZIPOutputStream; public enum StandardPastebin implements Pastebin { @@ -98,28 +100,44 @@ public enum StandardPastebin implements Pastebin { protected abstract String parseIdFromResult(BufferedReader reader); @Override - public Pastebin.Paste postJson(JsonElement content) { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(bytes))) { + public Pastebin.Paste postJson(JsonElement content, boolean compress) { + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + + OutputStream outputStream; + if (compress) { + try { + outputStream = new GZIPOutputStream(byteOut); + } catch (IOException e) { + throw new RuntimeException(e); + } + } else { + outputStream = byteOut; + } + + try (Writer writer = new OutputStreamWriter(outputStream)) { GSON.toJson(content, writer); } catch (IOException e) { throw new RuntimeException(e); } - return post(RequestBody.create(JSON_TYPE, bytes.toByteArray())); + return post(RequestBody.create(JSON_TYPE, byteOut.toByteArray()), compress); } @Override public Pastebin.Paste postPlain(String content) { - return post(RequestBody.create(PLAIN_TYPE, content)); + return post(RequestBody.create(PLAIN_TYPE, content), false); } - private Pastebin.Paste post(RequestBody body) { - Request request = new Request.Builder() + private Pastebin.Paste post(RequestBody body, boolean compressed) { + Request.Builder requestBuilder = new Request.Builder() .url(getPostUrl()) - .post(body) - .build(); + .post(body); + if (compressed) { + requestBuilder.header("Content-Encoding", "gzip"); + } + + Request request = requestBuilder.build(); try (Response response = HttpClient.makeCall(request)) { try (ResponseBody responseBody = response.body()) { if (responseBody == null) { diff --git a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java index dff6cc99..f8c8c3c7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java +++ b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java @@ -56,11 +56,11 @@ public class VerboseListener { private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); // how much data should we store before stopping. - private static final int DATA_TRUNCATION = 3000; + private static final int DATA_TRUNCATION = 10000; // how many lines should we include in each stack trace send as a chat message private static final int STACK_TRUNCATION_CHAT = 15; // how many lines should we include in each stack trace in the web output - private static final int STACK_TRUNCATION_WEB = 20; + private static final int STACK_TRUNCATION_WEB = 30; private static final StackTracePrinter FILTERING_PRINTER = StackTracePrinter.builder() .ignoreClassStartingWith("me.lucko.luckperms.") @@ -222,7 +222,7 @@ public class VerboseListener { .add("data", data) .toJson(); - return StandardPastebin.BYTEBIN.postJson(payload).id(); + return StandardPastebin.BYTEBIN.postJson(payload, true).id(); } private static String getTristateColor(Tristate tristate) {