[CLEANUP] Code cleanup and added javadoc

This commit is contained in:
Alexander Bielen
2018-12-12 22:34:46 +01:00
parent 2074dab2f3
commit 1aedd89889
6 changed files with 41 additions and 21 deletions
@@ -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 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_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_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_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 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"; public static final String SERVER_PROVIDER_INFO = "http://v3.bdn.parabot.org/api/bot/list/%s?latest=true";
@@ -166,6 +166,7 @@ public class ServerProviderInfo {
/** /**
* Gets the URL to download the Randoms JAR from. * 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. * @return The provided URL in the server config JSON (denoted by 'randoms:') or, fallback to the default BDN URL.
*/ */
public URL getRandoms() { public URL getRandoms() {
@@ -186,10 +187,10 @@ public class ServerProviderInfo {
/** /**
* Gets the current provider version * Gets the current provider version
* *
* @return * @return provider version
*/ */
public String getProviderVersion() { 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) { if(providerType != null) {
String providerInfo = String.format(Configuration.SERVER_PROVIDER_INFO, providerType); String providerInfo = String.format(Configuration.SERVER_PROVIDER_INFO, providerType);
return WebUtil.getJsonValue(providerInfo, "version"); return WebUtil.getJsonValue(providerInfo, "version");
@@ -87,10 +87,24 @@ public class FileUtil {
destination.close(); 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 { public static String getFileContents(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath())); 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 { public static void writeFileContents(File file, String contents) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file.getAbsolutePath())); BufferedWriter writer = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
writer.write(contents); writer.write(contents);
@@ -8,7 +8,7 @@ import java.io.File;
import java.util.HashMap; 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 * @author AlexanderBielen
*/ */
@@ -16,7 +16,6 @@ public class PBLocalPreferences {
private static JSONParser parser = new JSONParser(); private static JSONParser parser = new JSONParser();
private File settingsFile; private File settingsFile;
public PBLocalPreferences(String fileName) { public PBLocalPreferences(String fileName) {
settingsFile = new File(Directories.getSettingsPath() + "/" + secureFileName(fileName)); settingsFile = new File(Directories.getSettingsPath() + "/" + secureFileName(fileName));
} }
@@ -63,8 +62,8 @@ public class PBLocalPreferences {
/** /**
* Adds a setting, or overwrites it if it exists * Adds a setting, or overwrites it if it exists
* *
* @param key * @param key key of the setting
* @param value * @param value value of the setting
*/ */
public void addSetting(String key, String value) { public void addSetting(String key, String value) {
HashMap<String, String> pair = new HashMap<>(); HashMap<String, String> pair = new HashMap<>();
@@ -75,8 +74,8 @@ public class PBLocalPreferences {
/** /**
* Fetches a setting * Fetches a setting
* *
* @param key * @param key key to get the value for
* @return * @return value that belongs to given key or null if non-existent
*/ */
public String getSetting(String key) { public String getSetting(String key) {
if(getSettings() == null) { if(getSettings() == null) {
@@ -89,8 +88,8 @@ public class PBLocalPreferences {
/** /**
* Adjusts an existing setting * Adjusts an existing setting
* *
* @param key * @param key key to adjust the value for
* @param value * @param value value for the key
*/ */
public void adjustSetting(String key, String value) { public void adjustSetting(String key, String value) {
addSetting(key, value); addSetting(key, value);
@@ -99,7 +98,7 @@ public class PBLocalPreferences {
/** /**
* Removes a setting * Removes a setting
* *
* @param key * @param key key to remove
*/ */
public void removeSetting(String key) { public void removeSetting(String key) {
JSONObject json = getSettings(); JSONObject json = getSettings();
@@ -107,7 +106,13 @@ public class PBLocalPreferences {
writeSettings(json, false); 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("..", "");
} }
} }
@@ -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 * Fetches a single value from a JSON string at the given url
* *
* @param url * @param url url to get the JSON string from
* @param key * @param key key to search for in the JSON string
* @return value that belongs to given key * @return value that belongs to given key
*/ */
public static String getJsonValue(String url, String key) { public static String getJsonValue(String url, String key) {
@@ -39,7 +39,7 @@ public class PublicServerExecuter extends ServerExecuter {
private String serverName; private String serverName;
private PBLocalPreferences settings; private PBLocalPreferences settings;
private final String CACHE_VERSION_KEY = "cachedProviderVersion"; private final String cacheVersionKey = "cachedProviderVersion";
public PublicServerExecuter(final String serverName) { public PublicServerExecuter(final String serverName) {
this.serverName = serverName; this.serverName = serverName;
@@ -63,9 +63,9 @@ public class PublicServerExecuter extends ServerExecuter {
} }
settings = new PBLocalPreferences(serverProviderInfo.getClientCRC32()+".json"); settings = new PBLocalPreferences(serverProviderInfo.getClientCRC32()+".json");
if(settings.getSetting(CACHE_VERSION_KEY) != null) { if(settings.getSetting(cacheVersionKey) != null) {
Core.verbose(String.format("Latest provider version: %s, local provider version: %s", settings.getSetting(CACHE_VERSION_KEY), providerVersion)); Core.verbose(String.format("Latest provider version: %s, local provider version: %s", settings.getSetting(cacheVersionKey), providerVersion));
if(!settings.getSetting(CACHE_VERSION_KEY).equals(providerVersion)) { if(!settings.getSetting(cacheVersionKey).equals(providerVersion)) {
Core.verbose("Local provider outdated, clearing cache."); Core.verbose("Local provider outdated, clearing cache.");
Directories.clearCache(); Directories.clearCache();
} }
@@ -73,7 +73,7 @@ public class PublicServerExecuter extends ServerExecuter {
Core.verbose("No local provider version in settings, adding to settings file"); 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()) { if (destination.exists()) {
Core.verbose("Found cached server provider [CRC32: " + serverProviderInfo.getCRC32() + "]"); Core.verbose("Found cached server provider [CRC32: " + serverProviderInfo.getCRC32() + "]");