# HG changeset patch # User Jaroslav Tulach # Date 1371846849 -7200 # Node ID 265edcee24ed4cea0e0316785ab30f4a22f519ce # Parent be346bd5a46db4eae4fa0ed8d83ab289c0e2e453 Dynamic way of registering Http resources diff -r be346bd5a46d -r 265edcee24ed launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java --- a/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Fri Jun 21 15:21:09 2013 +0200 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Fri Jun 21 22:34:09 2013 +0200 @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -95,6 +96,31 @@ wait.countDown(); } + private static RegisterResource RR; + static void register(RegisterResource rr) { + RR = rr; + } + /** A running {@link InvocationContext test} can register additional + * resources to the associated browser (if any). + * + * @param content the stream to deliver content from + * @param mimeType mime type for the HTTP reply + * @param path suggested path in the server to use + * @param parameters additional parameters + * @return the URI the resource is made available at + * @throws NullPointerException if there is no {@link InvocationContext test} + * currently running + * @since 0.8 + */ + public static URI register( + InputStream content, String mimeType, String path, String[] parameters + ) { + final Resource r = new Resource(content, mimeType, path, parameters); + return RR.registerResource(r); + } + static interface RegisterResource { + public URI registerResource(Resource r); + } static final class Resource { final InputStream httpContent; diff -r be346bd5a46d -r 265edcee24ed launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Fri Jun 21 15:21:09 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Fri Jun 21 22:34:09 2013 +0200 @@ -177,20 +177,19 @@ server = initServer(".", true); final ServerConfiguration conf = server.getServerConfiguration(); - class DynamicResourceHandler extends HttpHandler { + class DynamicResourceHandler extends HttpHandler implements InvocationContext.RegisterResource { private final InvocationContext ic; public DynamicResourceHandler(InvocationContext ic) { - if (ic == null || ic.resources.isEmpty()) { - throw new NullPointerException(); - } this.ic = ic; for (Resource r : ic.resources) { conf.addHttpHandler(this, r.httpPath); } + InvocationContext.register(this); } public void close() { conf.removeHttpHandler(this); + InvocationContext.register(null); } @Override @@ -231,6 +230,15 @@ } } } + + @Override + public URI registerResource(Resource r) { + if (!ic.resources.contains(r)) { + ic.resources.add(r); + conf.addHttpHandler(this, r.httpPath); + } + return pageURL(server, r.httpPath); + } } conf.addHttpHandler(new Page(resources, harnessResource()), "/execute"); @@ -286,9 +294,7 @@ return; } - if (!mi.resources.isEmpty()) { - prev = new DynamicResourceHandler(mi); - } + prev = new DynamicResourceHandler(mi); cases.add(mi); final String cn = mi.clazz.getName(); @@ -381,10 +387,7 @@ private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException { server.start(); - NetworkListener listener = server.getListeners().iterator().next(); - int port = listener.getPort(); - - URI uri = new URI("http://localhost:" + port + page); + URI uri = pageURL(server, page); return showBrwsr(uri); } private static String toUTF8(String value) throws UnsupportedEncodingException { @@ -496,6 +499,16 @@ abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException; abstract String harnessResource(); + private static URI pageURL(HttpServer server, final String page) { + NetworkListener listener = server.getListeners().iterator().next(); + int port = listener.getPort(); + try { + return new URI("http://localhost:" + port + page); + } catch (URISyntaxException ex) { + throw new IllegalStateException(ex); + } + } + class Res { public InputStream get(String resource) throws IOException { URL u = null;