WebInterface: задействование фреймворка AngularJS
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
<groupId>asys</groupId>
|
<groupId>asys</groupId>
|
||||||
<artifactId>webinterface</artifactId>
|
<artifactId>webinterface</artifactId>
|
||||||
<version>0.3-SNAPSHOT</version>
|
<version>0.4-SNAPSHOT</version>
|
||||||
<packaging>bundle</packaging>
|
<packaging>bundle</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -33,6 +33,13 @@
|
|||||||
<artifactId>api</artifactId>
|
<artifactId>api</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.7</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2016-12-13
|
||||||
|
*/
|
||||||
|
package asys.webinterface;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
|
import org.osgi.framework.Bundle;
|
||||||
|
import org.osgi.framework.BundleContext;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
public class AjaxHandler implements HttpHandler {
|
||||||
|
private static final Charset defaultCharset = Charset.forName("UTF-8");
|
||||||
|
private BundleContext context;
|
||||||
|
|
||||||
|
public AjaxHandler(BundleContext context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(HttpExchange httpExchange) throws IOException {
|
||||||
|
String outJson = "{}";
|
||||||
|
|
||||||
|
String jsonPath = httpExchange.getRequestURI().getPath();
|
||||||
|
if (jsonPath.equals("/ajax/bundles.json")) {
|
||||||
|
JsonArray jsonArray = new JsonArray();
|
||||||
|
|
||||||
|
for (Bundle bundle : context.getBundle(0).getBundleContext().getBundles()) {
|
||||||
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
jsonObject.addProperty("name", bundle.getHeaders().get("Bundle-Name"));
|
||||||
|
jsonObject.addProperty("symname", bundle.getSymbolicName());
|
||||||
|
jsonArray.add(jsonObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
outJson = jsonArray.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
httpExchange.getResponseHeaders().add("Context-Type", "application/json;charset=utf-8");
|
||||||
|
httpExchange.sendResponseHeaders(200, 0);
|
||||||
|
OutputStream responseBody = httpExchange.getResponseBody();
|
||||||
|
responseBody.write(outJson.getBytes(defaultCharset));
|
||||||
|
responseBody.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,13 +6,9 @@ package asys.webinterface;
|
|||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import com.sun.net.httpserver.HttpHandler;
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
import org.osgi.framework.Bundle;
|
|
||||||
import org.osgi.framework.BundleContext;
|
import org.osgi.framework.BundleContext;
|
||||||
import org.osgi.framework.BundleException;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -20,38 +16,32 @@ import java.util.Map;
|
|||||||
public class IndexHandler implements HttpHandler {
|
public class IndexHandler implements HttpHandler {
|
||||||
private static final Charset defaultCharset = Charset.forName("UTF-8");
|
private static final Charset defaultCharset = Charset.forName("UTF-8");
|
||||||
private BundleContext context;
|
private BundleContext context;
|
||||||
|
private String htmlTemplate;
|
||||||
|
|
||||||
public IndexHandler(BundleContext context) {
|
public IndexHandler(BundleContext context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
|
||||||
|
try {
|
||||||
|
InputStream inputStream = getClass().getResourceAsStream("/index.html");
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
sb.append(line).append("\n");
|
||||||
|
}
|
||||||
|
htmlTemplate = sb.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Error load htmlTemplate", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange httpExchange) throws IOException {
|
public void handle(HttpExchange httpExchange) throws IOException {
|
||||||
URI requestURI = httpExchange.getRequestURI();
|
httpExchange.getResponseHeaders().add("Context-Type","text/html;charset=utf-8");
|
||||||
if (requestURI.getQuery() != null && !requestURI.getQuery().isEmpty()) {
|
httpExchange.sendResponseHeaders(200, htmlTemplate.length());
|
||||||
Map<String, String> map = queryToMap(requestURI.getQuery());
|
|
||||||
int bndid = Integer.valueOf(map.get("bnd"));
|
|
||||||
try {
|
|
||||||
context.getBundle(bndid).stop();
|
|
||||||
} catch (BundleException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder htmlText = new StringBuilder("<html><body>");
|
|
||||||
htmlText.append("<h1>").append("ASys Web interface").append("</h1>")
|
|
||||||
.append("<ul>");
|
|
||||||
for (Bundle bundle : context.getBundle(0).getBundleContext().getBundles()) {
|
|
||||||
htmlText.append("<li>").append(bundle.getHeaders().get("Bundle-Name"))
|
|
||||||
.append(" <a href=\"?bnd=").append(bundle.getBundleId()).append("&act=stop\">[Stop]</a>")
|
|
||||||
.append("</li>");
|
|
||||||
}
|
|
||||||
htmlText.append("</ul></body></html>");
|
|
||||||
|
|
||||||
httpExchange.sendResponseHeaders(200, htmlText.length());
|
|
||||||
httpExchange.setAttribute("Context-Type", "text/html;charset=utf-8");
|
|
||||||
OutputStream responseBody = httpExchange.getResponseBody();
|
OutputStream responseBody = httpExchange.getResponseBody();
|
||||||
responseBody.write(htmlText.toString().getBytes(defaultCharset));
|
responseBody.write(htmlTemplate.getBytes(defaultCharset));
|
||||||
responseBody.close();
|
responseBody.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2016-12-12
|
||||||
|
*/
|
||||||
|
package asys.webinterface;
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
public class StaticHandler implements HttpHandler {
|
||||||
|
@Override
|
||||||
|
public void handle(HttpExchange httpExchange) throws IOException {
|
||||||
|
String fileId = httpExchange.getRequestURI().getPath();
|
||||||
|
fileId = fileId.replace("../", "").replace("//", "/");
|
||||||
|
System.err.println(fileId);
|
||||||
|
InputStream inputStream = getClass().getResourceAsStream(fileId);
|
||||||
|
if (inputStream == null) {
|
||||||
|
String response = "Error 404 File not found.";
|
||||||
|
httpExchange.sendResponseHeaders(404, response.length());
|
||||||
|
OutputStream outputStream = httpExchange.getResponseBody();
|
||||||
|
outputStream.write(response.getBytes());
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
} else {
|
||||||
|
httpExchange.sendResponseHeaders(200, 0);
|
||||||
|
OutputStream outputStream = httpExchange.getResponseBody();
|
||||||
|
final byte[] buffer = new byte[0x10000];
|
||||||
|
int len;
|
||||||
|
while((len = inputStream.read(buffer)) > 0) {
|
||||||
|
outputStream.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,6 +21,8 @@ public class WebServer {
|
|||||||
public void start(int port) throws IOException {
|
public void start(int port) throws IOException {
|
||||||
server = HttpServer.create(new InetSocketAddress(port), 0);
|
server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||||
server.createContext("/", new IndexHandler(context));
|
server.createContext("/", new IndexHandler(context));
|
||||||
|
server.createContext("/static", new StaticHandler());
|
||||||
|
server.createContext("/ajax", new AjaxHandler(context));
|
||||||
server.start();
|
server.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
webinterface/src/main/resources/index.html
Normal file
15
webinterface/src/main/resources/index.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" ng-app="asysApp">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>ASys: Web interface</title>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
|
||||||
|
<script src="/static/app.js"></script>
|
||||||
|
<script src="/static/bundle-list.component.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>ASys: Web interface</h1>
|
||||||
|
<hr>
|
||||||
|
<bundle-list></bundle-list>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
2
webinterface/src/main/resources/static/app.js
Normal file
2
webinterface/src/main/resources/static/app.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
'use strict';
|
||||||
|
angular.module('asysApp', []);
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
angular.
|
||||||
|
module('asysApp').
|
||||||
|
component('bundleList', {
|
||||||
|
template:
|
||||||
|
'<ul>' +
|
||||||
|
'<li ng-repeat="bundle in $ctrl.bundles">' +
|
||||||
|
'<b>{{bundle.name}}</b>' +
|
||||||
|
'<p>{{bundle.symname}}</p>' +
|
||||||
|
'</li>' +
|
||||||
|
'</ul>',
|
||||||
|
controller: function PhoneListController($http) {
|
||||||
|
var self = this;
|
||||||
|
$http.get('/ajax/bundles.json').then(function(response) {
|
||||||
|
self.bundles = response.data;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user