From 1aedd898890821d8dd3f76de2fdca13717bd96b3 Mon Sep 17 00:00:00 2001 From: Alexander Bielen Date: Wed, 12 Dec 2018 22:34:46 +0100 Subject: [PATCH] [CLEANUP] Code cleanup and added javadoc --- .../java/org/parabot/core/Configuration.java | 2 +- .../parabot/core/desc/ServerProviderInfo.java | 5 ++-- .../environment/api/utils/FileUtil.java | 14 ++++++++++ .../api/utils/PBLocalPreferences.java | 27 +++++++++++-------- .../environment/api/utils/WebUtil.java | 4 +-- .../executers/PublicServerExecuter.java | 10 +++---- 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/parabot/core/Configuration.java b/src/main/java/org/parabot/core/Configuration.java index 27e1a6b..08a8ca1 100644 --- a/src/main/java/org/parabot/core/Configuration.java +++ b/src/main/java/org/parabot/core/Configuration.java @@ -11,7 +11,7 @@ public class Configuration extends org.parabot.api.Configuration { public static final String LOGIN_SERVER = "http://bdn.parabot.org/api/v2/users/login"; public static final String GET_SCRIPTS = "http://bdn.parabot.org/api/get.php?action=scripts_scripts&server="; public static final String GET_SCRIPT = "http://bdn.parabot.org/api/get.php?action=scripts_script&id="; - public static final String GET_SERVER_PROVIDER_TYPE = "http://v3.bdn.parabot.org/api/bot/server/type?server="; + public static final String GET_SERVER_PROVIDER_TYPE = "http://v3.bdn.parabot.org/api/bot/server/type?server=%s"; public static final String GET_SERVER_PROVIDERS = "http://bdn.parabot.org/api/get.php?action=server_providers"; public static final String GET_SERVER_PROVIDER = "http://v3.bdn.parabot.org/api/bot/download/provider?nightly=%s&server=%s"; public static final String SERVER_PROVIDER_INFO = "http://v3.bdn.parabot.org/api/bot/list/%s?latest=true"; diff --git a/src/main/java/org/parabot/core/desc/ServerProviderInfo.java b/src/main/java/org/parabot/core/desc/ServerProviderInfo.java index 81d3bcd..b5e84ae 100644 --- a/src/main/java/org/parabot/core/desc/ServerProviderInfo.java +++ b/src/main/java/org/parabot/core/desc/ServerProviderInfo.java @@ -166,6 +166,7 @@ public class ServerProviderInfo { /** * Gets the URL to download the Randoms JAR from. + * * @return The provided URL in the server config JSON (denoted by 'randoms:') or, fallback to the default BDN URL. */ public URL getRandoms() { @@ -186,10 +187,10 @@ public class ServerProviderInfo { /** * Gets the current provider version * - * @return + * @return provider version */ public String getProviderVersion() { - String providerType = WebUtil.getJsonValue(Configuration.GET_SERVER_PROVIDER_TYPE + properties.getProperty("name"), "type"); + String providerType = WebUtil.getJsonValue(String.format(Configuration.GET_SERVER_PROVIDER_TYPE , properties.getProperty("name")), "type"); if(providerType != null) { String providerInfo = String.format(Configuration.SERVER_PROVIDER_INFO, providerType); return WebUtil.getJsonValue(providerInfo, "version"); diff --git a/src/main/java/org/parabot/environment/api/utils/FileUtil.java b/src/main/java/org/parabot/environment/api/utils/FileUtil.java index 6f33897..a7e2fd5 100644 --- a/src/main/java/org/parabot/environment/api/utils/FileUtil.java +++ b/src/main/java/org/parabot/environment/api/utils/FileUtil.java @@ -87,10 +87,24 @@ public class FileUtil { destination.close(); } + /** + * Reads the contents of a text file + * + * @param file file to get contents from + * @return file contents + * @throws IOException when anything goes wrong + */ public static String getFileContents(File file) throws IOException { return new String(Files.readAllBytes(file.toPath())); } + /** + * Writes a string to a file overwriting the existing contents if present + * + * @param file file to write to + * @param contents contents to write to given file + * @throws IOException when anything goes wrong + */ public static void writeFileContents(File file, String contents) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter(file.getAbsolutePath())); writer.write(contents); diff --git a/src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java b/src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java index 66c84b7..74eb1d7 100644 --- a/src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java +++ b/src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java @@ -8,7 +8,7 @@ import java.io.File; import java.util.HashMap; /** - * Manages preferences in a local json file in the Parabot settings folder + * Manages preferences in a local file in JSON format in the Parabot settings folder * * @author AlexanderBielen */ @@ -16,7 +16,6 @@ public class PBLocalPreferences { private static JSONParser parser = new JSONParser(); private File settingsFile; - public PBLocalPreferences(String fileName) { settingsFile = new File(Directories.getSettingsPath() + "/" + secureFileName(fileName)); } @@ -63,8 +62,8 @@ public class PBLocalPreferences { /** * Adds a setting, or overwrites it if it exists * - * @param key - * @param value + * @param key key of the setting + * @param value value of the setting */ public void addSetting(String key, String value) { HashMap pair = new HashMap<>(); @@ -75,8 +74,8 @@ public class PBLocalPreferences { /** * Fetches a setting * - * @param key - * @return + * @param key key to get the value for + * @return value that belongs to given key or null if non-existent */ public String getSetting(String key) { if(getSettings() == null) { @@ -89,8 +88,8 @@ public class PBLocalPreferences { /** * Adjusts an existing setting * - * @param key - * @param value + * @param key key to adjust the value for + * @param value value for the key */ public void adjustSetting(String key, String value) { addSetting(key, value); @@ -99,7 +98,7 @@ public class PBLocalPreferences { /** * Removes a setting * - * @param key + * @param key key to remove */ public void removeSetting(String key) { JSONObject json = getSettings(); @@ -107,7 +106,13 @@ public class PBLocalPreferences { writeSettings(json, false); } - private static String secureFileName(String fileName) { - return fileName.replace("..", ""); + /** + * Replaces all double dots to make sure the link does not leave the settings folder + * + * @param filePath path to secure + * @return secured string + */ + private static String secureFileName(String filePath) { + return filePath.replace("..", ""); } } diff --git a/src/main/java/org/parabot/environment/api/utils/WebUtil.java b/src/main/java/org/parabot/environment/api/utils/WebUtil.java index de52651..8f1532d 100644 --- a/src/main/java/org/parabot/environment/api/utils/WebUtil.java +++ b/src/main/java/org/parabot/environment/api/utils/WebUtil.java @@ -12,8 +12,8 @@ public class WebUtil extends org.parabot.api.io.WebUtil { /** * Fetches a single value from a JSON string at the given url * - * @param url - * @param key + * @param url url to get the JSON string from + * @param key key to search for in the JSON string * @return value that belongs to given key */ public static String getJsonValue(String url, String key) { diff --git a/src/main/java/org/parabot/environment/servers/executers/PublicServerExecuter.java b/src/main/java/org/parabot/environment/servers/executers/PublicServerExecuter.java index 3089548..a316586 100644 --- a/src/main/java/org/parabot/environment/servers/executers/PublicServerExecuter.java +++ b/src/main/java/org/parabot/environment/servers/executers/PublicServerExecuter.java @@ -39,7 +39,7 @@ public class PublicServerExecuter extends ServerExecuter { private String serverName; private PBLocalPreferences settings; - private final String CACHE_VERSION_KEY = "cachedProviderVersion"; + private final String cacheVersionKey = "cachedProviderVersion"; public PublicServerExecuter(final String serverName) { this.serverName = serverName; @@ -63,9 +63,9 @@ public class PublicServerExecuter extends ServerExecuter { } settings = new PBLocalPreferences(serverProviderInfo.getClientCRC32()+".json"); - if(settings.getSetting(CACHE_VERSION_KEY) != null) { - Core.verbose(String.format("Latest provider version: %s, local provider version: %s", settings.getSetting(CACHE_VERSION_KEY), providerVersion)); - if(!settings.getSetting(CACHE_VERSION_KEY).equals(providerVersion)) { + if(settings.getSetting(cacheVersionKey) != null) { + Core.verbose(String.format("Latest provider version: %s, local provider version: %s", settings.getSetting(cacheVersionKey), providerVersion)); + if(!settings.getSetting(cacheVersionKey).equals(providerVersion)) { Core.verbose("Local provider outdated, clearing cache."); Directories.clearCache(); } @@ -73,7 +73,7 @@ public class PublicServerExecuter extends ServerExecuter { Core.verbose("No local provider version in settings, adding to settings file"); } - settings.addSetting(CACHE_VERSION_KEY, providerVersion); + settings.addSetting(cacheVersionKey, providerVersion); if (destination.exists()) { Core.verbose("Found cached server provider [CRC32: " + serverProviderInfo.getCRC32() + "]");