launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
branchclassloader
changeset 1188 4d324bf6ede9
parent 1186 265edcee24ed
child 1235 36614e9273e9
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Fri Jun 21 22:34:09 2013 +0200
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Mon Jun 24 14:18:17 2013 +0200
     1.3 @@ -17,6 +17,8 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.launcher;
     1.6  
     1.7 +import java.io.BufferedReader;
     1.8 +import java.io.ByteArrayInputStream;
     1.9  import java.io.Closeable;
    1.10  import java.io.File;
    1.11  import java.io.IOException;
    1.12 @@ -44,6 +46,7 @@
    1.13  import java.util.logging.Logger;
    1.14  import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
    1.15  import org.glassfish.grizzly.PortRange;
    1.16 +import org.glassfish.grizzly.http.Method;
    1.17  import org.glassfish.grizzly.http.server.HttpHandler;
    1.18  import org.glassfish.grizzly.http.server.HttpServer;
    1.19  import org.glassfish.grizzly.http.server.NetworkListener;
    1.20 @@ -177,23 +180,41 @@
    1.21          server = initServer(".", true);
    1.22          final ServerConfiguration conf = server.getServerConfiguration();
    1.23          
    1.24 -        class DynamicResourceHandler extends HttpHandler implements InvocationContext.RegisterResource {
    1.25 +        class DynamicResourceHandler extends HttpHandler {
    1.26              private final InvocationContext ic;
    1.27 +            private int resourcesCount;
    1.28              public DynamicResourceHandler(InvocationContext ic) {
    1.29                  this.ic = ic;
    1.30                  for (Resource r : ic.resources) {
    1.31                      conf.addHttpHandler(this, r.httpPath);
    1.32                  }
    1.33 -                InvocationContext.register(this);
    1.34              }
    1.35  
    1.36              public void close() {
    1.37                  conf.removeHttpHandler(this);
    1.38 -                InvocationContext.register(null);
    1.39              }
    1.40              
    1.41              @Override
    1.42              public void service(Request request, Response response) throws Exception {
    1.43 +                if ("/dynamic".equals(request.getRequestURI())) {
    1.44 +                    String mimeType = request.getParameter("mimeType");
    1.45 +                    List<String> params = new ArrayList<String>();
    1.46 +                    for (int i = 0; ; i++) {
    1.47 +                        String p = request.getParameter("param" + i);
    1.48 +                        if (p == null) {
    1.49 +                            break;
    1.50 +                        }
    1.51 +                        params.add(p);
    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 +                    response.getWriter().write(url.toString());
    1.58 +                    response.getWriter().write("\n");
    1.59 +                    return;
    1.60 +                }
    1.61 +                
    1.62                  for (Resource r : ic.resources) {
    1.63                      if (r.httpPath.equals(request.getRequestURI())) {
    1.64                          LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
    1.65 @@ -231,8 +252,7 @@
    1.66                  }
    1.67              }
    1.68  
    1.69 -            @Override
    1.70 -            public URI registerResource(Resource r) {
    1.71 +            private URI registerResource(Resource r) {
    1.72                  if (!ic.resources.contains(r)) {
    1.73                      ic.resources.add(r);
    1.74                      conf.addHttpHandler(this, r.httpPath);
    1.75 @@ -295,6 +315,7 @@
    1.76                  }
    1.77                  
    1.78                  prev = new DynamicResourceHandler(mi);
    1.79 +                conf.addHttpHandler(prev, "/dynamic");
    1.80                  
    1.81                  cases.add(mi);
    1.82                  final String cn = mi.clazz.getName();