From 7f7116e1c7dcd4d4a887f2e1d85e901ef6b52d98 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 30 Jan 2018 00:08:00 +0000 Subject: [PATCH] Use OkHttp for web requests --- bukkit/pom.xml | 8 + .../luckperms/bukkit/LPBukkitPlugin.java | 5 +- bungee/pom.xml | 8 + .../luckperms/bungee/LPBungeePlugin.java | 5 +- common/pom.xml | 15 +- .../commands/impl/misc/DebugCommand.java | 24 +-- .../common/dependencies/Dependency.java | 18 ++ .../dependencies/DependencyRegistry.java | 6 + .../relocation/RelocationHelper.java | 37 ++++ .../luckperms/common/treeview/TreeView.java | 18 +- .../me/lucko/luckperms/common/utils/Gist.java | 189 ++++++++++++++++++ .../luckperms/common/utils/HttpClient.java | 79 ++++++++ .../luckperms/common/utils/PasteUtils.java | 134 ------------- .../common/verbose/VerboseListener.java | 14 +- .../common/webeditor/WebEditorUtils.java | 120 ++++------- sponge/pom.xml | 12 ++ .../luckperms/sponge/LPSpongePlugin.java | 2 + 17 files changed, 446 insertions(+), 248 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHelper.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/utils/Gist.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/utils/HttpClient.java delete mode 100644 common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 791c641b9..e210de9ac 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -56,6 +56,14 @@ com.github.benmanes.caffeine me.lucko.luckperms.lib.caffeine + + okio + me.lucko.luckperms.lib.okio + + + okhttp3 + me.lucko.luckperms.lib.okhttp3 + org.mariadb.jdbc me.lucko.luckperms.lib.mariadb diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index 1c1f6afcd..062206783 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -57,8 +57,8 @@ import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.LuckPermsCalculator; -import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.dependencies.DependencyRegistry; import me.lucko.luckperms.common.dependencies.classloader.PluginClassLoader; import me.lucko.luckperms.common.dependencies.classloader.ReflectionClassLoader; import me.lucko.luckperms.common.event.EventFactory; @@ -94,7 +94,6 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.InputStream; import java.util.Arrays; -import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -157,7 +156,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { this.pluginClassLoader = new ReflectionClassLoader(this); this.dependencyManager = new DependencyManager(this); - this.dependencyManager.loadDependencies(Collections.singleton(Dependency.CAFFEINE)); + this.dependencyManager.loadDependencies(DependencyRegistry.GLOBAL_DEPENDENCIES); } @Override diff --git a/bungee/pom.xml b/bungee/pom.xml index 11b2e48f9..3ea490153 100644 --- a/bungee/pom.xml +++ b/bungee/pom.xml @@ -56,6 +56,14 @@ com.github.benmanes.caffeine me.lucko.luckperms.lib.caffeine + + okio + me.lucko.luckperms.lib.okio + + + okhttp3 + me.lucko.luckperms.lib.okhttp3 + org.mariadb.jdbc me.lucko.luckperms.lib.mariadb diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java index 36a72939c..893edcd83 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -49,8 +49,8 @@ import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.LuckPermsCalculator; -import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.dependencies.DependencyRegistry; import me.lucko.luckperms.common.dependencies.classloader.PluginClassLoader; import me.lucko.luckperms.common.dependencies.classloader.ReflectionClassLoader; import me.lucko.luckperms.common.event.EventFactory; @@ -83,7 +83,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; -import java.util.Collections; import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -133,7 +132,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { this.pluginClassLoader = new ReflectionClassLoader(this); this.dependencyManager = new DependencyManager(this); - this.dependencyManager.loadDependencies(Collections.singleton(Dependency.CAFFEINE)); + this.dependencyManager.loadDependencies(DependencyRegistry.GLOBAL_DEPENDENCIES); } @Override diff --git a/common/pom.xml b/common/pom.xml index e95a627d3..a1347597a 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -92,7 +92,20 @@ 2.6.1 provided - + + + com.squareup.okhttp3 + okhttp + 3.9.1 + provided + + + + com.squareup.okio + okio + 1.13.0 + provided + diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/DebugCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/DebugCommand.java index f11e24cda..687d737fa 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/DebugCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/DebugCommand.java @@ -51,7 +51,7 @@ import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.processors.PermissionProcessor; -import me.lucko.luckperms.common.utils.PasteUtils; +import me.lucko.luckperms.common.utils.Gist; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.TextUtils; @@ -62,7 +62,6 @@ import net.kyori.text.event.HoverEvent; import net.kyori.text.format.TextColor; import java.util.Collection; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -81,20 +80,19 @@ public class DebugCommand extends SingleCommand { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, List args, String label) { Message.DEBUG_START.send(sender); - Map pages = new LinkedHashMap<>(); + Gist gist = Gist.builder() + .description("LuckPerms Debug Output") + .file("__DEBUG__.md", TextUtils.joinNewline("# Debug Output", "The debugging data can be found in the files below.")) + .file("platform.json", GSON.toJson(getPlatformData(plugin).toJson())) + .file("storage.json", GSON.toJson(getStorageData(plugin).toJson())) + .file("context.json", GSON.toJson(getContextData(plugin).toJson())) + .file("players.json", GSON.toJson(getPlayersData(plugin).toJson())) + .upload(); - pages.put("__DEBUG__.md", TextUtils.joinNewline("# Debug Output", "The debugging data can be found in the files below.")); - - pages.put("platform.json", GSON.toJson(getPlatformData(plugin).toJson())); - pages.put("storage.json", GSON.toJson(getStorageData(plugin).toJson())); - pages.put("context.json", GSON.toJson(getContextData(plugin).toJson())); - pages.put("players.json", GSON.toJson(getPlayersData(plugin).toJson())); - - String url = PasteUtils.paste("LuckPerms Debug Output", pages.entrySet()); Message.DEBUG_URL.send(sender); - Component message = TextComponent.builder(url).color(TextColor.AQUA) - .clickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, String.valueOf(url))) + Component message = TextComponent.builder(gist.getUrl()).color(TextColor.AQUA) + .clickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, String.valueOf(gist.getUrl()))) .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to open the debugging data.").color(TextColor.GRAY))) .build(); diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java index 57c213497..0e05e0a26 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.dependencies; import com.google.common.io.ByteStreams; import me.lucko.luckperms.common.dependencies.relocation.Relocation; +import me.lucko.luckperms.common.dependencies.relocation.RelocationHelper; import java.io.InputStream; import java.net.URL; @@ -65,6 +66,23 @@ public enum Dependency { "5F55lb0PmSThBWkRJ9mwkvN+8xT6uDZKIwKk37QW0co=", Relocation.of("caffeine", "com{}github{}benmanes{}caffeine") ), + OKIO( + "com{}squareup{}" + RelocationHelper.OKIO_STRING, + RelocationHelper.OKIO_STRING, + "1.13.0", + "c0Jpw+vFCQ47I1ZttVj0IfC0AnJ3x5rV0Xa47BaLuFA=", + Relocation.of(RelocationHelper.OKIO_STRING, RelocationHelper.OKIO_STRING) + ), + OKHTTP( + "com{}squareup{}" + RelocationHelper.OKHTTP3_STRING, + "okhttp", + "3.9.1", + "oNAQF6QruiblB/xtRIuzblNvS25hL3xC3jC72sK3eF4=", + Relocation.allOf( + Relocation.of(RelocationHelper.OKHTTP3_STRING, RelocationHelper.OKHTTP3_STRING), + Relocation.of(RelocationHelper.OKIO_STRING, RelocationHelper.OKIO_STRING) + ) + ), MARIADB_DRIVER( "org{}mariadb{}jdbc", "mariadb-java-client", diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java index f9b97703b..3b701be00 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java @@ -27,12 +27,14 @@ package me.lucko.luckperms.common.dependencies; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import me.lucko.luckperms.api.platform.PlatformType; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.StorageType; +import java.util.EnumSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -40,6 +42,10 @@ import java.util.Set; public class DependencyRegistry { + public static final Set GLOBAL_DEPENDENCIES = ImmutableSet.copyOf(EnumSet.of( + Dependency.CAFFEINE, Dependency.OKIO, Dependency.OKHTTP + )); + private static final Map> STORAGE_DEPENDENCIES = ImmutableMap.>builder() .put(StorageType.JSON, ImmutableList.of(Dependency.CONFIGURATE_CORE, Dependency.CONFIGURATE_GSON)) .put(StorageType.YAML, ImmutableList.of(Dependency.CONFIGURATE_CORE, Dependency.CONFIGURATE_YAML)) diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHelper.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHelper.java new file mode 100644 index 000000000..1fdc4c400 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/relocation/RelocationHelper.java @@ -0,0 +1,37 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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.luckperms.common.dependencies.relocation; + +public final class RelocationHelper { + + // screw maven shade + public static final String OKIO_STRING = String.valueOf(new char[]{'o', 'k', 'i', 'o'}); + public static final String OKHTTP3_STRING = String.valueOf(new char[]{'o', 'k', 'h', 't', 't', 'p', '3'}); + + private RelocationHelper() { + + } +} 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 a1a1cfb99..f3f284807 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 @@ -32,7 +32,7 @@ import com.google.common.collect.Maps; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.caching.type.PermissionCache; -import me.lucko.luckperms.common.utils.PasteUtils; +import me.lucko.luckperms.common.utils.Gist; import me.lucko.luckperms.common.verbose.CheckOrigin; import java.text.SimpleDateFormat; @@ -153,7 +153,6 @@ public class TreeView { * * @param version the plugin version string * @return the url, or null - * @see PasteUtils#paste(String, List) */ public String uploadPasteData(String version) { // only paste if there is actually data here @@ -180,7 +179,12 @@ public class TreeView { ret.clear(); // upload the return the data - return PasteUtils.paste("LuckPerms Permission Tree", ImmutableList.of(Maps.immutableEntry("luckperms-tree.md", builder.build().stream().collect(Collectors.joining("\n"))))); + Gist gist = Gist.builder() + .description("LuckPerms Permission Tree") + .file("luckperms-tree.md", builder.build().stream().collect(Collectors.joining("\n"))) + .upload(); + + return gist.getUrl(); } /** @@ -193,7 +197,6 @@ public class TreeView { * @param username the username of the reference user * @param checker the permission data instance to check against * @return the url, or null - * @see PasteUtils#paste(String, List) */ public String uploadPasteData(String version, String username, PermissionCache checker) { // only paste if there is actually data here @@ -225,7 +228,12 @@ public class TreeView { ret.clear(); // upload the return the data - return PasteUtils.paste("LuckPerms Permission Tree", ImmutableList.of(Maps.immutableEntry("luckperms-tree.md", builder.build().stream().collect(Collectors.joining("\n"))))); + Gist gist = Gist.builder() + .description("LuckPerms Permission Tree") + .file("luckperms-tree.md", builder.build().stream().collect(Collectors.joining("\n"))) + .upload(); + + return gist.getUrl(); } private static String getTristateDiffPrefix(Tristate t) { diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/Gist.java b/common/src/main/java/me/lucko/luckperms/common/utils/Gist.java new file mode 100644 index 000000000..fe65777e6 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/utils/Gist.java @@ -0,0 +1,189 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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.luckperms.common.utils; + +import com.google.common.collect.ImmutableList; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.stream.JsonWriter; + +import okhttp3.FormBody; +import okhttp3.MediaType; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +/** + * Represents a posted GitHub Gist + */ +public class Gist { + private static final Gson GSON = new Gson(); + + private static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8"); + + private static final String GIST_API_URL = "https://api.github.com/gists"; + private static final String GIT_IO_URL = "https://git.io"; + + public static Builder builder() { + return new Builder(); + } + + private final String url; + private final String id; + + private Gist(String url, String id) { + this.url = url; + this.id = id; + } + + public String getUrl() { + return this.url; + } + + public String getId() { + return this.id; + } + + private static final class GistFile { + private final String name; + private final String content; + + private GistFile(String name, String content) { + this.name = name; + this.content = content; + } + } + + public static final class Builder { + private final List files = new ArrayList<>(); + private boolean shorten = true; + private String description = "LuckPerms Gist"; + + private Builder() { + + } + + public Builder file(String name, String content) { + this.files.add(new GistFile(name, content)); + return this; + } + + public Builder shorten(boolean shorten) { + this.shorten = shorten; + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + public Gist upload() { + return Gist.upload(ImmutableList.copyOf(this.files), this.shorten, this.description); + } + } + + private static Gist upload(List files, boolean shorten, String description) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (JsonWriter jw = new JsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) { + jw.beginObject(); + jw.name("description").value(description); + jw.name("public").value(false); + jw.name("files").beginObject(); + for (GistFile file : files) { + jw.name(file.name).beginObject().name("content").value(file.content).endObject(); + } + jw.endObject().endObject(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + RequestBody body = RequestBody.create(JSON_TYPE, out.toByteArray()); + Request request = new Request.Builder() + .url(GIST_API_URL) + .post(body) + .build(); + + try (Response response = HttpClient.makeCall(request)) { + try (ResponseBody responseBody = response.body()) { + if (responseBody == null) { + throw new RuntimeException("No response"); + } + + String id; + String pasteUrl; + try (InputStream inputStream = responseBody.byteStream()) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + JsonObject object = GSON.fromJson(reader, JsonObject.class); + id = object.get("id").getAsString(); + pasteUrl = object.get("html_url").getAsString(); + } + } + + if (shorten) { + pasteUrl = shortenUrl(pasteUrl); + } + + return new Gist(pasteUrl, id); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static String shortenUrl(String pasteUrl) { + RequestBody requestBody = new FormBody.Builder() + .add("url", pasteUrl) + .build(); + + Request request = new Request.Builder() + .url(GIT_IO_URL) + .post(requestBody) + .build(); + + try (Response response = HttpClient.makeCall(request)) { + String location = response.header("Location"); + if (location == null) { + throw new RuntimeException("No location header"); + } + return location; + } catch (Exception e) { + e.printStackTrace(); + } + return pasteUrl; + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/HttpClient.java b/common/src/main/java/me/lucko/luckperms/common/utils/HttpClient.java new file mode 100644 index 000000000..686cc2b75 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/utils/HttpClient.java @@ -0,0 +1,79 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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.luckperms.common.utils; + +import okhttp3.Interceptor; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import java.io.IOException; + +/** + * Utilities for the OkHttp client + */ +public class HttpClient { + + private static final OkHttpClient CLIENT = new OkHttpClient.Builder() + .addInterceptor(new LuckPermsUserAgentInterceptor()) + .build(); + + public static Response makeCall(Request request) throws IOException { + Response response = CLIENT.newCall(request).execute(); + if (!response.isSuccessful()) { + throw exceptionForUnsuccessfulResponse(response); + } + return response; + } + + public 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 LuckPermsUserAgentInterceptor implements Interceptor { + @Override + public Response intercept(Chain chain) throws IOException { + Request orig = chain.request(); + Request modified = orig.newBuilder() + .header("User-Agent", "luckperms") + .build(); + + return chain.proceed(modified); + } + } + + private HttpClient() {} + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java b/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java deleted file mode 100644 index ea1a424b8..000000000 --- a/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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.luckperms.common.utils; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.stream.JsonWriter; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.StringWriter; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.Map; - -/** - * Uploads content to GitHub's GIST service. - */ -public class PasteUtils { - private static final String GIST_API = "https://api.github.com/gists"; - private static final String SHORTEN_API = "https://git.io"; - - /** - * Uploads content to GIST, and returns a shortened URL. - * - * @param desc the description of the gist - * @param files the files to include in the gist (file name --> content) - * @return the url, or null - */ - public static String paste(String desc, Iterable> files) { - HttpURLConnection connection = null; - try { - connection = (HttpURLConnection) new URL(GIST_API).openConnection(); - connection.setRequestProperty("User-Agent", "luckperms"); - connection.setRequestMethod("POST"); - connection.setDoOutput(true); - - try (OutputStream os = connection.getOutputStream()) { - StringWriter sw = new StringWriter(); - - JsonWriter jw = new JsonWriter(sw) - .beginObject() - .name("description").value(desc) - .name("public").value(false) - .name("files") - .beginObject(); - - for (Map.Entry file : files) { - jw.name(file.getKey()).beginObject().name("content").value(file.getValue()).endObject(); - } - - jw.endObject().endObject(); - os.write(sw.toString().getBytes(StandardCharsets.UTF_8)); - } - - if (connection.getResponseCode() >= 400) { - throw new RuntimeException("Connection returned response code: " + connection.getResponseCode() + " - " + connection.getResponseMessage()); - } - - String pasteUrl; - try (InputStream inputStream = connection.getInputStream()) { - try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { - try (BufferedReader reader = new BufferedReader(inputStreamReader)) { - JsonObject response = new Gson().fromJson(reader, JsonObject.class); - pasteUrl = response.get("html_url").getAsString(); - } - } - } - - connection.disconnect(); - - try { - connection = (HttpURLConnection) new URL(SHORTEN_API).openConnection(); - connection.setRequestProperty("User-Agent", "luckperms"); - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - connection.setRequestMethod("GET"); - connection.setDoOutput(true); - try (OutputStream os = connection.getOutputStream()) { - os.write(("url=" + pasteUrl).getBytes(StandardCharsets.UTF_8)); - } - - if (connection.getResponseCode() >= 400) { - new RuntimeException("Connection returned response code: " + connection.getResponseCode() + " - " + connection.getResponseMessage()).printStackTrace(); - } else { - String shortUrl = connection.getHeaderField("Location"); - if (shortUrl != null) { - pasteUrl = shortUrl; - } - } - } catch (Exception e) { - e.printStackTrace(); - } - - return pasteUrl; - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - if (connection != null) { - try { - connection.disconnect(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - } -} 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 c2c7af268..32a244e69 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 @@ -26,7 +26,6 @@ package me.lucko.luckperms.common.verbose; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Maps; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.commands.CommandManager; @@ -34,7 +33,7 @@ import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.utils.DateUtil; -import me.lucko.luckperms.common.utils.PasteUtils; +import me.lucko.luckperms.common.utils.Gist; import me.lucko.luckperms.common.utils.TextUtils; import net.kyori.text.TextComponent; @@ -44,7 +43,6 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -151,7 +149,6 @@ public class VerboseListener { * @param showTraces if stack traces should be included in the output * @param attachRaw if the rawdata should be attached to the gist * @return the url - * @see PasteUtils#paste(String, List) */ public String uploadPasteData(boolean showTraces, boolean attachRaw) { @@ -250,14 +247,15 @@ public class VerboseListener { } this.results.clear(); - ImmutableList.Builder> content = ImmutableList.builder(); - content.add(Maps.immutableEntry("luckperms-verbose.md", prettyOutput.build().stream().collect(Collectors.joining("\n")))); + Gist.Builder gist = Gist.builder() + .description("LuckPerms Verbose Checking Output") + .file("luckperms-verbose.md", prettyOutput.build().stream().collect(Collectors.joining("\n"))); if (attachRaw) { - content.add(Maps.immutableEntry("raw-data.csv", csvOutput.build().stream().collect(Collectors.joining("\n")))); + gist.file("raw-data.csv", csvOutput.build().stream().collect(Collectors.joining("\n"))); } - return PasteUtils.paste("LuckPerms Verbose Checking Output", content.build()); + return gist.upload().getUrl(); } /** diff --git a/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java b/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java index ee55005fe..1c134479c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java @@ -29,7 +29,6 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.stream.JsonWriter; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.commands.sender.Sender; @@ -40,15 +39,18 @@ import me.lucko.luckperms.common.model.PermissionHolder; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.node.NodeModel; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.utils.Gist; +import me.lucko.luckperms.common.utils.HttpClient; import me.lucko.luckperms.common.utils.Uuids; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.StringWriter; -import java.net.HttpURLConnection; -import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Set; @@ -59,6 +61,7 @@ import java.util.stream.Stream; * Utility methods for interacting with the LuckPerms web permission editor. */ public final class WebEditorUtils { + private static final Gson GSON = new Gson(); private static final String FILE_NAME = "luckperms-data.json"; private static final String GIST_API_URL = "https://api.github.com/gists"; @@ -103,101 +106,56 @@ public final class WebEditorUtils { } public static String postToGist(String content) { - HttpURLConnection connection = null; - try { - connection = (HttpURLConnection) new URL(GIST_API_URL).openConnection(); - connection.addRequestProperty("User-Agent", "luckperms"); - connection.setRequestMethod("POST"); - connection.setDoOutput(true); + Gist gist = Gist.builder() + .description("LuckPerms Web Editor Data") + .shorten(false) + .file(FILE_NAME, content) + .upload(); - try (OutputStream os = connection.getOutputStream()) { - StringWriter sw = new StringWriter(); - new JsonWriter(sw).beginObject() - .name("description").value("LuckPerms Web Editor Data") - .name("public").value(false) - .name("files") - .beginObject().name(FILE_NAME) - .beginObject().name("content").value(content) - .endObject() - .endObject() - .endObject(); - - os.write(sw.toString().getBytes(StandardCharsets.UTF_8)); - } - - if (connection.getResponseCode() >= 400) { - throw new RuntimeException("Connection returned response code: " + connection.getResponseCode() + " - " + connection.getResponseMessage()); - } - - try (InputStream inputStream = connection.getInputStream()) { - try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { - try (BufferedReader reader = new BufferedReader(inputStreamReader)) { - JsonObject response = new Gson().fromJson(reader, JsonObject.class); - return response.get("id").getAsString(); - } - } - } - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - if (connection != null) { - try { - connection.disconnect(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } + return gist.getId(); } public static JsonObject getDataFromGist(String id) { - HttpURLConnection connection = null; - try { - connection = (HttpURLConnection) new URL(GIST_API_URL + "/" + id).openConnection(); - connection.addRequestProperty("User-Agent", "luckperms"); - connection.setRequestMethod("GET"); + Request request = new Request.Builder() + .url(GIST_API_URL + "/" + id) + .build(); - if (connection.getResponseCode() >= 400) { - throw new RuntimeException("Connection returned response code: " + connection.getResponseCode() + " - " + connection.getResponseMessage()); - } + try (Response response = HttpClient.makeCall(request)) { + try (ResponseBody responseBody = response.body()) { + if (responseBody == null) { + throw new RuntimeException("No response"); + } - try (InputStream inputStream = connection.getInputStream()) { - try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { - try (BufferedReader reader = new BufferedReader(inputStreamReader)) { - JsonObject response = new Gson().fromJson(reader, JsonObject.class); - JsonObject files = response.get("files").getAsJsonObject(); + try (InputStream inputStream = responseBody.byteStream()) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + JsonObject object = new Gson().fromJson(reader, JsonObject.class); + JsonObject files = object.get("files").getAsJsonObject(); JsonObject permsFile = files.get(FILE_NAME).getAsJsonObject(); // uh.. if (permsFile.get("truncated").getAsBoolean()) { - String rawUrlStr = permsFile.get("raw_url").getAsString(); - URL rawUrl = new URL(rawUrlStr); - try (InputStream rawInputStream = rawUrl.openStream()) { - try (InputStreamReader rawInputStreamReader = new InputStreamReader(rawInputStream, StandardCharsets.UTF_8)) { - try (BufferedReader rawReader = new BufferedReader(rawInputStreamReader)) { - return new Gson().fromJson(rawReader, JsonObject.class); + try (Response rawResponse = HttpClient.makeCall(new Request.Builder().url(permsFile.get("raw_url").getAsString()).build())) { + try (ResponseBody rawResponseBody = rawResponse.body()) { + if (rawResponseBody == null) { + throw new RuntimeException("No response"); + } + + try (InputStream rawInputStream = rawResponseBody.byteStream()) { + try (BufferedReader rawReader = new BufferedReader(new InputStreamReader(rawInputStream, StandardCharsets.UTF_8))) { + return GSON.fromJson(rawReader, JsonObject.class); + } } } } } else { String content = permsFile.get("content").getAsString(); - return new Gson().fromJson(content, JsonObject.class); + return GSON.fromJson(content, JsonObject.class); } } } } - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - if (connection != null) { - try { - connection.disconnect(); - } catch (Exception e) { - e.printStackTrace(); - } - } + } catch (IOException e) { + throw new RuntimeException(e); } } diff --git a/sponge/pom.xml b/sponge/pom.xml index f6a766599..c6d786bb6 100644 --- a/sponge/pom.xml +++ b/sponge/pom.xml @@ -52,6 +52,18 @@ + + com.github.benmanes.caffeine + me.lucko.luckperms.lib.caffeine + + + okio + me.lucko.luckperms.lib.okio + + + okhttp3 + me.lucko.luckperms.lib.okhttp3 + org.mariadb.jdbc me.lucko.luckperms.lib.mariadb diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index 02ebb3979..54fa04100 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -47,6 +47,7 @@ import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.LuckPermsCalculator; import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.dependencies.DependencyRegistry; import me.lucko.luckperms.common.dependencies.classloader.PluginClassLoader; import me.lucko.luckperms.common.dependencies.classloader.ReflectionClassLoader; import me.lucko.luckperms.common.event.EventFactory; @@ -197,6 +198,7 @@ public class LPSpongePlugin implements LuckPermsSpongePlugin { this.log = new SenderLogger(this, getConsoleSender()); this.pluginClassLoader = new ReflectionClassLoader(this); this.dependencyManager = new DependencyManager(this); + this.dependencyManager.loadDependencies(DependencyRegistry.GLOBAL_DEPENDENCIES); sendStartupBanner(getConsoleSender()); this.verboseHandler = new VerboseHandler(this.scheduler.async(), getVersion());