|
1 | 1 | package xyz.theprogramsrc.supercoreapi.global.utils;
|
2 | 2 |
|
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
3 | 5 | import org.apache.commons.io.FileUtils;
|
4 | 6 | import org.bukkit.Bukkit;
|
5 | 7 | import org.bukkit.plugin.Plugin;
|
6 | 8 |
|
7 | 9 | import java.io.*;
|
| 10 | +import java.net.HttpURLConnection; |
8 | 11 | import java.net.URL;
|
9 | 12 | import java.net.URLConnection;
|
10 | 13 | import java.net.UnknownHostException;
|
@@ -290,7 +293,7 @@ public static String readWithInputStream(String url) {
|
290 | 293 | try{
|
291 | 294 | URL javaURL = new URL(url);
|
292 | 295 | URLConnection connection = javaURL.openConnection();
|
293 |
| - connection.setRequestProperty("User-Agent", "Mozilla/5.0"); |
| 296 | + connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); |
294 | 297 | return new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining());
|
295 | 298 | }catch (IOException ex){
|
296 | 299 | ex.printStackTrace();
|
@@ -623,7 +626,82 @@ public static List<String> ct(List<String> list){
|
623 | 626 | * and {@code false} otherwise
|
624 | 627 | */
|
625 | 628 | public static boolean equals(Object a, Object b) {
|
626 |
| - return (a == b) || (a != null && a.equals(b)); |
| 629 | + return Objects.equals(a, b); |
| 630 | + } |
| 631 | + |
| 632 | + /** |
| 633 | + * Returns {@code true} if the input is a valid json |
| 634 | + * @return {@code true} if the input is a valid json, otherwise {@code false} |
| 635 | + */ |
| 636 | + public static boolean isJSONEncoded(String input){ |
| 637 | + try{ |
| 638 | + new JsonParser().parse(input); |
| 639 | + return true; |
| 640 | + }catch (Exception e){ |
| 641 | + return false; |
| 642 | + } |
| 643 | + } |
| 644 | + |
| 645 | + /** |
| 646 | + * Create a new post request |
| 647 | + * @param url the url |
| 648 | + * @param contentData the data to send |
| 649 | + * @return the url response output |
| 650 | + */ |
| 651 | + public static String postRequest(String url, String contentData) throws IOException{ |
| 652 | + URL javaURL = new URL(url); |
| 653 | + HttpURLConnection connection = ((HttpURLConnection)javaURL.openConnection()); |
| 654 | + connection.setRequestMethod("POST"); |
| 655 | + connection.setDoOutput(true); |
| 656 | + connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); |
| 657 | + connection.addRequestProperty("Content-Type", "text/plain; charset=UTF-8"); |
| 658 | + connection.addRequestProperty("Content-Length", contentData.length()+""); |
| 659 | + OutputStream out = connection.getOutputStream(); |
| 660 | + out.write(contentData.getBytes()); |
| 661 | + connection.connect(); |
| 662 | + InputStream in = connection.getInputStream(); |
| 663 | + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
| 664 | + String result = reader.lines().collect(Collectors.joining()); |
| 665 | + connection.disconnect(); |
| 666 | + return result; |
| 667 | + } |
| 668 | + |
| 669 | + /** |
| 670 | + * Upload a new paste to <a href="https://paste.theprogramsrc.xyz/">https://paste.theprogramsrc.xyz/</a> |
| 671 | + * |
| 672 | + * @param body the body of the paste |
| 673 | + * @return the paste key |
| 674 | + */ |
| 675 | + public static String uploadPaste(String body){ |
| 676 | + String url = "https://paste.theprogramsrc.xyz/documents"; |
| 677 | + try{ |
| 678 | + String post = postRequest(url, body); |
| 679 | + if(post != null){ |
| 680 | + if(isJSONEncoded(post)){ |
| 681 | + JsonObject json = new JsonParser().parse(post).getAsJsonObject(); |
| 682 | + return json.get("key").getAsString(); |
| 683 | + } |
| 684 | + } |
| 685 | + }catch (IOException e){ |
| 686 | + e.printStackTrace(); |
| 687 | + } |
| 688 | + |
| 689 | + return null; |
| 690 | + } |
| 691 | + |
| 692 | + /** |
| 693 | + * Build an {@link Exception exception} into a string |
| 694 | + * @param e the exception |
| 695 | + * @return the stack trace as string |
| 696 | + */ |
| 697 | + public static String exceptionToString(Exception e){ |
| 698 | + StringBuilder builder = new StringBuilder(); |
| 699 | + builder.append(e.getClass().getName()).append(": ").append(e.getMessage() != null ? e.getMessage() : "null").append("\n"); |
| 700 | + for (StackTraceElement ste : e.getStackTrace()) { |
| 701 | + builder.append("\tat ").append(ste.getClassName()).append(".").append(ste.getMethodName()).append("(").append(ste.getFileName()).append(":").append(ste.getLineNumber()).append(")").append("\n"); |
| 702 | + } |
| 703 | + |
| 704 | + return builder.toString(); |
627 | 705 | }
|
628 | 706 | }
|
629 | 707 |
|
0 commit comments