Using JSON to gather the server information

This commit is contained in:
JKetelaar
2014-07-23 00:41:19 +02:00
parent 73abf39ade
commit c4323b6c8b
@@ -1,12 +1,17 @@
package org.parabot.core.desc;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.parabot.core.Core;
import org.parabot.core.ui.utils.UILog;
import org.parabot.environment.api.utils.WebUtil;
import javax.swing.*;
import java.io.BufferedReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import org.parabot.core.Core;
import org.parabot.environment.api.utils.WebUtil;
import java.util.HashMap;
import java.util.Map;
/**
*
@@ -14,19 +19,29 @@ import org.parabot.environment.api.utils.WebUtil;
*
*/
public class ServerProviderInfo {
private Properties properties;
private HashMap<String, String> properties;
public ServerProviderInfo(URL providerInfo, String username, String password) {
this.properties = new Properties();
this.properties = new HashMap<>();
try {
String line;
Core.verbose("Reading info: " + providerInfo);
BufferedReader br = WebUtil.getReader(providerInfo, username, password);
while ((line = br.readLine()) != null) {
if(line.contains(": ")) {
Core.verbose(line);
properties.put(line.substring(0, line.indexOf(": ")), line.substring(line.indexOf(": ") + 2, line.length()));
}
BufferedReader br = WebUtil.getReader(new URL(providerInfo.toString() + "&method=json"), username, password);
JSONParser parser = new JSONParser();
if ((line = br.readLine()) != null) {
JSONObject jsonObject = (JSONObject) parser.parse(line);
for (Object o : jsonObject.entrySet()) {
Map.Entry pairs = (Map.Entry) o;
properties.put(String.valueOf(pairs.getKey()), String.valueOf(pairs.getValue()));
}
}else{
UILog.log(
"Error",
"Failed to load server provider, error: [No information about the provider found.]",
JOptionPane.ERROR_MESSAGE);
return;
}
br.close();
} catch (Exception e) {
@@ -36,7 +51,7 @@ public class ServerProviderInfo {
public URL getClient() {
try {
return new URL(properties.getProperty("client"));
return new URL(properties.get("client"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
@@ -45,7 +60,7 @@ public class ServerProviderInfo {
public URL getHookFile() {
try {
return new URL(properties.getProperty("hooks"));
return new URL(properties.get("hooks"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
@@ -53,28 +68,26 @@ public class ServerProviderInfo {
}
public String getClientClass() {
return properties.getProperty("clientClass");
return properties.get("clientClass");
}
public String getServerName() {
return properties.getProperty("serverName");
return properties.get("serverName");
}
public long getCRC32() {
return Long.parseLong(properties.getProperty("crc32"));
return Long.parseLong(properties.get("crc32"));
}
public long getClientCRC32() {
return Long.parseLong(properties.getProperty("clientCrc32"));
return Long.parseLong(properties.get("clientCrc32"));
}
public int getBankTabs() {
return Integer.parseInt(properties.getProperty("bankTabs"));
return Integer.parseInt(properties.get("bankTabs"));
}
public Properties getProperties() {
public HashMap<String, String> getProperties() {
return this.properties;
}
}