Use OkHttp for web requests

This commit is contained in:
Luck 2018-01-30 00:08:00 +00:00
parent 0cc1c4e5a7
commit 7f7116e1c7
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
17 changed files with 446 additions and 248 deletions

View File

@ -56,6 +56,14 @@
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>me.lucko.luckperms.lib.caffeine</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>me.lucko.luckperms.lib.okio</shadedPattern>
</relocation>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>me.lucko.luckperms.lib.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>org.mariadb.jdbc</pattern>
<shadedPattern>me.lucko.luckperms.lib.mariadb</shadedPattern>

View File

@ -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

View File

@ -56,6 +56,14 @@
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>me.lucko.luckperms.lib.caffeine</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>me.lucko.luckperms.lib.okio</shadedPattern>
</relocation>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>me.lucko.luckperms.lib.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>org.mariadb.jdbc</pattern>
<shadedPattern>me.lucko.luckperms.lib.mariadb</shadedPattern>

View File

@ -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

View File

@ -92,7 +92,20 @@
<version>2.6.1</version>
<scope>provided</scope>
</dependency>
<!-- okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
<scope>provided</scope>
</dependency>
<!-- okio -->
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.13.0</version>
<scope>provided</scope>
</dependency>
<!-- configurate -->
<dependency>

View File

@ -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<String> args, String label) {
Message.DEBUG_START.send(sender);
Map<String, String> 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();

View File

@ -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",

View File

@ -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<Dependency> GLOBAL_DEPENDENCIES = ImmutableSet.copyOf(EnumSet.of(
Dependency.CAFFEINE, Dependency.OKIO, Dependency.OKHTTP
));
private static final Map<StorageType, List<Dependency>> STORAGE_DEPENDENCIES = ImmutableMap.<StorageType, List<Dependency>>builder()
.put(StorageType.JSON, ImmutableList.of(Dependency.CONFIGURATE_CORE, Dependency.CONFIGURATE_GSON))
.put(StorageType.YAML, ImmutableList.of(Dependency.CONFIGURATE_CORE, Dependency.CONFIGURATE_YAML))

View File

@ -0,0 +1,37 @@
/*
* 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.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() {
}
}

View File

@ -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) {

View File

@ -0,0 +1,189 @@
/*
* 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.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<GistFile> 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<GistFile> 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;
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.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() {}
}

View File

@ -1,134 +0,0 @@
/*
* 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.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<Map.Entry<String, String>> 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<String, String> 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();
}
}
}
}
}

View File

@ -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<Map.Entry<String, String>> 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();
}
/**

View File

@ -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);
}
}

View File

@ -52,6 +52,18 @@
</relocation>
<!-- relocated dependencies -->
<relocation>
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>me.lucko.luckperms.lib.caffeine</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>me.lucko.luckperms.lib.okio</shadedPattern>
</relocation>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>me.lucko.luckperms.lib.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>org.mariadb.jdbc</pattern>
<shadedPattern>me.lucko.luckperms.lib.mariadb</shadedPattern>

View File

@ -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());