Работающий прототип расширения веб интерфейса
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
group = 'asys'
|
||||
version = '0.14-SNAPSHOT'
|
||||
version = '0.15-SNAPSHOT'
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
|
||||
@@ -52,7 +52,7 @@ public class Activator implements BundleActivator {
|
||||
@Override
|
||||
public Map<String, String> getMainMenuItems() {
|
||||
return new HashMap<String, String>(){{
|
||||
this.put("Модули", "#");
|
||||
this.put("Модули", "/modules");
|
||||
}};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2017-03-15
|
||||
*/
|
||||
package asys.webinterface;
|
||||
|
||||
import asys.webinterface.api.HttpReqResp;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
public class HttpReqRespImpl implements HttpReqResp {
|
||||
private HttpExchange httpExchange;
|
||||
|
||||
public HttpReqRespImpl(HttpExchange httpExchange) {
|
||||
this.httpExchange = httpExchange;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getRequestURIParts() {
|
||||
String uriStr = httpExchange.getRequestURI().toString().replaceAll("/{2,}", "/");
|
||||
if (uriStr.startsWith("/")) {
|
||||
uriStr = uriStr.replaceFirst("/","");
|
||||
}
|
||||
|
||||
String[] result = uriStr.split("/");
|
||||
if (result.length == 1 && result[0].isEmpty()) {
|
||||
return new String[0];
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,23 +4,28 @@
|
||||
*/
|
||||
package asys.webinterface;
|
||||
|
||||
import asys.webinterface.api.HttpReqResp;
|
||||
import asys.webinterface.api.WebModule;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class IndexHandler implements HttpHandler {
|
||||
private final Logger logger = LoggerFactory.getLogger(IndexHandler.class);
|
||||
static final Charset defaultCharset = Charset.forName("UTF-8");
|
||||
private String htmlTemplate;
|
||||
private WebinterfaceImpl webinterface;
|
||||
|
||||
public IndexHandler(WebinterfaceImpl webinterface) {
|
||||
try {
|
||||
InputStream inputStream = getClass().getResourceAsStream("/index.html");
|
||||
htmlTemplate = IOUtils.toString(inputStream, "UTF-8")
|
||||
.replace("<{moduleScript}>", webinterface.javascriptToTags("modules"))
|
||||
.replace("<{moduleStylesheet}>", webinterface.stylesheetToTags("modules"));
|
||||
htmlTemplate = IOUtils.toString(inputStream, "UTF-8");
|
||||
this.webinterface = webinterface;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error load htmlTemplate", e);
|
||||
}
|
||||
@@ -28,10 +33,32 @@ public class IndexHandler implements HttpHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange httpExchange) throws IOException {
|
||||
HttpReqResp httpReqResp = new HttpReqRespImpl(httpExchange);
|
||||
String[] requestURIParts = httpReqResp.getRequestURIParts();
|
||||
if (requestURIParts.length > 0 && requestURIParts[0].equals("module")) {
|
||||
logger.debug("Module request: {}", requestURIParts[1]);
|
||||
WebModule module = webinterface.getModule(requestURIParts[1]);
|
||||
if (module != null) {
|
||||
sendHtmlTemplate(prepareHtmlTemplate(module), httpExchange);
|
||||
} else {
|
||||
logger.warn("404 - module \"{}\" not found", requestURIParts[1]);
|
||||
}
|
||||
} else if (requestURIParts.length == 0 || requestURIParts[0].equals("modules")) {
|
||||
logger.debug("use default module");
|
||||
sendHtmlTemplate(prepareHtmlTemplate(webinterface.getModule("modules")), httpExchange);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] prepareHtmlTemplate(WebModule module) {
|
||||
return htmlTemplate.replace("<{moduleScript}>", webinterface.javascriptToTags(module))
|
||||
.replace("<{moduleStylesheet}>", webinterface.stylesheetToTags(module)).getBytes(defaultCharset);
|
||||
}
|
||||
|
||||
private void sendHtmlTemplate(byte[] htmlTemplateBytes, HttpExchange httpExchange) throws IOException {
|
||||
httpExchange.getResponseHeaders().add("Context-Type","text/html;charset=utf-8");
|
||||
httpExchange.sendResponseHeaders(200, htmlTemplate.length());
|
||||
httpExchange.sendResponseHeaders(200, htmlTemplateBytes.length);
|
||||
OutputStream responseBody = httpExchange.getResponseBody();
|
||||
responseBody.write(htmlTemplate.getBytes(defaultCharset));
|
||||
responseBody.write(htmlTemplateBytes);
|
||||
responseBody.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,11 @@ public class WebinterfaceImpl implements Webinterface {
|
||||
webModules.remove(moduleName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebModule getModule(String moduleName) {
|
||||
return webModules.get(moduleName);
|
||||
}
|
||||
|
||||
public String mainMenuItemsToReactJS() {
|
||||
StringJoiner sj = new StringJoiner(",");
|
||||
|
||||
@@ -37,10 +42,9 @@ public class WebinterfaceImpl implements Webinterface {
|
||||
return sj.toString();
|
||||
}
|
||||
|
||||
public String javascriptToTags(String moduleName) {
|
||||
public String javascriptToTags(WebModule webModule) {
|
||||
StringJoiner sj = new StringJoiner("\n");
|
||||
|
||||
WebModule webModule = webModules.get(moduleName);
|
||||
for (String link : webModule.getJavaScriptLinks()) {
|
||||
sj.add(String.format("<script src=\"%s\"></script>", link));
|
||||
}
|
||||
@@ -48,10 +52,9 @@ public class WebinterfaceImpl implements Webinterface {
|
||||
return sj.toString();
|
||||
}
|
||||
|
||||
public String stylesheetToTags(String moduleName) {
|
||||
public String stylesheetToTags(WebModule webModule) {
|
||||
StringJoiner sj = new StringJoiner("\n");
|
||||
|
||||
WebModule webModule = webModules.get(moduleName);
|
||||
for (String link : webModule.getJavaScriptLinks()) {
|
||||
sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link));
|
||||
}
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
package asys.webinterface.api;
|
||||
|
||||
public interface HttpReqResp {
|
||||
String[] getRequestURIParts();
|
||||
}
|
||||
|
||||
@@ -7,4 +7,5 @@ package asys.webinterface.api;
|
||||
public interface Webinterface {
|
||||
void addModule(WebModule webModule);
|
||||
void removeModule(String moduleName);
|
||||
WebModule getModule(String moduleName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user