diff --git a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java index d8b233f12..bf269a35b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java @@ -47,6 +47,7 @@ public class PlanCommand extends TreeCommand { commands.add(new ReloadCommand(plugin)); commands.add(new ManageCommand(plugin)); commands.add(new StatusCommand<>(plugin, Permissions.MANAGE.getPermission())); + if (plugin.getUiServer().isEnabled()) { commands.add(new ListCommand()); RegisterCommand registerCommand = new RegisterCommand(plugin); diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpLog.java b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpLog.java index 0b2876b77..dd1098987 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpLog.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpLog.java @@ -1,25 +1,12 @@ package main.java.com.djrapitops.plan.utilities.file.dump; -import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableList; -import main.java.com.djrapitops.plan.Log; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import javax.net.ssl.HttpsURLConnection; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author Fuzzlemann - * @since 3.7.0 + * @since 3.6.2 */ public class DumpLog { @@ -99,66 +86,7 @@ public class DumpLog { * @return The link to the Dump Log */ String upload() { - List parts = ImmutableList.copyOf(split()).reverse(); - - String lastLink = null; - for (String part : parts) { - if (lastLink != null) { - part += "\n" + lastLink; - } - - lastLink = upload(part); - } - - return lastLink; - } - - /** - * Uploads the content to Hastebin using HTTPS and POST - * - * @param content The content - * @return The link to the content - */ - private String upload(String content) { - HttpsURLConnection connection = null; - try { - URL url = new URL("https://hastebin.com/documents"); - connection = (HttpsURLConnection) url.openConnection(); - - connection.setRequestProperty("Content-length", String.valueOf(content.length())); - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - connection.setRequestProperty("User-Agent", "Mozilla/4.0"); - connection.setRequestMethod("POST"); - connection.setDoInput(true); - connection.setDoOutput(true); - - DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); - wr.writeBytes(this.toString()); - wr.flush(); - wr.close(); - - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - JSONParser parser = new JSONParser(); - JSONObject json = (JSONObject) parser.parse(reader.readLine()); - - return "https://hastebin.com/" + json.get("key"); - } catch (IOException | ParseException e) { - Log.toLog("DumpLog.upload", e); - return "Error"; - } finally { - if (connection != null) { - connection.disconnect(); - } - } - } - - /** - * Splits the content of the DumpLog into parts - * - * @return The splitted content - */ - private Iterable split() { - return Splitter.fixedLength(390000).split(this.toString()); + return Hastebin.safeUpload(this.toString()); } @Override diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpUtils.java index 91f378938..23e1fd85f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/DumpUtils.java @@ -23,10 +23,13 @@ import java.util.stream.Collectors; /** * @author Fuzzlemann - * @since 3.7.0 + * @since 3.6.2 */ public class DumpUtils { + /** + * Constructor used to hide the public constructor + */ private DumpUtils() { throw new IllegalStateException("Utility Class"); } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/Hastebin.java b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/Hastebin.java new file mode 100644 index 000000000..4ee8a8a54 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/Hastebin.java @@ -0,0 +1,127 @@ +package main.java.com.djrapitops.plan.utilities.file.dump; + +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; +import main.java.com.djrapitops.plan.Log; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import javax.net.ssl.HttpsURLConnection; +import java.io.*; +import java.net.URL; +import java.util.List; + +/** + * @author Fuzzlemann + * @since 3.6.4 + */ +class Hastebin { + + /** + * Constructor used to hide the public constructor + */ + private Hastebin() { + throw new IllegalStateException("Utility Class"); + } + + /** + * Uploads the content safely to Hastebin. + * Longer than allowed content is being uploaded too. + * + * @return The link to the Dump Log + * @implNote Splits the content into parts of 390.000 chars each, + * uploads the parts in reverse order and adds the last link (if present) + * at each end of the following part, that's why the redundancy of 10.000 chars exists. + * @see #split(String) + */ + static String safeUpload(String content) { + List parts = ImmutableList.copyOf(split(content)).reverse(); + + String lastLink = null; + try { + for (String part : parts) { + if (lastLink != null) { + part += "\n" + lastLink; + } + + lastLink = upload(part); + } + } catch (IOException | ParseException e) { + Log.toLog("DumpLog.upload", e); + } + + return lastLink; + } + + /** + * Uploads the content to Hastebin using HTTPS and POST + * + * @param content The content + * @return The link to the content + */ + private static String upload(String content) throws IOException, ParseException { + HttpsURLConnection connection = null; + try { + URL url = new URL("https://hastebin.com/documents"); + connection = (HttpsURLConnection) url.openConnection(); + + connection.setRequestProperty("Content-length", String.valueOf(content.length())); + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + connection.setRequestProperty("User-Agent", "Mozilla/4.0"); + connection.setRequestMethod("POST"); + connection.setDoInput(true); + connection.setDoOutput(true); + + writeData(connection.getOutputStream(), content); + + return getHastebinLink(connection.getInputStream()); + } finally { + if (connection != null) { + connection.disconnect(); + } + } + } + + /** + * Writes the data to the {@link OutputStream} + * + * @param outputStream The OutputStream that the data should be written to + * @throws IOException when an error at the writing of the data happens + */ + private static void writeData(OutputStream outputStream, String content) throws IOException { + try (DataOutputStream wr = new DataOutputStream(outputStream)) { + wr.writeBytes(content); + } + } + + /** + * Gets the Hastebin Link from the {@link InputStream} + * + * @param inputStream The InputStream in which the Hastebin Key is included (encoded in JSON) + * @return The full Hastebin Link ({@code https://hastebin.com/ + key}) + * @throws IOException when an error at the reading of the InputStream happens + * @throws ParseException when an error at the parsing of the line that was read happens + */ + private static String getHastebinLink(InputStream inputStream) throws IOException, ParseException { + String key; + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + JSONParser parser = new JSONParser(); + JSONObject json = (JSONObject) parser.parse(reader.readLine()); + + key = (String) json.get("key"); + } + + return "https://hastebin.com/" + key; + } + + /** + * Splits the content in parts of 390.000 chars each + * + * @return The content that was splitted + */ + private static Iterable split(String content) { + return Splitter.fixedLength(390000).split(content); + } +}