Archived
0

WebInterface: обновление банлов

This commit is contained in:
2016-12-18 13:46:24 +03:00
parent f29ed38289
commit ae3689f408
4 changed files with 72 additions and 16 deletions

View File

@@ -19,7 +19,7 @@
<groupId>asys</groupId>
<artifactId>webinterface</artifactId>
<version>0.6-SNAPSHOT</version>
<version>0.7-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>

View File

@@ -10,10 +10,13 @@ import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
public class AjaxHandler implements HttpHandler {
private static final Charset defaultCharset = Charset.forName("UTF-8");
@@ -29,9 +32,35 @@ public class AjaxHandler implements HttpHandler {
String jsonPath = httpExchange.getRequestURI().getPath();
if (jsonPath.equals("/ajax/bundles.json")) {
outJson = bundleList().toString();
if (httpExchange.getRequestURI().getQuery() != null && !httpExchange.getRequestURI().getQuery().isEmpty()) {
Map<String, String> query = queryToMap(httpExchange.getRequestURI().getQuery());
if (query.containsKey("id")) {
Bundle bundle = context.getBundle(Integer.valueOf(query.get("id")));
if (bundle == null) {
sendResponse(httpExchange, outJson);
return;
}
if (query.containsKey("act") && query.get("act").equals("upd")) {
try {
bundle.update();
} catch (BundleException e) {
e.printStackTrace();
}
}
outJson = bundleInfo(bundle).toString();
}
} else {
outJson = bundleList().toString();
}
}
sendResponse(httpExchange, outJson);
}
private void sendResponse(HttpExchange httpExchange, String outJson) throws IOException {
httpExchange.getResponseHeaders().add("Context-Type", "application/json;charset=utf-8");
httpExchange.sendResponseHeaders(200, 0);
OutputStream responseBody = httpExchange.getResponseBody();
@@ -55,4 +84,32 @@ public class AjaxHandler implements HttpHandler {
return jsonArray;
}
private JsonObject bundleInfo(Bundle bundle) {
JsonObject jsonObject = new JsonObject();
if (bundle != null) {
jsonObject.addProperty("id", bundle.getBundleId());
jsonObject.addProperty("state", bundle.getState());
jsonObject.addProperty("name", bundle.getHeaders().get("Bundle-Name"));
jsonObject.addProperty("symname", bundle.getSymbolicName());
jsonObject.addProperty("lastModified", bundle.getHeaders().get("Bnd-LastModified"));
jsonObject.addProperty("version", bundle.getVersion().toString());
}
return jsonObject;
}
private Map<String, String> queryToMap(String query){
Map<String, String> result = new HashMap<>();
for (String param : query.split("&")) {
String pair[] = param.split("=");
if (pair.length>1) {
result.put(pair[0], pair[1]);
}else{
result.put(pair[0], "");
}
}
return result;
}
}

View File

@@ -44,17 +44,4 @@ public class IndexHandler implements HttpHandler {
responseBody.write(htmlTemplate.getBytes(defaultCharset));
responseBody.close();
}
private Map<String, String> queryToMap(String query){
Map<String, String> result = new HashMap<String, String>();
for (String param : query.split("&")) {
String pair[] = param.split("=");
if (pair.length>1) {
result.put(pair[0], pair[1]);
}else{
result.put(pair[0], "");
}
}
return result;
}
}

View File

@@ -18,7 +18,7 @@ app.component('bundleList', {
template:
'<table width="100%">' +
'<thead><tr>' +
'<th>ID</th><th>Name</th><th>State</th><th>Version</th><th>Last modified</th>' +
'<th>ID</th><th>Name</th><th>State</th><th>Version</th><th>Last modified</th><th>CTRL</th>' +
'</tr></thead>' +
'<tbody>' +
'<tr ng-repeat="bundle in $ctrl.bundles">' +
@@ -27,6 +27,7 @@ app.component('bundleList', {
'<td>{{bundle.state | stateFormat}}</td>' +
'<td>{{bundle.version}}</td>' +
'<td>{{bundle.lastModified | date:"dd/MM/yyyy HH:mm"}}</td>' +
'<td><button ng-click="$ctrl.bndUpd(bundle.id)">update</button></td>' +
'</tr>' +
'</tbody>' +
'</table>',
@@ -35,5 +36,16 @@ app.component('bundleList', {
$http.get('/ajax/bundles.json').then(function(response) {
self.bundles = response.data;
});
this.bndUpd = function(bundleId){
$http.get('/ajax/bundles.json?id='+bundleId+'&act=upd').then(function(response){
for(var i=0; i < self.bundles.length; i++) {
if (self.bundles[i].id == bundleId) {
self.bundles[i] = response.data;
break;
}
}
});
};
}
});