From 1644a4b07d1437d2174d762c689c178742abfd95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 20 Dec 2022 15:11:44 +0100 Subject: [PATCH] Make serialization util class --- .../commands/base/UrbanDictionary.java | 50 ++++--------------- .../message/UrbanDictionaryCommand.java | 2 +- .../slash/UrbanDictionaryCommand.java | 2 +- .../hidekobot/util/SerializationUtil.java | 38 ++++++++++++++ 4 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/util/SerializationUtil.java diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java index 02cc59e..e69eefe 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java @@ -9,15 +9,14 @@ import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.ItemComponent; import net.dv8tion.jda.api.interactions.components.buttons.Button; -import org.apache.commons.lang3.SerializationException; import org.apache.commons.text.StringEscapeUtils; import org.apache.commons.text.WordUtils; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.datasources.DatabaseSource; +import wtf.beatrice.hidekobot.util.SerializationUtil; -import java.io.*; import java.util.*; public class UrbanDictionary @@ -88,40 +87,12 @@ public class UrbanDictionary return embedBuilder.build(); } - public static String serialize(List dataList) { - try (ByteArrayOutputStream bo = new ByteArrayOutputStream(); - ObjectOutputStream so = new ObjectOutputStream(bo)) { - so.writeObject(dataList); - so.flush(); - return Base64.getEncoder().encodeToString(bo.toByteArray()); - } - catch (IOException ignored) {} - return null; - } - - public static LinkedList deserialize(String dataStr) { - - byte[] b = Base64.getDecoder().decode(dataStr); - ByteArrayInputStream bi = new ByteArrayInputStream(b); - ObjectInputStream si; - try { - si = new ObjectInputStream(bi); - return LinkedList.class.cast(si.readObject()); - } - catch (IOException | ClassNotFoundException e) { - throw new SerializationException("Error during deserialization", e); - } - } - - public static String getNoTermFoundError() + public static String getTermNotFoundError() { return "\uD83D\uDE22 I couldn't find that term!"; } - - - public static void track(Message message, User user, UrbanSearch search, String sanitizedTerm) { Cache.getDatabaseSource().queueDisabling(message); @@ -134,7 +105,6 @@ public class UrbanDictionary sanitizedTerm); } - public static void changePage(ButtonInteractionEvent event, ChangeType changeType) { String messageId = event.getMessageId(); @@ -224,10 +194,10 @@ public class UrbanDictionary this.serializedContributors = serializedContributors; this.serializedDates = serializedDates; - this.plaintextMeanings = UrbanDictionary.deserialize(serializedMeanings); - this.plaintextExamples = UrbanDictionary.deserialize(serializedExamples); - this.contributorsNames = UrbanDictionary.deserialize(serializedContributors); - this.submissionDates = UrbanDictionary.deserialize(serializedDates); + this.plaintextMeanings = SerializationUtil.deserializeBase64(serializedMeanings); + this.plaintextExamples = SerializationUtil.deserializeBase64(serializedExamples); + this.contributorsNames = SerializationUtil.deserializeBase64(serializedContributors); + this.submissionDates = SerializationUtil.deserializeBase64(serializedDates); this.pages = submissionDates.size(); } @@ -297,10 +267,10 @@ public class UrbanDictionary } } - serializedMeanings = serialize(plaintextMeanings); - serializedExamples = serialize(plaintextExamples); - serializedContributors = serialize(contributorsNames); - serializedDates = serialize(submissionDates); + serializedMeanings = SerializationUtil.serializeBase64(plaintextMeanings); + serializedExamples = SerializationUtil.serializeBase64(plaintextExamples); + serializedContributors = SerializationUtil.serializeBase64(contributorsNames); + serializedDates = SerializationUtil.serializeBase64(submissionDates); pages = submissionDates.size(); } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java index df73528..f80a00b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java @@ -64,7 +64,7 @@ public class UrbanDictionaryCommand implements MessageCommand try { doc = Jsoup.connect(url).get(); } catch (IOException e) { - event.getMessage().reply(UrbanDictionary.getNoTermFoundError()).queue(); + event.getMessage().reply(UrbanDictionary.getTermNotFoundError()).queue(); return; } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/UrbanDictionaryCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/UrbanDictionaryCommand.java index 00d29b4..84727d6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/UrbanDictionaryCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/UrbanDictionaryCommand.java @@ -57,7 +57,7 @@ public class UrbanDictionaryCommand extends SlashCommandImpl try { doc = Jsoup.connect(url).get(); } catch (IOException e) { - event.reply(UrbanDictionary.getNoTermFoundError()) + event.reply(UrbanDictionary.getTermNotFoundError()) .setEphemeral(true) .queue(); return; diff --git a/src/main/java/wtf/beatrice/hidekobot/util/SerializationUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/SerializationUtil.java new file mode 100644 index 0000000..1fd71bb --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/util/SerializationUtil.java @@ -0,0 +1,38 @@ +package wtf.beatrice.hidekobot.util; + +import org.apache.commons.lang3.SerializationException; + +import java.io.*; +import java.util.Base64; +import java.util.LinkedList; +import java.util.List; + +public class SerializationUtil +{ + + public static String serializeBase64(List dataList) { + + try (ByteArrayOutputStream bo = new ByteArrayOutputStream(); + ObjectOutputStream so = new ObjectOutputStream(bo)) { + so.writeObject(dataList); + so.flush(); + return Base64.getEncoder().encodeToString(bo.toByteArray()); + } + catch (IOException ignored) {} + return null; + } + + public static LinkedList deserializeBase64(String dataStr) { + + byte[] b = Base64.getDecoder().decode(dataStr); + ByteArrayInputStream bi = new ByteArrayInputStream(b); + ObjectInputStream si; + try { + si = new ObjectInputStream(bi); + return LinkedList.class.cast(si.readObject()); + } + catch (IOException | ClassNotFoundException e) { + throw new SerializationException("Error during deserialization", e); + } + } +}