No need for register method when we can do it via the HTTP classloader
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Jun 2013 14:18:17 +0200
branchclassloader
changeset 11884d324bf6ede9
parent 1187 ce6327094088
child 1224 7189e9abb928
No need for register method when we can do it via the HTTP
launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
     1.1 --- a/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java	Mon Jun 24 14:17:42 2013 +0200
     1.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java	Mon Jun 24 14:18:17 2013 +0200
     1.3 @@ -96,32 +96,6 @@
     1.4          wait.countDown();
     1.5      }
     1.6  
     1.7 -    private static RegisterResource RR;
     1.8 -    static void register(RegisterResource rr) {
     1.9 -        RR = rr;
    1.10 -    }
    1.11 -    /** A running {@link InvocationContext test} can register additional 
    1.12 -     * resources to the associated browser (if any). 
    1.13 -     * 
    1.14 -     * @param content the stream to deliver content from
    1.15 -     * @param mimeType mime type for the HTTP reply
    1.16 -     * @param path suggested path in the server to use
    1.17 -     * @param parameters additional parameters
    1.18 -     * @return the URI the resource is made available at
    1.19 -     * @throws NullPointerException if there is no {@link InvocationContext test} 
    1.20 -     *    currently running
    1.21 -     * @since 0.8
    1.22 -     */
    1.23 -    public static URI register(
    1.24 -        InputStream content, String mimeType, String path, String[] parameters
    1.25 -    ) {
    1.26 -        final Resource r = new Resource(content, mimeType, path, parameters);
    1.27 -        return RR.registerResource(r);
    1.28 -    }
    1.29 -    static interface RegisterResource {
    1.30 -        public URI registerResource(Resource r);
    1.31 -    }
    1.32 -
    1.33      static final class Resource {
    1.34          final InputStream httpContent;
    1.35          final String httpType;
     2.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Mon Jun 24 14:17:42 2013 +0200
     2.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Mon Jun 24 14:18:17 2013 +0200
     2.3 @@ -17,6 +17,8 @@
     2.4   */
     2.5  package org.apidesign.bck2brwsr.launcher;
     2.6  
     2.7 +import java.io.BufferedReader;
     2.8 +import java.io.ByteArrayInputStream;
     2.9  import java.io.Closeable;
    2.10  import java.io.File;
    2.11  import java.io.IOException;
    2.12 @@ -44,6 +46,7 @@
    2.13  import java.util.logging.Logger;
    2.14  import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
    2.15  import org.glassfish.grizzly.PortRange;
    2.16 +import org.glassfish.grizzly.http.Method;
    2.17  import org.glassfish.grizzly.http.server.HttpHandler;
    2.18  import org.glassfish.grizzly.http.server.HttpServer;
    2.19  import org.glassfish.grizzly.http.server.NetworkListener;
    2.20 @@ -177,23 +180,41 @@
    2.21          server = initServer(".", true);
    2.22          final ServerConfiguration conf = server.getServerConfiguration();
    2.23          
    2.24 -        class DynamicResourceHandler extends HttpHandler implements InvocationContext.RegisterResource {
    2.25 +        class DynamicResourceHandler extends HttpHandler {
    2.26              private final InvocationContext ic;
    2.27 +            private int resourcesCount;
    2.28              public DynamicResourceHandler(InvocationContext ic) {
    2.29                  this.ic = ic;
    2.30                  for (Resource r : ic.resources) {
    2.31                      conf.addHttpHandler(this, r.httpPath);
    2.32                  }
    2.33 -                InvocationContext.register(this);
    2.34              }
    2.35  
    2.36              public void close() {
    2.37                  conf.removeHttpHandler(this);
    2.38 -                InvocationContext.register(null);
    2.39              }
    2.40              
    2.41              @Override
    2.42              public void service(Request request, Response response) throws Exception {
    2.43 +                if ("/dynamic".equals(request.getRequestURI())) {
    2.44 +                    String mimeType = request.getParameter("mimeType");
    2.45 +                    List<String> params = new ArrayList<String>();
    2.46 +                    for (int i = 0; ; i++) {
    2.47 +                        String p = request.getParameter("param" + i);
    2.48 +                        if (p == null) {
    2.49 +                            break;
    2.50 +                        }
    2.51 +                        params.add(p);
    2.52 +                    }
    2.53 +                    final String cnt = request.getParameter("content");
    2.54 +                    String mangle = cnt.replace("%20", " ").replace("%0A", "\n");
    2.55 +                    ByteArrayInputStream is = new ByteArrayInputStream(mangle.getBytes("UTF-8"));
    2.56 +                    URI url = registerResource(new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()])));
    2.57 +                    response.getWriter().write(url.toString());
    2.58 +                    response.getWriter().write("\n");
    2.59 +                    return;
    2.60 +                }
    2.61 +                
    2.62                  for (Resource r : ic.resources) {
    2.63                      if (r.httpPath.equals(request.getRequestURI())) {
    2.64                          LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
    2.65 @@ -231,8 +252,7 @@
    2.66                  }
    2.67              }
    2.68  
    2.69 -            @Override
    2.70 -            public URI registerResource(Resource r) {
    2.71 +            private URI registerResource(Resource r) {
    2.72                  if (!ic.resources.contains(r)) {
    2.73                      ic.resources.add(r);
    2.74                      conf.addHttpHandler(this, r.httpPath);
    2.75 @@ -295,6 +315,7 @@
    2.76                  }
    2.77                  
    2.78                  prev = new DynamicResourceHandler(mi);
    2.79 +                conf.addHttpHandler(prev, "/dynamic");
    2.80                  
    2.81                  cases.add(mi);
    2.82                  final String cn = mi.clazz.getName();