Now using StringUtils.
This commit is contained in:
parent
5a40c1886e
commit
e74b87d371
9 changed files with 59 additions and 66 deletions
|
@ -44,6 +44,7 @@ import com.rometools.rome.io.SyndFeedOutput;
|
|||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
@ -200,7 +201,7 @@ public final class EntriesMgr {
|
|||
bot.getLogger().debug("Saving the feeds...");
|
||||
}
|
||||
|
||||
if (Utils.isValidString(bot.getLogsDir()) && Utils.isValidString(bot.getWeblogUrl())) {
|
||||
if (StringUtils.isNotBlank(bot.getLogsDir()) && StringUtils.isNotBlank(bot.getWeblogUrl())) {
|
||||
try {
|
||||
final SyndFeedOutput output = new SyndFeedOutput();
|
||||
SyndFeed rss = new SyndFeedImpl();
|
||||
|
@ -279,8 +280,8 @@ public final class EntriesMgr {
|
|||
}
|
||||
|
||||
if (isDayBackup) {
|
||||
if (Utils.isValidString(bot.getBacklogsUrl())) {
|
||||
if (history.indexOf(bot.getToday()) == -1) {
|
||||
if (StringUtils.isNotBlank(bot.getBacklogsUrl())) {
|
||||
if (!history.contains(bot.getToday())) {
|
||||
history.add(bot.getToday());
|
||||
|
||||
while (history.size() > MAX_BACKLOGS) {
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -103,7 +103,7 @@ public abstract class AbstractModule {
|
|||
public abstract void helpResponse(final Mobibot bot,
|
||||
final String sender,
|
||||
final String args,
|
||||
final boolean isPrivate);
|
||||
@SuppressWarnings("unused") final boolean isPrivate);
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if the module is enabled.
|
||||
|
@ -134,7 +134,7 @@ public abstract class AbstractModule {
|
|||
*/
|
||||
boolean isValidProperties() {
|
||||
for (final String s : getPropertyKeys()) {
|
||||
if (!Utils.isValidString(properties.get(s))) {
|
||||
if (StringUtils.isBlank(properties.get(s))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ public abstract class AbstractModule {
|
|||
* @param value The value.
|
||||
*/
|
||||
public void setProperty(final String key, final String value) {
|
||||
if (Utils.isValidString(key)) {
|
||||
if (StringUtils.isNotBlank(key)) {
|
||||
properties.put(key, value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ package net.thauvin.erik.mobibot.modules;
|
|||
import net.objecthunter.exp4j.Expression;
|
||||
import net.objecthunter.exp4j.ExpressionBuilder;
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
|
@ -82,7 +82,7 @@ public class Calc extends AbstractModule {
|
|||
*/
|
||||
@Override
|
||||
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
|
||||
if (Utils.isValidString(args)) {
|
||||
if (StringUtils.isNotBlank(args)) {
|
||||
bot.send(calc(args));
|
||||
|
||||
} else {
|
||||
|
|
|
@ -40,6 +40,7 @@ import net.thauvin.erik.mobibot.msg.ErrorMessage;
|
|||
import net.thauvin.erik.mobibot.msg.Message;
|
||||
import net.thauvin.erik.mobibot.msg.NoticeMessage;
|
||||
import net.thauvin.erik.mobibot.msg.PublicMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jdom2.Document;
|
||||
import org.jdom2.Element;
|
||||
import org.jdom2.JDOMException;
|
||||
|
@ -105,7 +106,7 @@ public final class CurrencyConverter extends ThreadedModule {
|
|||
@SuppressFBWarnings("REDOS")
|
||||
@Override
|
||||
void run(final Mobibot bot, final String sender, final String query) {
|
||||
if (Utils.isValidString(sender) && Utils.isValidString(query)) {
|
||||
if (StringUtils.isNotBlank(sender) && StringUtils.isNotBlank(query)) {
|
||||
if (query.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+")) {
|
||||
try {
|
||||
final Message msg = convertCurrency(query);
|
||||
|
@ -146,7 +147,7 @@ public final class CurrencyConverter extends ThreadedModule {
|
|||
|
||||
pubDate = cubeTime.getAttribute("time").getValue();
|
||||
|
||||
final List cubes = cubeTime.getChildren();
|
||||
final List<Element> cubes = cubeTime.getChildren();
|
||||
Element cube;
|
||||
|
||||
for (final Object rawCube : cubes) {
|
||||
|
|
|
@ -37,6 +37,7 @@ import net.thauvin.erik.mobibot.Mobibot;
|
|||
import net.thauvin.erik.mobibot.Utils;
|
||||
import net.thauvin.erik.mobibot.msg.Message;
|
||||
import net.thauvin.erik.mobibot.msg.NoticeMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jibble.pircbot.Colors;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
@ -96,7 +97,7 @@ public final class GoogleSearch extends ThreadedModule {
|
|||
*/
|
||||
@Override
|
||||
void run(final Mobibot bot, final String sender, final String query) {
|
||||
if (Utils.isValidString(query)) {
|
||||
if (StringUtils.isNotBlank(query)) {
|
||||
try {
|
||||
final List<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP),
|
||||
properties.get(GOOGLE_CSE_KEY_PROP));
|
||||
|
@ -125,11 +126,11 @@ public final class GoogleSearch extends ThreadedModule {
|
|||
@SuppressWarnings(("PMD.AvoidInstantiatingObjectsInLoops"))
|
||||
static List<Message> searchGoogle(final String query, final String apiKey, final String cseKey)
|
||||
throws ModuleException {
|
||||
if (!Utils.isValidString(apiKey) || !Utils.isValidString(cseKey)) {
|
||||
throw new ModuleException(Utils.capitalize(GOOGLE_CMD) + " is disabled. The API keys are missing.");
|
||||
if (StringUtils.isBlank(apiKey) || StringUtils.isBlank(cseKey)) {
|
||||
throw new ModuleException(StringUtils.capitalize(GOOGLE_CMD) + " is disabled. The API keys are missing.");
|
||||
}
|
||||
|
||||
if (Utils.isValidString(query)) {
|
||||
if (StringUtils.isNotBlank(query)) {
|
||||
final ArrayList<Message> results = new ArrayList<>();
|
||||
try {
|
||||
final String q = URLEncoder.encode(query, StandardCharsets.UTF_8.toString());
|
||||
|
|
|
@ -32,11 +32,8 @@
|
|||
|
||||
package net.thauvin.erik.mobibot.modules;
|
||||
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import okhttp3.HttpUrl;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* The <code>ModuleException</code> class.
|
||||
|
@ -49,7 +46,6 @@ public class ModuleException extends Exception {
|
|||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final String debugMessage;
|
||||
private final Pattern urlPattern = Pattern.compile("(https?://\\S+)(\\?\\S+)");
|
||||
|
||||
/**
|
||||
* Creates a new exception.
|
||||
|
@ -94,33 +90,19 @@ public class ModuleException extends Exception {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the sanitized (URL query parameters are replaced by char count) message.
|
||||
* Return the sanitized message (e.g. remove API keys, etc.)
|
||||
*
|
||||
* @param sanitize The words to sanitize.
|
||||
* @return The sanitized message.
|
||||
*/
|
||||
@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "false positive?")
|
||||
String getSanitizedMessage() {
|
||||
if (hasCause()) {
|
||||
final String causeMessage = getCause().getMessage();
|
||||
final Matcher matcher = urlPattern.matcher(causeMessage);
|
||||
if (matcher.find()) {
|
||||
final HttpUrl url = HttpUrl.parse(matcher.group(1) + matcher.group(2));
|
||||
if (url != null) {
|
||||
final StringBuilder query = new StringBuilder("?");
|
||||
final int size = url.querySize();
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i > 0) {
|
||||
query.append('&');
|
||||
}
|
||||
query.append(url.queryParameterName(i)).append("=[")
|
||||
.append(url.queryParameterValue(i).length()).append(']');
|
||||
}
|
||||
return getDebugMessage() + "\nCaused by: " + getCause().getClass().getName() + ": "
|
||||
+ causeMessage.replace(matcher.group(2), query);
|
||||
}
|
||||
}
|
||||
String getSanitizedMessage(final String... sanitize) {
|
||||
final String[] obfuscate = new String[sanitize.length];
|
||||
for (int i = 0; i < sanitize.length; i++) {
|
||||
obfuscate[i] = Utils.obfuscate(sanitize[i]);
|
||||
}
|
||||
return getDebugMessage() + "\nCaused by: " + getCause().getClass().getName() + ": " + getCause().getMessage();
|
||||
return getCause().getClass().getName() + ": " + StringUtils.replaceEach(getCause().getMessage(),
|
||||
sanitize,
|
||||
obfuscate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -128,6 +110,7 @@ public class ModuleException extends Exception {
|
|||
*
|
||||
* @return <code>true</code> or <code>false</code>
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
boolean hasCause() {
|
||||
return getCause() != null;
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ import net.thauvin.erik.mobibot.msg.PublicMessage;
|
|||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
@ -68,7 +69,7 @@ public final class StockQuote extends ThreadedModule {
|
|||
*/
|
||||
static final String INVALID_SYMBOL = "Invalid symbol.";
|
||||
// The Alpha Advantage URL.
|
||||
private static final String ALAPHADVANTAGE_URL = "https://www.alphavantage.co/query?function=";
|
||||
private static final String ALAPHAVANTAGE_URL = "https://www.alphavantage.co/query?function=";
|
||||
// The quote command.
|
||||
private static final String STOCK_CMD = "stock";
|
||||
|
||||
|
@ -132,11 +133,11 @@ public final class StockQuote extends ThreadedModule {
|
|||
* @throws ModuleException If an errors occurs.
|
||||
*/
|
||||
static List<Message> getQuote(final String symbol, final String apiKey) throws ModuleException {
|
||||
if (!Utils.isValidString(apiKey)) {
|
||||
throw new ModuleException(Utils.capitalize(STOCK_CMD) + " is disabled. The API key is missing.");
|
||||
if (StringUtils.isBlank(apiKey)) {
|
||||
throw new ModuleException(StringUtils.capitalize(STOCK_CMD) + " is disabled. The API key is missing.");
|
||||
}
|
||||
|
||||
if (Utils.isValidString(symbol)) {
|
||||
if (StringUtils.isNotBlank(symbol)) {
|
||||
final String debugMessage = "getQuote(" + symbol + ')';
|
||||
final ArrayList<Message> messages = new ArrayList<>();
|
||||
final OkHttpClient client = new OkHttpClient();
|
||||
|
@ -144,7 +145,7 @@ public final class StockQuote extends ThreadedModule {
|
|||
try {
|
||||
// Search for symbol/keywords
|
||||
Request request = new Request.Builder().url(
|
||||
ALAPHADVANTAGE_URL + "SYMBOL_SEARCH&keywords=" + symbol + "&apikey=" + apiKey).build();
|
||||
ALAPHAVANTAGE_URL + "SYMBOL_SEARCH&keywords=" + symbol + "&apikey=" + apiKey).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
JSONObject json = getJsonResponse(response, debugMessage);
|
||||
|
@ -159,7 +160,7 @@ public final class StockQuote extends ThreadedModule {
|
|||
|
||||
// Get quote for symbol
|
||||
request = new Request.Builder().url(
|
||||
ALAPHADVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + symbolInfo.getString("1. symbol") + "&apikey="
|
||||
ALAPHAVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + symbolInfo.getString("1. symbol") + "&apikey="
|
||||
+ apiKey).build();
|
||||
response = client.newCall(request).execute();
|
||||
|
||||
|
@ -210,7 +211,7 @@ public final class StockQuote extends ThreadedModule {
|
|||
*/
|
||||
@Override
|
||||
void run(final Mobibot bot, final String sender, final String symbol) {
|
||||
if (Utils.isValidString(symbol)) {
|
||||
if (StringUtils.isNotBlank(symbol)) {
|
||||
try {
|
||||
final List<Message> messages = getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
|
||||
for (final Message msg : messages) {
|
||||
|
|
|
@ -45,10 +45,12 @@ import net.thauvin.erik.mobibot.msg.Message;
|
|||
import net.thauvin.erik.mobibot.msg.NoticeMessage;
|
||||
import net.thauvin.erik.mobibot.msg.PublicMessage;
|
||||
import okhttp3.HttpUrl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jibble.pircbot.Colors;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The <code>Weather2</code> module.
|
||||
|
@ -105,8 +107,8 @@ public class Weather2 extends ThreadedModule {
|
|||
* @throws ModuleException If an error occurs while retrieving the weather data.
|
||||
*/
|
||||
static List<Message> getWeather(final String query, final String apiKey) throws ModuleException {
|
||||
if (!Utils.isValidString(apiKey)) {
|
||||
throw new ModuleException(Utils.capitalize(WEATHER_CMD) + " is disabled. The API key is missing.");
|
||||
if (StringUtils.isBlank(apiKey)) {
|
||||
throw new ModuleException(StringUtils.capitalize(WEATHER_CMD) + " is disabled. The API key is missing.");
|
||||
}
|
||||
|
||||
final OWM owm = new OWM(apiKey);
|
||||
|
@ -114,13 +116,13 @@ public class Weather2 extends ThreadedModule {
|
|||
|
||||
owm.setUnit(OWM.Unit.IMPERIAL);
|
||||
|
||||
if (Utils.isValidString(query)) {
|
||||
if (StringUtils.isNotBlank(query)) {
|
||||
final String[] argv = query.split(",");
|
||||
|
||||
if (argv.length >= 1 && argv.length <= 2) {
|
||||
final String country;
|
||||
final String city = argv[0].trim();
|
||||
if (argv.length > 1 && Utils.isValidString(argv[1])) {
|
||||
if (argv.length > 1 && StringUtils.isNotBlank(argv[1])) {
|
||||
country = argv[1].trim();
|
||||
} else {
|
||||
country = "US";
|
||||
|
@ -160,7 +162,7 @@ public class Weather2 extends ThreadedModule {
|
|||
if (list != null) {
|
||||
for (final Weather w : list) {
|
||||
if (condition.indexOf(",") == -1) {
|
||||
condition.append(Utils.capitalize(w.getDescription()));
|
||||
condition.append(StringUtils.capitalize(w.getDescription()));
|
||||
} else {
|
||||
condition.append(", ").append(w.getDescription());
|
||||
}
|
||||
|
@ -174,11 +176,13 @@ public class Weather2 extends ThreadedModule {
|
|||
messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(),
|
||||
Colors.GREEN));
|
||||
} else {
|
||||
final HttpUrl url =
|
||||
HttpUrl.parse("https://openweathermap.org/find").newBuilder().addQueryParameter(
|
||||
"q", city + ',' + country).build();
|
||||
messages.add(
|
||||
new NoticeMessage(url.toString(), Colors.GREEN));
|
||||
final HttpUrl url = Objects.requireNonNull(HttpUrl.parse(
|
||||
"https://openweathermap.org/find"))
|
||||
.newBuilder()
|
||||
.addQueryParameter("q",
|
||||
city + ',' + country)
|
||||
.build();
|
||||
messages.add(new NoticeMessage(url.toString(), Colors.GREEN));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -204,8 +208,9 @@ public class Weather2 extends ThreadedModule {
|
|||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " <city> [, <country code>]"));
|
||||
bot.send(sender, "For example:");
|
||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " paris, fr"));
|
||||
bot.send(sender, "The default ISO 3166 country code is " + Utils.bold("US")
|
||||
+ ". Zip codes are supported in most countries.");
|
||||
bot.send(sender,
|
||||
"The default ISO 3166 country code is " + Utils.bold("US")
|
||||
+ ". Zip codes are supported in most countries.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -213,7 +218,7 @@ public class Weather2 extends ThreadedModule {
|
|||
*/
|
||||
@Override
|
||||
void run(final Mobibot bot, final String sender, final String args) {
|
||||
if (Utils.isValidString(args)) {
|
||||
if (StringUtils.isNotBlank(args)) {
|
||||
try {
|
||||
final List<Message> messages = getWeather(args, properties.get(OWM_API_KEY_PROP));
|
||||
if (messages.get(0).isError()) {
|
||||
|
|
|
@ -36,6 +36,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|||
import net.thauvin.erik.mobibot.Commands;
|
||||
import net.thauvin.erik.mobibot.Mobibot;
|
||||
import net.thauvin.erik.mobibot.Utils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
@ -143,7 +144,7 @@ public class Tell {
|
|||
@SuppressFBWarnings(value = "CC_CYCLOMATIC_COMPLEXITY", justification = "Working on it.")
|
||||
public void response(final String sender, final String cmds) {
|
||||
final String arrow = " --> ";
|
||||
if (!Utils.isValidString(cmds)) {
|
||||
if (StringUtils.isBlank(cmds)) {
|
||||
helpResponse(sender);
|
||||
} else if (cmds.startsWith(Commands.VIEW_CMD)) {
|
||||
if (bot.isOp(sender) && (Commands.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(cmds)) {
|
||||
|
@ -249,7 +250,7 @@ public class Tell {
|
|||
} else {
|
||||
final String[] split = cmds.split(" ", 2);
|
||||
|
||||
if (split.length == 2 && (Utils.isValidString(split[1]) && split[1].contains(" "))) {
|
||||
if (split.length == 2 && (StringUtils.isNotBlank(split[1]) && split[1].contains(" "))) {
|
||||
if (messages.size() < maxSize) {
|
||||
final TellMessage message = new TellMessage(sender, split[0], split[1].trim());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue