Archived
0

MCSM: отображение реального списка серверов

This commit is contained in:
2017-05-01 14:42:16 +03:00
parent 26352ef17e
commit c8fc952ffc
5 changed files with 58 additions and 29 deletions

View File

@@ -31,7 +31,7 @@ public class Activator implements BundleActivator, ServiceListener {
Manager manager = new Manager();
module = new MCSM_WebModule();
module = new MCSM_WebModule(manager);
logger.debug("Get service: {}", Webinterface.class);
serviceTracker = new ServiceTracker<>(context, Webinterface.class, null);

View File

@@ -5,6 +5,8 @@
package asys.mcsmanager;
import asys.webinterface.api.WebModule;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
@@ -15,8 +17,15 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MCSM_WebModule extends WebModule {
private static final Gson GSON = new Gson();
private Manager manager;
private final String MODULE_NAME = "mcsmanager";
private final Pattern URL_PATTERN_JS = Pattern.compile("/"+MODULE_NAME+"/(\\w+)\\.js");
private final String MODULE_URL = "/"+MODULE_NAME;
private final Pattern URL_PATTERN_JS = Pattern.compile(MODULE_URL+"/(\\w+)\\.js");
MCSM_WebModule(Manager manager) {
this.manager = manager;
}
@Override
public String getName() {
@@ -38,18 +47,31 @@ public class MCSM_WebModule extends WebModule {
@Override
public boolean handle(HttpExchange httpExchange) throws IOException {
String urlPath = httpExchange.getRequestURI().getPath();
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");
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;
}
httpExchange.getResponseHeaders().add("Content-Type", "text/javascript;charset=utf-8");
this.sendContent(httpExchange, 0, stream);
return true;
}
return false;
}
private boolean handleServersJson(HttpExchange httpExchange) throws IOException {
this.sendJson(httpExchange, serverList());
return true;
}
private JsonElement serverList() {
return GSON.toJsonTree(manager.getServerList());
}
}