From da976ff5416bf0994e5197f31b7ce5e505082eb5 Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 23 Jun 2022 15:30:26 +0200 Subject: [PATCH] :hammer: Refracto libs --- build.gradle | 25 +++++-------- .../java/net/Broken/SlashCommands/Cat.java | 10 +++-- .../Tools/Command/NumberedSlashCommand.java | 4 +- .../Broken/Tools/Random/Data/RandomData.java | 18 +++++++++ .../Broken/Tools/{ => Random}/TrueRandom.java | 37 ++++++++----------- 5 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 src/main/java/net/Broken/Tools/Random/Data/RandomData.java rename src/main/java/net/Broken/Tools/{ => Random}/TrueRandom.java (59%) diff --git a/build.gradle b/build.gradle index 1d234e6..a9544dc 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,7 @@ plugins { id 'java' id 'groovy' id 'org.liquibase.gradle' version '2.0.4' + id "nebula.lint" version "16.9.0" } @@ -35,41 +36,33 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-data-jpa") - implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("org.springframework.boot:spring-boot-starter-log4j2") implementation("org.springframework.boot:spring-boot-starter-oauth2-client") - implementation("org.springdoc:springdoc-openapi-ui:1.6.8") - implementation("org.springdoc:springdoc-openapi-security:1.6.8") + implementation('org.springdoc:springdoc-openapi-ui:1.6.9') + implementation('org.springdoc:springdoc-openapi-security:1.6.9') implementation('org.liquibase:liquibase-core') implementation('io.jsonwebtoken:jjwt-api:0.11.5') implementation('io.jsonwebtoken:jjwt-impl:0.11.5') - implementation('io.jsonwebtoken:jjwt-gson:0.11.5') - - implementation 'org.codehaus.groovy:groovy-all:3.0.8' + implementation('io.jsonwebtoken:jjwt-jackson:0.11.5') implementation 'com.sedmelluq:lavaplayer:1.3.77' implementation 'net.dv8tion:JDA:4.4.0_350' - implementation group: 'org.json', name: 'json', version: '20210307' -// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) implementation(platform("org.apache.logging.log4j:log4j-bom:2.17.1")) implementation group: 'org.hibernate', name: 'hibernate-validator', version: '7.0.4.Final' // Use MySQL Connector-J - implementation 'mysql:mysql-connector-java' - implementation 'org.reflections:reflections:0.9.12' + implementation 'mysql:mysql-connector-java:8.0.29' + implementation 'org.reflections:reflections:0.10.2' implementation 'org.apache.commons:commons-lang3:3.12.0' - implementation 'com.google.api-client:google-api-client:1.31.5' - implementation 'com.google.apis:google-api-services-youtube:v3-rev20210410-1.31.0' - implementation group: 'org.jsoup', name: 'jsoup', version: '1.13.1' - liquibaseRuntime 'org.liquibase:liquibase-core:4.2.2' - liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.1.1' - liquibaseRuntime 'mysql:mysql-connector-java:5.1.34' + liquibaseRuntime 'org.liquibase:liquibase-core:4.8.0' + liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:3.0.2' + liquibaseRuntime 'mysql:mysql-connector-java:8.0.29' liquibaseRuntime group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1' liquibaseRuntime group: 'org.liquibase.ext', name: 'liquibase-hibernate5', version: '4.10.0' liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa' diff --git a/src/main/java/net/Broken/SlashCommands/Cat.java b/src/main/java/net/Broken/SlashCommands/Cat.java index d171cee..b9f75a2 100644 --- a/src/main/java/net/Broken/SlashCommands/Cat.java +++ b/src/main/java/net/Broken/SlashCommands/Cat.java @@ -1,6 +1,8 @@ package net.Broken.SlashCommands; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import net.Broken.SlashCommand; import net.Broken.Tools.EmbedMessageUtils; import net.dv8tion.jda.api.MessageBuilder; @@ -9,7 +11,6 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; @@ -17,6 +18,7 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.List; /** @@ -38,9 +40,11 @@ public class Cat implements SlashCommand { a.append(inputLine); in.close(); - JSONObject json = new JSONObject(a.toString()); + TypeReference> typeRef = new TypeReference<>() {}; + ObjectMapper mapper = new ObjectMapper(); + HashMap json = mapper.readValue(a.toString(), typeRef); - event.reply(json.getString("file")).queue(); + event.reply(json.get("file")).queue(); } catch (IOException e) { logger.catching(e); diff --git a/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java b/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java index 385a1d7..1fb4870 100644 --- a/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java +++ b/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java @@ -4,7 +4,7 @@ import net.Broken.SlashCommand; import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.FindContentOnWebPage; import net.Broken.Tools.LimitChecker; -import net.Broken.Tools.TrueRandom; +import net.Broken.Tools.Random.TrueRandom; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.interactions.commands.build.OptionData; @@ -75,7 +75,7 @@ public abstract class NumberedSlashCommand implements SlashCommand { private void fillRandomQueue() throws IOException { TrueRandom trueRandom = TrueRandom.getINSTANCE(); - ArrayList numbers = trueRandom.getNumbers(minNumber, maxNumber); + List numbers = trueRandom.getNumbers(minNumber, maxNumber); randomQueue.addAll(numbers); diff --git a/src/main/java/net/Broken/Tools/Random/Data/RandomData.java b/src/main/java/net/Broken/Tools/Random/Data/RandomData.java new file mode 100644 index 0000000..02f9df4 --- /dev/null +++ b/src/main/java/net/Broken/Tools/Random/Data/RandomData.java @@ -0,0 +1,18 @@ +package net.Broken.Tools.Random.Data; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.List; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public record RandomData(String jsonrpc, String method, int id, Params params, Error error, Result result) { + public record Params(String apiKey, int n, int min, int max, boolean replacement) { + } + + public record Error(long code, String message, Object data){}; + public record Result(Random random, String bitsUsed, String bitsLeft, String requestsLeft, String advisoryDelay){ + public record Random(List data, String completionTime){}; + } +} + + diff --git a/src/main/java/net/Broken/Tools/TrueRandom.java b/src/main/java/net/Broken/Tools/Random/TrueRandom.java similarity index 59% rename from src/main/java/net/Broken/Tools/TrueRandom.java rename to src/main/java/net/Broken/Tools/Random/TrueRandom.java index 91c41df..02b379e 100644 --- a/src/main/java/net/Broken/Tools/TrueRandom.java +++ b/src/main/java/net/Broken/Tools/Random/TrueRandom.java @@ -1,7 +1,9 @@ -package net.Broken.Tools; +package net.Broken.Tools.Random; +import com.fasterxml.jackson.databind.ObjectMapper; import net.Broken.BotConfigLoader; import net.Broken.SpringContext; +import net.Broken.Tools.Random.Data.RandomData; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; @@ -11,12 +13,10 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.List; public class TrueRandom { @@ -32,13 +32,15 @@ public class TrueRandom { return INSTANCE; } - public ArrayList getNumbers(int min, int max) throws IOException { + public List getNumbers(int min, int max) throws IOException { // TODO Migrate to native http client HttpClient httpClient = HttpClientBuilder.create().build(); - String postVal = "{\"jsonrpc\":\"2.0\",\"method\":\"generateIntegers\",\"params\":{\"apiKey\":\"" + apiKey + "\",\"n\":50,\"min\":" + min + ",\"max\":" + max + ",\"replacement\":" + (((max - min) >= 50) ? "false" : "true") + "},\"id\":41}"; - StringEntity entity = new StringEntity(postVal, ContentType.APPLICATION_JSON); + RandomData postData = new RandomData("2.0", "generateIntegers", 41, new RandomData.Params(apiKey, 50, min, max, (max - min) < 50), null, null); + ObjectMapper mapper = new ObjectMapper(); + + StringEntity entity = new StringEntity(mapper.writeValueAsString(postData), ContentType.APPLICATION_JSON); String url = "https://api.random.org/json-rpc/2/invoke"; HttpPost request = new HttpPost(url); request.setEntity(entity); @@ -56,28 +58,21 @@ public class TrueRandom { InputStream responseIS = response.getEntity().getContent(); String content = IOUtils.toString(responseIS, StandardCharsets.UTF_8); - logger.trace(content); - JSONObject json = new JSONObject(content); - if (json.keySet().contains("error")) { + RandomData responseData = mapper.readValue(content, RandomData.class); + + if (responseData.error() != null) { logger.error("Request fail!"); - logger.error("Request : " + postVal); - logger.error("Response : " + content); + logger.error("Response : " + responseData.error().message()); throw new IOException(); } - logger.debug("Request left: " + json.getJSONObject("result").getInt("requestsLeft")); - logger.debug("Bits left: " + json.getJSONObject("result").getInt("bitsLeft")); - logger.debug("Numbers: " + json.getJSONObject("result").getJSONObject("random").getJSONArray("data")); + logger.debug("Request left: " + responseData.result().requestsLeft()); + logger.debug("Bits left: " + responseData.result().bitsLeft()); + logger.debug("Numbers: " + responseData.result().random().data()); - List numbers = json.getJSONObject("result").getJSONObject("random").getJSONArray("data").toList(); - - ArrayList converted = new ArrayList<>(); - for (Object nbr : numbers) { - converted.add((Integer) nbr); - } - return converted; + return responseData.result().random().data(); }