Archived
0

WebInterface: static page

This commit is contained in:
2016-12-05 15:25:25 +03:00
parent f4a259aecc
commit c3f7d75546
4 changed files with 140 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-12-05
*/
package asys.webinterface;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
private WebServer webServer;
@Override
public void start(BundleContext context) throws Exception {
webServer = new WebServer();
webServer.start(8778);
}
@Override
public void stop(BundleContext context) throws Exception {
webServer.stop();
}
}

View File

@@ -0,0 +1,26 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-12-05
*/
package asys.webinterface;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
public class IndexHandler implements HttpHandler {
private static final Charset defaultCharset = Charset.forName("UTF-8");
@Override
public void handle(HttpExchange httpExchange) throws IOException {
String plainText = "ASys Webinterface";
httpExchange.sendResponseHeaders(200, plainText.length());
httpExchange.setAttribute("Context-Type", "text/plain;charset=utf-8");
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(plainText.getBytes(defaultCharset));
responseBody.close();
}
}

View File

@@ -0,0 +1,24 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-12-05
*/
package asys.webinterface;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
public class WebServer {
private HttpServer server;
public void start(int port) throws IOException {
server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", new IndexHandler());
server.start();
}
public void stop() {
server.stop(0);
}
}