# HG changeset patch # User Jaroslav Tulach # Date 1372076297 -7200 # Node ID 4d324bf6ede9de1d1b6fb45bc201da114fe6651a # Parent ce6327094088a4ee53f61b54d5141b77cd7f3f1f No need for register method when we can do it via the HTTP diff -r ce6327094088 -r 4d324bf6ede9 launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java --- a/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Mon Jun 24 14:17:42 2013 +0200 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Mon Jun 24 14:18:17 2013 +0200 @@ -96,32 +96,6 @@ 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; final String httpType; diff -r ce6327094088 -r 4d324bf6ede9 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Mon Jun 24 14:17:42 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Mon Jun 24 14:18:17 2013 +0200 @@ -17,6 +17,8 @@ */ package org.apidesign.bck2brwsr.launcher; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.File; import java.io.IOException; @@ -44,6 +46,7 @@ import java.util.logging.Logger; import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource; import org.glassfish.grizzly.PortRange; +import org.glassfish.grizzly.http.Method; import org.glassfish.grizzly.http.server.HttpHandler; import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.grizzly.http.server.NetworkListener; @@ -177,23 +180,41 @@ server = initServer(".", true); final ServerConfiguration conf = server.getServerConfiguration(); - class DynamicResourceHandler extends HttpHandler implements InvocationContext.RegisterResource { + class DynamicResourceHandler extends HttpHandler { private final InvocationContext ic; + private int resourcesCount; public DynamicResourceHandler(InvocationContext ic) { 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 public void service(Request request, Response response) throws Exception { + if ("/dynamic".equals(request.getRequestURI())) { + String mimeType = request.getParameter("mimeType"); + List params = new ArrayList(); + for (int i = 0; ; i++) { + String p = request.getParameter("param" + i); + if (p == null) { + break; + } + params.add(p); + } + final String cnt = request.getParameter("content"); + String mangle = cnt.replace("%20", " ").replace("%0A", "\n"); + ByteArrayInputStream is = new ByteArrayInputStream(mangle.getBytes("UTF-8")); + URI url = registerResource(new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()]))); + response.getWriter().write(url.toString()); + response.getWriter().write("\n"); + return; + } + for (Resource r : ic.resources) { if (r.httpPath.equals(request.getRequestURI())) { LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI()); @@ -231,8 +252,7 @@ } } - @Override - public URI registerResource(Resource r) { + private URI registerResource(Resource r) { if (!ic.resources.contains(r)) { ic.resources.add(r); conf.addHttpHandler(this, r.httpPath); @@ -295,6 +315,7 @@ } prev = new DynamicResourceHandler(mi); + conf.addHttpHandler(prev, "/dynamic"); cases.add(mi); final String cn = mi.clazz.getName();