108 lines
3.7 KiB
Java
108 lines
3.7 KiB
Java
/*
|
|
* DmitriyMX <mail@dmitriymx.ru>
|
|
* 2017-03-15
|
|
*/
|
|
package asys.mcsmanager;
|
|
|
|
import asys.mcsmanager.packets.CS_Ping;
|
|
import asys.webinterface.api.WebModule;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.google.gson.JsonElement;
|
|
import com.sun.net.httpserver.HttpExchange;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class MCSM_WebModule extends WebModule {
|
|
private static final Gson GSON = new GsonBuilder()
|
|
.registerTypeAdapter(ServerInfo.class, new ServerInfoSerializer())
|
|
.registerTypeAdapter(CS_Ping.class, new CSPingJsonSerializer())
|
|
.create();
|
|
private final String MODULE_NAME = "mcsmanager";
|
|
private final String MODULE_URL = "/"+MODULE_NAME;
|
|
private final Pattern URL_PATTERN_JS = Pattern.compile(MODULE_URL+"/(\\w+)\\.js");
|
|
private final Pattern URL_PATTERN_CSS = Pattern.compile(MODULE_URL+"/(\\w+)\\.css");
|
|
private Manager manager;
|
|
|
|
MCSM_WebModule(Manager manager) {
|
|
this.manager = manager;
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return MODULE_NAME;
|
|
}
|
|
|
|
@Override
|
|
public String getReactJSModuleLink() {
|
|
return "/"+MODULE_NAME+"/module.js";
|
|
}
|
|
|
|
@Override
|
|
public Map<String, String> getMainMenuItems() {
|
|
return new HashMap<String, String>(){{
|
|
this.put("Серверы", "/"+MODULE_NAME);
|
|
}};
|
|
}
|
|
|
|
@Override
|
|
public boolean handle(HttpExchange httpExchange) throws IOException {
|
|
String urlPath = httpExchange.getRequestURI().getPath();
|
|
if (urlPath.equals(MODULE_URL+"/servers.json")) {
|
|
return handleServersJson(httpExchange);
|
|
} else {
|
|
Matcher matcher = URL_PATTERN_JS.matcher(urlPath);
|
|
if (matcher.find()) {
|
|
InputStream stream = getClass().getResourceAsStream("/" + matcher.group(1) + ".js");
|
|
if (stream == null) {
|
|
this.sendHttpCode(httpExchange, 404, "not found");
|
|
return true;
|
|
}
|
|
httpExchange.getResponseHeaders().add("Content-Type", "text/javascript;charset=utf-8");
|
|
this.sendContent(httpExchange, 0, stream);
|
|
return true;
|
|
}
|
|
|
|
//FIXME дублирование кода
|
|
matcher = URL_PATTERN_CSS.matcher(urlPath);
|
|
if (matcher.find()) {
|
|
InputStream stream = getClass().getResourceAsStream("/" + matcher.group(1) + ".css");
|
|
if (stream == null) {
|
|
this.sendHttpCode(httpExchange, 404, "not found");
|
|
return true;
|
|
}
|
|
httpExchange.getResponseHeaders().add("Content-Type", "text/css;charset=utf-8");
|
|
this.sendContent(httpExchange, 0, stream);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private boolean handleServersJson(HttpExchange httpExchange) throws IOException {
|
|
if (httpExchange.getRequestURI().getQuery() != null &&
|
|
!httpExchange.getRequestURI().getQuery().isEmpty()) {
|
|
Map<String, String> query = this.queryToMap(httpExchange.getRequestURI().getQuery());
|
|
ServerInfo serverInfo = manager.getInfo(query.get("clientid"));
|
|
if (serverInfo == null) {
|
|
this.sendJson(httpExchange, "{}");
|
|
} else {
|
|
this.sendJson(httpExchange, GSON.toJson(serverInfo));
|
|
}
|
|
} else {
|
|
this.sendJson(httpExchange, serverList());
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private JsonElement serverList() {
|
|
return GSON.toJsonTree(manager.getServerList());
|
|
}
|
|
}
|