From 1a5ae72d6e275df8f88e8f4539a9c613b1a4f66a Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 29 Apr 2020 11:52:03 -0700 Subject: [PATCH] Added urlReader() method. --- .../java/net/thauvin/erik/mobibot/Utils.java | 22 +++++++++++++++ .../erik/mobibot/modules/GoogleSearch.java | 27 +++++-------------- .../thauvin/erik/mobibot/modules/Joke.java | 24 +++-------------- .../net/thauvin/erik/mobibot/UtilsTest.java | 16 ++++++++--- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/main/java/net/thauvin/erik/mobibot/Utils.java b/src/main/java/net/thauvin/erik/mobibot/Utils.java index e4f582a..3d0632a 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Utils.java +++ b/src/main/java/net/thauvin/erik/mobibot/Utils.java @@ -32,16 +32,23 @@ package net.thauvin.erik.mobibot; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.apache.commons.lang3.StringUtils; import org.jibble.pircbot.Colors; import org.jsoup.Jsoup; +import java.io.BufferedReader; import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; /** * Miscellaneous utilities class. @@ -332,6 +339,21 @@ public final class Utils { return info.toString(); } + /** + * Reads contents of a URL. + * + * @param url The URL to read. + * @return The URL contents. + * @throws IOException If an IO error occurs. + */ + @SuppressFBWarnings("SECSSSRFUC") + public static String urlReader(final URL url) throws IOException { + try (final BufferedReader reader = new BufferedReader( + new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) { + return reader.lines().collect(Collectors.joining(System.lineSeparator())); + } + } + /** * Returns the specified date formatted as yyyy-MM-dd HH:mm. * diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java index bc05a1f..a36789f 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java @@ -42,11 +42,8 @@ import org.jibble.pircbot.Colors; import org.json.JSONArray; import org.json.JSONObject; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.net.URL; -import java.net.URLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -111,25 +108,15 @@ public final class GoogleSearch extends ThreadedModule { + "&q=" + q + "&filter=1&num=5&alt=json"); - final URLConnection conn = url.openConnection(); - final StringBuilder sb = new StringBuilder(); - try (final BufferedReader reader = - new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { - String line; - while ((line = reader.readLine()) != null) { - sb.append(line); - } + final JSONObject json = new JSONObject(Utils.urlReader(url)); + final JSONArray ja = json.getJSONArray("items"); - final JSONObject json = new JSONObject(sb.toString()); - final JSONArray ja = json.getJSONArray("items"); - - for (int i = 0; i < ja.length(); i++) { - final JSONObject j = ja.getJSONObject(i); - results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title")))); - results.add( - new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN)); - } + for (int i = 0; i < ja.length(); i++) { + final JSONObject j = ja.getJSONObject(i); + results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title")))); + results.add( + new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN)); } } catch (IOException e) { throw new ModuleException("searchGoogle(" + query + ')', "An error has occurred searching Google.", e); diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java b/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java index 00d1076..5c1d858 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java @@ -38,11 +38,7 @@ import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.PublicMessage; import org.json.JSONObject; -import java.io.BufferedReader; -import java.io.InputStreamReader; import java.net.URL; -import java.net.URLConnection; -import java.nio.charset.StandardCharsets; /** * The Joke module. @@ -79,22 +75,10 @@ public final class Joke extends ThreadedModule { static Message randomJoke() throws ModuleException { try { final URL url = new URL(JOKE_URL); - final URLConnection conn = url.openConnection(); - - final StringBuilder sb = new StringBuilder(); - try (final BufferedReader reader = - new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { - String line; - while ((line = reader.readLine()) != null) { - sb.append(line); - } - - final JSONObject json = new JSONObject(sb.toString()); - - return new PublicMessage( - json.getJSONObject("value").get("joke").toString().replace("\\'", "'") - .replace("\\\"", "\"")); - } + final JSONObject json = new JSONObject(Utils.urlReader(url)); + return new PublicMessage( + json.getJSONObject("value").get("joke").toString().replace("\\'", "'") + .replace("\\\"", "\"")); } catch (Exception e) { throw new ModuleException("randomJoke()", "An error has occurred retrieving a random joke.", e); } diff --git a/src/test/java/net/thauvin/erik/mobibot/UtilsTest.java b/src/test/java/net/thauvin/erik/mobibot/UtilsTest.java index df2ff57..b754fbf 100644 --- a/src/test/java/net/thauvin/erik/mobibot/UtilsTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/UtilsTest.java @@ -38,6 +38,8 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.io.File; +import java.io.IOException; +import java.net.URL; import java.time.LocalDateTime; import java.util.Calendar; @@ -52,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class UtilsTest { private static final String ASCII = - " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; + " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; private final Calendar cal = Calendar.getInstance(); private final LocalDateTime localDateTime = LocalDateTime.of(1952, 2, 17, 12, 30, 0); @@ -71,7 +73,7 @@ public class UtilsTest { @Test public void testColorize() { assertThat(Utils.colorize(ASCII, Colors.REVERSE)).as("colorize(reverse)").isEqualTo( - Colors.REVERSE + ASCII + Colors.REVERSE); + Colors.REVERSE + ASCII + Colors.REVERSE); assertThat(Utils.colorize(ASCII, Colors.RED)).as("colorize(red)").isEqualTo(Colors.RED + ASCII + Colors.NORMAL); assertThat(Utils.colorize(null, Colors.RED)).as("colorize(null)").isEqualTo(Colors.NORMAL); } @@ -85,7 +87,7 @@ public class UtilsTest { public void testEnsureDir() { assertThat(Utils.ensureDir("dir", false)).as("ensureDir(dir, false)").isEqualTo("dir" + File.separatorChar); assertThat(Utils.ensureDir("https://erik.thauvin.net", true)).as("ensureDir(erik.thauvin.net, true)").isEqualTo( - "https://erik.thauvin.net/"); + "https://erik.thauvin.net/"); } @Test @@ -136,7 +138,7 @@ public class UtilsTest { @Test public void testUnescapeXml() { assertThat(Utils.unescapeXml("<a name="test & ''">")).isEqualTo( - ""); + ""); } @Test @@ -144,6 +146,12 @@ public class UtilsTest { assertThat("17 years 2 months 2 weeks 1 day 6 hours 45 minutes").isEqualTo(Utils.uptime(547800300076L)); } + @Test + public void testUrlReader() throws IOException { + assertThat(Utils.urlReader(new URL("https://postman-echo.com/status/200"))).as("urlReader()").isEqualTo( + "{\"status\":200}"); + } + @Test public void testUtcDateTime() { assertThat(Utils.utcDateTime(cal.getTime())).as("utcDateTime(date)").isEqualTo("1952-02-17 12:30");