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; 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.io.BufferedReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Properties; import java.util.HashMap;
import java.util.Map;
import org.parabot.core.Core;
import org.parabot.environment.api.utils.WebUtil;
/** /**
* *
@@ -14,19 +19,29 @@ import org.parabot.environment.api.utils.WebUtil;
* *
*/ */
public class ServerProviderInfo { public class ServerProviderInfo {
private Properties properties; private HashMap<String, String> properties;
public ServerProviderInfo(URL providerInfo, String username, String password) { public ServerProviderInfo(URL providerInfo, String username, String password) {
this.properties = new Properties(); this.properties = new HashMap<>();
try { try {
String line; String line;
Core.verbose("Reading info: " + providerInfo); Core.verbose("Reading info: " + providerInfo);
BufferedReader br = WebUtil.getReader(providerInfo, username, password); BufferedReader br = WebUtil.getReader(new URL(providerInfo.toString() + "&method=json"), username, password);
while ((line = br.readLine()) != null) {
if(line.contains(": ")) {
Core.verbose(line); JSONParser parser = new JSONParser();
properties.put(line.substring(0, line.indexOf(": ")), line.substring(line.indexOf(": ") + 2, line.length())); 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(); br.close();
} catch (Exception e) { } catch (Exception e) {
@@ -36,7 +51,7 @@ public class ServerProviderInfo {
public URL getClient() { public URL getClient() {
try { try {
return new URL(properties.getProperty("client")); return new URL(properties.get("client"));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -45,7 +60,7 @@ public class ServerProviderInfo {
public URL getHookFile() { public URL getHookFile() {
try { try {
return new URL(properties.getProperty("hooks")); return new URL(properties.get("hooks"));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -53,28 +68,26 @@ public class ServerProviderInfo {
} }
public String getClientClass() { public String getClientClass() {
return properties.getProperty("clientClass"); return properties.get("clientClass");
} }
public String getServerName() { public String getServerName() {
return properties.getProperty("serverName"); return properties.get("serverName");
} }
public long getCRC32() { public long getCRC32() {
return Long.parseLong(properties.getProperty("crc32")); return Long.parseLong(properties.get("crc32"));
} }
public long getClientCRC32() { public long getClientCRC32() {
return Long.parseLong(properties.getProperty("clientCrc32")); return Long.parseLong(properties.get("clientCrc32"));
} }
public int getBankTabs() { 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; return this.properties;
} }
} }