[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 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";
@@ -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");
@@ -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);
@@ -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<String, String> 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("..", "");
}
}
@@ -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) {
@@ -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() + "]");