Added urlReader() method.

This commit is contained in:
Erik C. Thauvin 2020-04-29 11:52:03 -07:00
parent 6787b0dcbe
commit 1a5ae72d6e
4 changed files with 45 additions and 44 deletions

View file

@ -32,16 +32,23 @@
package net.thauvin.erik.mobibot; package net.thauvin.erik.mobibot;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jibble.pircbot.Colors; import org.jibble.pircbot.Colors;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import java.io.BufferedReader;
import java.io.File; 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.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/** /**
* Miscellaneous utilities class. * Miscellaneous utilities class.
@ -332,6 +339,21 @@ public final class Utils {
return info.toString(); 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 <code>yyyy-MM-dd HH:mm</code>. * Returns the specified date formatted as <code>yyyy-MM-dd HH:mm</code>.
* *

View file

@ -42,11 +42,8 @@ import org.jibble.pircbot.Colors;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
@ -111,17 +108,8 @@ public final class GoogleSearch extends ThreadedModule {
+ "&q=" + "&q="
+ q + q
+ "&filter=1&num=5&alt=json"); + "&filter=1&num=5&alt=json");
final URLConnection conn = url.openConnection();
final StringBuilder sb = new StringBuilder(); final JSONObject json = new JSONObject(Utils.urlReader(url));
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());
final JSONArray ja = json.getJSONArray("items"); final JSONArray ja = json.getJSONArray("items");
for (int i = 0; i < ja.length(); i++) { for (int i = 0; i < ja.length(); i++) {
@ -130,7 +118,6 @@ public final class GoogleSearch extends ThreadedModule {
results.add( results.add(
new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN)); new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN));
} }
}
} catch (IOException e) { } catch (IOException e) {
throw new ModuleException("searchGoogle(" + query + ')', "An error has occurred searching Google.", e); throw new ModuleException("searchGoogle(" + query + ')', "An error has occurred searching Google.", e);
} }

View file

@ -38,11 +38,7 @@ import net.thauvin.erik.mobibot.msg.Message;
import net.thauvin.erik.mobibot.msg.PublicMessage; import net.thauvin.erik.mobibot.msg.PublicMessage;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
/** /**
* The Joke module. * The Joke module.
@ -79,22 +75,10 @@ public final class Joke extends ThreadedModule {
static Message randomJoke() throws ModuleException { static Message randomJoke() throws ModuleException {
try { try {
final URL url = new URL(JOKE_URL); final URL url = new URL(JOKE_URL);
final URLConnection conn = url.openConnection(); final JSONObject json = new JSONObject(Utils.urlReader(url));
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( return new PublicMessage(
json.getJSONObject("value").get("joke").toString().replace("\\'", "'") json.getJSONObject("value").get("joke").toString().replace("\\'", "'")
.replace("\\\"", "\"")); .replace("\\\"", "\""));
}
} catch (Exception e) { } catch (Exception e) {
throw new ModuleException("randomJoke()", "An error has occurred retrieving a random joke.", e); throw new ModuleException("randomJoke()", "An error has occurred retrieving a random joke.", e);
} }

View file

@ -38,6 +38,8 @@ import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Calendar; import java.util.Calendar;
@ -144,6 +146,12 @@ public class UtilsTest {
assertThat("17 years 2 months 2 weeks 1 day 6 hours 45 minutes").isEqualTo(Utils.uptime(547800300076L)); 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 @Test
public void testUtcDateTime() { public void testUtcDateTime() {
assertThat(Utils.utcDateTime(cal.getTime())).as("utcDateTime(date)").isEqualTo("1952-02-17 12:30"); assertThat(Utils.utcDateTime(cal.getTime())).as("utcDateTime(date)").isEqualTo("1952-02-17 12:30");