1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Thu Jun 27 10:05:00 2013 +0200
1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Mon Aug 26 08:56:37 2013 +0200
1.3 @@ -18,6 +18,7 @@
1.4 package org.apidesign.bck2brwsr.launcher;
1.5
1.6 import java.io.ByteArrayInputStream;
1.7 +import java.io.ByteArrayOutputStream;
1.8 import java.io.Closeable;
1.9 import java.io.File;
1.10 import java.io.IOException;
1.11 @@ -53,6 +54,11 @@
1.12 import org.glassfish.grizzly.http.server.ServerConfiguration;
1.13 import org.glassfish.grizzly.http.util.HttpStatus;
1.14 import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
1.15 +import org.glassfish.grizzly.websockets.WebSocket;
1.16 +import org.glassfish.grizzly.websockets.WebSocketAddOn;
1.17 +import org.glassfish.grizzly.websockets.WebSocketApplication;
1.18 +import org.glassfish.grizzly.websockets.WebSocketEngine;
1.19 +import org.openide.util.Exceptions;
1.20
1.21 /**
1.22 * Lightweight server to launch Bck2Brwsr applications and tests.
1.23 @@ -151,7 +157,11 @@
1.24
1.25 private HttpServer initServer(String path, boolean addClasses) throws IOException {
1.26 HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
1.27 -
1.28 + final WebSocketAddOn addon = new WebSocketAddOn();
1.29 + for (NetworkListener listener : s.getListeners()) {
1.30 + listener.registerAddOn(addon);
1.31 + }
1.32 +
1.33 ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy().
1.34 setPoolName("Fx/Bck2 Brwsr").
1.35 setCorePoolSize(1).
1.36 @@ -202,6 +212,7 @@
1.37 }
1.38
1.39 if ("/dynamic".equals(request.getRequestURI())) {
1.40 + boolean webSocket = false;
1.41 String mimeType = request.getParameter("mimeType");
1.42 List<String> params = new ArrayList<String>();
1.43 for (int i = 0; ; i++) {
1.44 @@ -210,11 +221,20 @@
1.45 break;
1.46 }
1.47 params.add(p);
1.48 - }
1.49 + if ("protocol:ws".equals(p)) {
1.50 + webSocket = true;
1.51 + continue;
1.52 + } }
1.53 final String cnt = request.getParameter("content");
1.54 String mangle = cnt.replace("%20", " ").replace("%0A", "\n");
1.55 ByteArrayInputStream is = new ByteArrayInputStream(mangle.getBytes("UTF-8"));
1.56 - URI url = registerResource(new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()])));
1.57 + URI url;
1.58 + final Resource res = new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()]));
1.59 + if (webSocket) {
1.60 + url = registerWebSocket(res);
1.61 + } else {
1.62 + url = registerResource(res);
1.63 + }
1.64 response.getWriter().write(url.toString());
1.65 response.getWriter().write("\n");
1.66 return;
1.67 @@ -256,13 +276,18 @@
1.68 }
1.69 }
1.70 }
1.71 +
1.72 + private URI registerWebSocket(Resource r) {
1.73 + WebSocketEngine.getEngine().register("", r.httpPath, new WS(r));
1.74 + return pageURL("ws", server, r.httpPath);
1.75 + }
1.76
1.77 private URI registerResource(Resource r) {
1.78 if (!ic.resources.contains(r)) {
1.79 ic.resources.add(r);
1.80 conf.addHttpHandler(this, r.httpPath);
1.81 }
1.82 - return pageURL(server, r.httpPath);
1.83 + return pageURL("http", server, r.httpPath);
1.84 }
1.85 }
1.86
1.87 @@ -411,7 +436,7 @@
1.88
1.89 private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
1.90 server.start();
1.91 - URI uri = pageURL(server, page);
1.92 + URI uri = pageURL("http", server, page);
1.93 return showBrwsr(uri);
1.94 }
1.95 private static String toUTF8(String value) throws UnsupportedEncodingException {
1.96 @@ -523,11 +548,11 @@
1.97 abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
1.98 abstract String harnessResource();
1.99
1.100 - private static URI pageURL(HttpServer server, final String page) {
1.101 + private static URI pageURL(String protocol, HttpServer server, final String page) {
1.102 NetworkListener listener = server.getListeners().iterator().next();
1.103 int port = listener.getPort();
1.104 try {
1.105 - return new URI("http://localhost:" + port + page);
1.106 + return new URI(protocol + "://localhost:" + port + page);
1.107 } catch (URISyntaxException ex) {
1.108 throw new IllegalStateException(ex);
1.109 }
1.110 @@ -675,4 +700,25 @@
1.111 }
1.112 }
1.113 }
1.114 -}
1.115 + private static class WS extends WebSocketApplication {
1.116 +
1.117 + private final Resource r;
1.118 +
1.119 + private WS(Resource r) {
1.120 + this.r = r;
1.121 + }
1.122 +
1.123 + @Override
1.124 + public void onMessage(WebSocket socket, String text) {
1.125 + try {
1.126 + r.httpContent.reset();
1.127 + ByteArrayOutputStream out = new ByteArrayOutputStream();
1.128 + copyStream(r.httpContent, out, null, text);
1.129 + String s = new String(out.toByteArray(), "UTF-8");
1.130 + socket.send(s);
1.131 + } catch (IOException ex) {
1.132 + Exceptions.printStackTrace(ex);
1.133 + }
1.134 + }
1.135 +
1.136 + }}