diff options
Diffstat (limited to 'common/src')
8 files changed, 945 insertions, 0 deletions
diff --git a/common/src/main/java/com/sk89q/warmroast/Sampler.java b/common/src/main/java/com/sk89q/warmroast/Sampler.java new file mode 100644 index 0000000..6c4f60c --- /dev/null +++ b/common/src/main/java/com/sk89q/warmroast/Sampler.java @@ -0,0 +1,156 @@ +/* + * WarmRoast + * Copyright (C) 2013 Albert Pham <http://www.sk89q.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package com.sk89q.warmroast; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.CompletableFuture; + +/** + * Main sampler class. + */ +public class Sampler extends TimerTask { + + /** + * The thread management interface for the current JVM + */ + private final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); + + /** + * A map of root stack nodes for each thread with sampling data + */ + private final Map<String, StackNode> threadData = new HashMap<>(); + + /** + * A future to encapsulation the completion of this sampler instance + */ + private final CompletableFuture<Sampler> future = new CompletableFuture<>(); + + /** The interval to wait between sampling, in milliseconds */ + private final int interval; + /** The instance used to generate thread information for use in sampling */ + private final ThreadDumper threadDumper; + /** The time when sampling first began */ + private long startTime = -1; + /** The unix timestamp (in millis) when this sampler should automatically complete.*/ + private final long endTime; // -1 for nothing + + public Sampler(int interval, ThreadDumper threadDumper, long endTime) { + this.interval = interval; + this.threadDumper = threadDumper; + this.endTime = endTime; + } + + /** + * Starts the sampler. + * + * @param timer the timer to schedule the sampling on + */ + public synchronized void start(Timer timer) { + timer.scheduleAtFixedRate(this, 0, this.interval); + this.startTime = System.currentTimeMillis(); + } + + /** + * Gets the sampling data recorded by this instance. + * + * @return the data + */ + public Map<String, StackNode> getData() { + return this.threadData; + } + + public long getStartTime() { + if (this.startTime == -1) { + throw new IllegalStateException("Not yet started"); + } + return this.startTime; + } + + public long getEndTime() { + return this.endTime; + } + + public CompletableFuture<Sampler> getFuture() { + return this.future; + } + + private StackNode getRootNode(String threadName) { + return this.threadData.computeIfAbsent(threadName, StackNode::new); + } + + @Override + public synchronized void run() { + try { + if (this.endTime != -1 && this.endTime <= System.currentTimeMillis()) { + this.future.complete(this); + cancel(); + return; + } + + ThreadInfo[] threadDumps = this.threadDumper.dumpThreads(this.threadBean); + for (ThreadInfo threadInfo : threadDumps) { + String threadName = threadInfo.getThreadName(); + StackTraceElement[] stack = threadInfo.getStackTrace(); + + if (threadName == null || stack == null) { + continue; + } + + StackNode node = getRootNode(threadName); + node.log(stack, this.interval); + } + } catch (Throwable t) { + this.future.completeExceptionally(t); + cancel(); + } + } + + public JsonObject formOutput() { + JsonObject out = new JsonObject(); + + JsonArray threads = new JsonArray(); + + List<Map.Entry<String, StackNode>> data = new ArrayList<>(getData().entrySet()); + data.sort(Map.Entry.comparingByKey()); + + for (Map.Entry<String, StackNode> entry : data) { + JsonObject o = new JsonObject(); + o.addProperty("threadName", entry.getKey()); + o.addProperty("totalTime", entry.getValue().getTotalTime()); + o.add("rootNode", entry.getValue().serialize()); + + threads.add(o); + } + out.add("threads", threads); + + return out; + } + +} diff --git a/common/src/main/java/com/sk89q/warmroast/StackNode.java b/common/src/main/java/com/sk89q/warmroast/StackNode.java new file mode 100644 index 0000000..85941a3 --- /dev/null +++ b/common/src/main/java/com/sk89q/warmroast/StackNode.java @@ -0,0 +1,135 @@ +/* + * WarmRoast + * Copyright (C) 2013 Albert Pham <http://www.sk89q.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package com.sk89q.warmroast; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Represents a node in the overall sampling stack. + * + * <p>The base implementation of this class is only used for the root of node structures. The + * {@link StackTraceNode} class is used for representing method calls in the structure.</p> + */ +public class StackNode implements Comparable<StackNode> { + + /** + * The name of this node + */ + private final String name; + + /** + * A map of this nodes children + */ + private final Map<String, StackNode> children = new HashMap<>(); + + /** + * The accumulated sample time for this node + */ + private long totalTime = 0; + + public StackNode(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public Collection<StackNode> getChildren() { + if (this.children.isEmpty()) { + return Collections.emptyList(); + } + + List<StackNode> list = new ArrayList<>(this.children.values()); + list.sort(null); + return list; + } + + private StackNode resolveChild(String name) { + return this.children.computeIfAbsent(name, StackNode::new); + } + + private StackNode resolveChild(String className, String methodName) { + return this.children.computeIfAbsent(StackTraceNode.formName(className, methodName), name -> new StackTraceNode(className, methodName)); + } + + public long getTotalTime() { + return this.totalTime; + } + + public void accumulateTime(long time) { + this.totalTime += time; + } + + private void log(StackTraceElement[] elements, int skip, long time) { + accumulateTime(time); + + if (elements.length - skip == 0) { + return; + } + + StackTraceElement bottom = elements[elements.length - (skip + 1)]; + resolveChild(bottom.getClassName(), bottom.getMethodName()).log(elements, skip + 1, time); + } + + public void log(StackTraceElement[] elements, long time) { + log(elements, 0, time); + } + + @Override + public int compareTo(StackNode o) { + return getName().compareTo(o.getName()); + } + + public JsonObject serialize() { + JsonObject ret = new JsonObject(); + + // append metadata about this node + ret.addProperty("name", getName()); + appendMetadata(ret); + + // include the total time recorded for this node + ret.addProperty("totalTime", getTotalTime()); + + // append child nodes, if any are present + Collection<StackNode> childNodes = getChildren(); + if (!childNodes.isEmpty()) { + JsonArray children = new JsonArray(); + for (StackNode child : childNodes) { + children.add(child.serialize()); + } + ret.add("children", children); + } + + return ret; + } + + protected void appendMetadata(JsonObject obj) { + + } + +} diff --git a/common/src/main/java/com/sk89q/warmroast/StackTraceNode.java b/common/src/main/java/com/sk89q/warmroast/StackTraceNode.java new file mode 100644 index 0000000..016d700 --- /dev/null +++ b/common/src/main/java/com/sk89q/warmroast/StackTraceNode.java @@ -0,0 +1,69 @@ +/* + * WarmRoast + * Copyright (C) 2013 Albert Pham <http://www.sk89q.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package com.sk89q.warmroast; + +import com.google.gson.JsonObject; + +/** + * Represents a {@link StackNode node} for a method call. + */ +public class StackTraceNode extends StackNode { + + /** + * Forms the {@link StackNode#getName()} for a {@link StackTraceNode}. + * + * @param className the name of the class + * @param methodName the name of the method + * @return the name + */ + static String formName(String className, String methodName) { + return className + "." + methodName + "()"; + } + + /** The name of the class */ + private final String className; + /** The name of the method */ + private final String methodName; + + public StackTraceNode(String className, String methodName) { + super(formName(className, methodName)); + this.className = className; + this.methodName = methodName; + } + + public String getClassName() { + return className; + } + + public String getMethodName() { + return methodName; + } + + @Override + protected void appendMetadata(JsonObject obj) { + obj.addProperty("className", className); + obj.addProperty("methodName", methodName); + } + + @Override + public int compareTo(StackNode that) { + return Long.compare(that.getTotalTime(), this.getTotalTime()); + } + +} diff --git a/common/src/main/java/com/sk89q/warmroast/ThreadDumper.java b/common/src/main/java/com/sk89q/warmroast/ThreadDumper.java new file mode 100644 index 0000000..6c25daf --- /dev/null +++ b/common/src/main/java/com/sk89q/warmroast/ThreadDumper.java @@ -0,0 +1,72 @@ +/* + * WarmRoast + * Copyright (C) 2013 Albert Pham <http://www.sk89q.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package com.sk89q.warmroast; + +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; + +/** + * Uses the {@link ThreadMXBean} to generate {@link ThreadInfo} instances for the threads being + * sampled. + */ +@FunctionalInterface +public interface ThreadDumper { + + /** + * Generates {@link ThreadInfo} data for the sampled threads. + * + * @param threadBean the thread bean instance to obtain the data from + * @return an array of generated thread info instances + */ + ThreadInfo[] dumpThreads(ThreadMXBean threadBean); + + /** + * Implementation of {@link ThreadDumper} that generates data for all threads. + */ + final class All implements ThreadDumper { + @Override + public ThreadInfo[] dumpThreads(ThreadMXBean threadBean) { + return threadBean.dumpAllThreads(false, false); + } + } + + /** + * Implementation of {@link ThreadDumper} that generates data for a specific set of threads. + */ + final class Specific implements ThreadDumper { + private final long[] ids; + + public Specific(long[] ids) { + this.ids = ids; + } + + public Specific(String name) { + this.ids = Thread.getAllStackTraces().keySet().stream() + .filter(t -> t.getName().equalsIgnoreCase(name)) + .mapToLong(Thread::getId) + .toArray(); + } + + @Override + public ThreadInfo[] dumpThreads(ThreadMXBean threadBean) { + return threadBean.getThreadInfo(this.ids, Integer.MAX_VALUE); + } + } + +} diff --git a/common/src/main/java/me/lucko/spark/common/CommandHandler.java b/common/src/main/java/me/lucko/spark/common/CommandHandler.java new file mode 100644 index 0000000..aa7e47d --- /dev/null +++ b/common/src/main/java/me/lucko/spark/common/CommandHandler.java @@ -0,0 +1,287 @@ +package me.lucko.spark.common; + +import com.google.gson.JsonObject; +import com.sk89q.warmroast.ThreadDumper; +import com.sk89q.warmroast.Sampler; + +import me.lucko.spark.common.http.Bytebin; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Timer; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Abstract command handling class used by all platforms. + * + * @param <T> the sender (e.g. CommandSender) type used by the platform + */ +public abstract class CommandHandler<T> { + + /** The URL of the viewer frontend */ + private static final String VIEWER_URL = "https://sparkprofiler.github.io/?"; + + /** + * The {@link Timer} being used by the {@link #activeSampler}. + */ + private final Timer timer = new Timer("spark-sampling-thread", true); + + /** Guards {@link #activeSampler} */ + private final Object[] activeSamplerMutex = new Object[0]; + /** The WarmRoast instance currently running, if any */ + private Sampler activeSampler = null; + + + // abstract methods implemented by each platform + + protected abstract void sendMessage(T sender, String message); + protected abstract void runAsync(Runnable r); + + private void sendPrefixedMessage(T sender, String message) { + sendMessage(sender, "&8[&fspark&8] &7" + message); + } + + public void handleCommand(T sender, String[] args) { + try { + if (args.length == 0) { + sendInfo(sender); + return; + } + + List<String> arguments = new ArrayList<>(Arrays.asList(args)); + switch (arguments.remove(0).toLowerCase()) { + case "start": + handleStart(sender, arguments); + break; + case "info": + handleInfo(sender); + break; + case "cancel": + handleCancel(sender); + break; + case "stop": + case "upload": + case "paste": + handleStop(sender); + break; + default: + sendInfo(sender); + break; + } + } catch (IllegalArgumentException e) { + sendMessage(sender, "&c" + e.getMessage()); + } + } + + private void sendInfo(T sender) { + sendPrefixedMessage(sender, "&fspark profiler &7v1.0"); + sendMessage(sender, "&b&l> &7/profiler start"); + sendMessage(sender, " &8[&7--timeout&8 <timeout seconds>]"); + sendMessage(sender, " &8[&7--thread&8 <thread name>]"); + sendMessage(sender, " &8[&7--interval&8 <interval millis>]"); + sendMessage(sender, "&b&l> &7/profiler info"); + sendMessage(sender, "&b&l> &7/profiler stop"); + sendMessage(sender, "&b&l> &7/profiler cancel"); + } + + private void handleStart(T sender, List<String> args) { + Map<String, String> arguments = parseArguments(args); + + int timeoutSeconds = parseInt(arguments, "timeout", "d"); + if (timeoutSeconds != -1 && timeoutSeconds <= 10) { + sendPrefixedMessage(sender, "&cThe specified timeout is not long enough for accurate results to be formed."); + return; + } + + if (timeoutSeconds != -1 && timeoutSeconds < 100) { + sendPrefixedMessage(sender, "&7The accuracy of the output will significantly improve when sampling is able to run for longer periods. Consider setting a value of timeout over 1-2 minutes."); + } + + int intervalMillis = parseInt(arguments, "interval", "i"); + if (intervalMillis == -1) { + intervalMillis = 10; + } + + String threadName = arguments.getOrDefault("thread", arguments.getOrDefault("t", null)); + ThreadDumper threadDumper; + if (threadName == null) { + // use the server thread + threadDumper = new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); + } else if (threadName.equals("*")) { + threadDumper = new ThreadDumper.All(); + } else { + threadDumper = new ThreadDumper.Specific(threadName); + } + + Sampler sampler; + synchronized (this.activeSamplerMutex) { + if (this.activeSampler != null) { + sendPrefixedMessage(sender, "&7An active sampler is already running."); + return; + } + + sendPrefixedMessage(sender, "&7Starting a new sampler task..."); + + SamplerBuilder builder = new SamplerBuilder(); + builder.threadDumper(threadDumper); + if (timeoutSeconds != -1) { + builder.completeAfter(timeoutSeconds, TimeUnit.SECONDS); + } + builder.samplingInterval(intervalMillis); + sampler = this.activeSampler = builder.start(timer); + + sendPrefixedMessage(sender, "&bSampling has begun!"); + } + + CompletableFuture<Sampler> future = sampler.getFuture(); + + // send message if profiling fails + future.whenComplete((s, throwable) -> { + if (throwable != null) { + sendPrefixedMessage(sender, "&cSampling operation failed unexpectedly. Error: " + throwable.toString()); + throwable.printStackTrace(); + } + }); + + // set activeSampler to null when complete. + future.whenComplete((s, throwable) -> { + synchronized (this.activeSamplerMutex) { + if (sampler == this.activeSampler) { + this.activeSampler = null; + } + } + }); + + // await the result + if (timeoutSeconds != -1) { + future.thenAcceptAsync(s -> { + sendPrefixedMessage(sender, "&7The active sampling operation has completed! Uploading results..."); + handleUpload(sender, s); + }); + } + } + + private void handleInfo(T sender) { + synchronized (this.activeSamplerMutex) { + if (this.activeSampler == null) { + sendPrefixedMessage(sender, "&7There isn't an active sampling task running."); + } else { + long timeout = this.activeSampler.getEndTime(); + if (timeout == -1) { + sendPrefixedMessage(sender, "&7There is an active sampler currently running, with no defined timeout."); + } else { + long timeoutDiff = (timeout - System.currentTimeMillis()) / 1000L; + sendPrefixedMessage(sender, "&7There is an active sampler currently running, due to timeout in " + timeoutDiff + " seconds."); + } + + long runningTime = (System.currentTimeMillis() - this.activeSampler.getStartTime()) / 1000L; + sendPrefixedMessage(sender, "&7It has been sampling for " + runningTime + " seconds so far."); + } + } + } + + private void handleStop(T sender) { + synchronized (this.activeSamplerMutex) { + if (this.activeSampler == null) { + sendPrefixedMessage(sender, "&7There isn't an active sampling task running."); + } else { + this.activeSampler.cancel(); + sendPrefixedMessage(sender, "&7The active sampling operation has been stopped! Uploading results..."); + handleUpload(sender, this.activeSampler); + this.activeSampler = null; + } + } + } + + private void handleCancel(T sender) { + synchronized (this.activeSamplerMutex) { + if (this.activeSampler == null) { + sendPrefixedMessage(sender, "&7There isn't an active sampling task running."); + } else { + this.activeSampler.cancel(); + this.activeSampler = null; + sendPrefixedMessage(sender, "&bThe active sampling task has been cancelled."); + } + } + } + + private void handleUpload(T sender, Sampler sampler) { + runAsync(() -> { + JsonObject output = sampler.formOutput(); + try { + String pasteId = Bytebin.postContent(output); + sendPrefixedMessage(sender, "&bSampling results can be viewed here: &7" + VIEWER_URL + pasteId); + } catch (IOException e) { + sendPrefixedMessage(sender, "&cAn error occurred whilst uploading the results."); + e.printStackTrace(); + } + }); + } + + private int parseInt(Map<String, String> arguments, String longArg, String shortArg) { + String value = arguments.getOrDefault(longArg, arguments.getOrDefault(shortArg, null)); + if (value != null) { + try { + return Math.abs(Integer.parseInt(value)); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Invalid input for '" + longArg + "' argument. Please specify a number!"); + } + } else { + return -1; // undefined + } + } + + private static final Pattern FLAG_REGEX = Pattern.compile("--(.+)$|-([a-zA-z])$"); + + private static Map<String, String> parseArguments(List<String> args) { + Map<String, String> arguments = new HashMap<>(); + + String flag = null; + List<String> value = null; + + for (int i = 0; i < args.size(); i++) { + String arg = args.get(i); + + Matcher matcher = FLAG_REGEX.matcher(arg); + boolean matches = matcher.matches(); + + if (flag == null || matches) { + if (!matches) { + throw new IllegalArgumentException("Expected flag at position " + i + " but got '" + arg + "' instead!"); + } + + String match = matcher.group(1); + if (match == null) { + match = matcher.group(2); + } + + // store existing value, if present + if (flag != null) { + arguments.put(flag, value.stream().collect(Collectors.joining(" "))); + } + + flag = match.toLowerCase(); + value = new ArrayList<>(); + } else { + // part of a value + value.add(arg); + } + } + + // store remaining value, if present + if (flag != null) { + arguments.put(flag, value.stream().collect(Collectors.joining(" "))); + } + + return arguments; + } + +} diff --git a/common/src/main/java/me/lucko/spark/common/SamplerBuilder.java b/common/src/main/java/me/lucko/spark/common/SamplerBuilder.java new file mode 100644 index 0000000..e6c0cf8 --- /dev/null +++ b/common/src/main/java/me/lucko/spark/common/SamplerBuilder.java @@ -0,0 +1,44 @@ +package me.lucko.spark.common; + +import com.google.common.base.Preconditions; +import com.sk89q.warmroast.ThreadDumper; +import com.sk89q.warmroast.Sampler; + +import java.util.Timer; +import java.util.concurrent.TimeUnit; + +/** + * Builds {@link Sampler} instances. + */ +public class SamplerBuilder { + + private int samplingInterval = 10; + private long timeout = -1; + private ThreadDumper threadDumper = new ThreadDumper.All(); + + public SamplerBuilder() { + } + + public SamplerBuilder samplingInterval(int samplingInterval) { + this.samplingInterval = samplingInterval; + return this; + } + + public SamplerBuilder completeAfter(long timeout, TimeUnit unit) { + Preconditions.checkArgument(timeout > 0, "time > 0"); + this.timeout = System.currentTimeMillis() + unit.toMillis(timeout); + return this; + } + + public SamplerBuilder threadDumper(ThreadDumper threadDumper) { + this.threadDumper = threadDumper; + return this; + } + + public Sampler start(Timer timer) { + Sampler sampler = new Sampler(samplingInterval, threadDumper, timeout); + sampler.start(timer); + return sampler; + } + +} diff --git a/common/src/main/java/me/lucko/spark/common/http/Bytebin.java b/common/src/main/java/me/lucko/spark/common/http/Bytebin.java new file mode 100644 index 0000000..7d95838 --- /dev/null +++ b/common/src/main/java/me/lucko/spark/common/http/Bytebin.java @@ -0,0 +1,69 @@ +package me.lucko.spark.common.http; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +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.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.util.zip.GZIPOutputStream; + +/** + * Utility for uploading JSON data to bytebin. + */ +public final class Bytebin { + + /** Shared GSON instance */ + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); + /** 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 postContent(JsonElement content) throws IOException { + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(byteOut), StandardCharsets.UTF_8)) { + GSON.toJson(content, writer); + } catch (IOException e) { + throw new RuntimeException(e); + } + + RequestBody body = RequestBody.create(JSON_TYPE, byteOut.toByteArray()); + + 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/common/src/main/java/me/lucko/spark/common/http/HttpClient.java b/common/src/main/java/me/lucko/spark/common/http/HttpClient.java new file mode 100644 index 0000000..61db597 --- /dev/null +++ b/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 |