Removing spaces at the end of lines and providing support for http.header substitution
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 May 2015 22:47:26 +0200
changeset 1834d4433058bf0c
parent 1833 8ece64bedc99
child 1835 9581e8765176
Removing spaces at the end of lines and providing support for http.header substitution
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Sun May 17 14:28:17 2015 +0200
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Wed May 27 22:47:26 2015 +0200
     1.3 @@ -70,7 +70,7 @@
     1.4  
     1.5  /**
     1.6   * Lightweight server to launch Bck2Brwsr applications and tests.
     1.7 - * Supports execution in native browser as well as Java's internal 
     1.8 + * Supports execution in native browser as well as Java's internal
     1.9   * execution engine.
    1.10   */
    1.11  abstract class BaseHTTPLauncher extends Launcher implements Closeable, Callable<HttpServer> {
    1.12 @@ -84,13 +84,13 @@
    1.13      private Object[] brwsr;
    1.14      private HttpServer server;
    1.15      private CountDownLatch wait;
    1.16 -    
    1.17 +
    1.18      public BaseHTTPLauncher(String cmd) {
    1.19          this.cmd = cmd;
    1.20          addClassLoader(BaseHTTPLauncher.class.getClassLoader());
    1.21          setTimeout(180000);
    1.22      }
    1.23 -    
    1.24 +
    1.25      @Override
    1.26      InvocationContext runMethod(InvocationContext c) throws IOException {
    1.27          loaders.add(c.clazz.getClassLoader());
    1.28 @@ -102,15 +102,15 @@
    1.29          }
    1.30          return c;
    1.31      }
    1.32 -    
    1.33 +
    1.34      public void setTimeout(long ms) {
    1.35          timeOut = ms;
    1.36      }
    1.37 -    
    1.38 +
    1.39      public void addClassLoader(ClassLoader url) {
    1.40          this.loaders.add(url);
    1.41      }
    1.42 -    
    1.43 +
    1.44      ClassLoader[] loaders() {
    1.45          return loaders.toArray(new ClassLoader[loaders.size()]);
    1.46      }
    1.47 @@ -169,7 +169,7 @@
    1.48              throw new IOException(ex);
    1.49          }
    1.50      }
    1.51 -    
    1.52 +
    1.53      private HttpServer initServer(String path, boolean addClasses, String vmPrefix) throws IOException {
    1.54          HttpServer s = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
    1.55          /*
    1.56 @@ -185,7 +185,7 @@
    1.57              nl.getTransport().setWorkerThreadPoolConfig(fewThreads);
    1.58              nl.getTransport().setKernelThreadPoolConfig(oneKernel);
    1.59          }
    1.60 -*/        
    1.61 +*/
    1.62          final ServerConfiguration conf = s.getServerConfiguration();
    1.63          VMAndPages vm = new VMAndPages();
    1.64          conf.addHttpHandler(vm, "/");
    1.65 @@ -210,13 +210,13 @@
    1.66          l.addHandler(ch);
    1.67          return s;
    1.68      }
    1.69 -    
    1.70 +
    1.71      private static int resourcesCount;
    1.72      private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
    1.73          wait = new CountDownLatch(1);
    1.74          server = initServer(".", true, "");
    1.75          final ServerConfiguration conf = server.getServerConfiguration();
    1.76 -        
    1.77 +
    1.78          class DynamicResourceHandler extends HttpHandler {
    1.79              private final InvocationContext ic;
    1.80              DynamicResourceHandler delegate;
    1.81 @@ -231,14 +231,14 @@
    1.82                  conf.removeHttpHandler(this);
    1.83                  delegate = del;
    1.84              }
    1.85 -            
    1.86 +
    1.87              @Override
    1.88              public void service(Request request, Response response) throws Exception {
    1.89                  if (delegate != null) {
    1.90                      delegate.service(request, response);
    1.91                      return;
    1.92                  }
    1.93 -                
    1.94 +
    1.95                  if ("/dynamic".equals(request.getRequestURI())) {
    1.96                      boolean webSocket = false;
    1.97                      String mimeType = request.getParameter("mimeType");
    1.98 @@ -269,7 +269,7 @@
    1.99                      response.getWriter().write("\n");
   1.100                      return;
   1.101                  }
   1.102 -                
   1.103 +
   1.104                  for (Resource r : ic.resources) {
   1.105                      if (r.httpPath.equals(request.getRequestURI())) {
   1.106                          LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
   1.107 @@ -296,6 +296,8 @@
   1.108                                              sb.append((char)ch);
   1.109                                          }
   1.110                                          params[i] = sb.toString();
   1.111 +                                    } else if (r.parameters[i].startsWith("http.header.")) {
   1.112 +                                        params[i] = request.getHeader(r.parameters[i].substring(12));
   1.113                                      }
   1.114                                  }
   1.115                                  if (params[i] == null) {
   1.116 @@ -303,12 +305,12 @@
   1.117                                  }
   1.118                              }
   1.119                          }
   1.120 -                        
   1.121 +
   1.122                          copyStream(r.httpContent, response.getOutputStream(), null, params);
   1.123                      }
   1.124                  }
   1.125              }
   1.126 -            
   1.127 +
   1.128              private URI registerWebSocket(Resource r) {
   1.129                  WebSocketEngine.getEngine().register("", r.httpPath, new WS(r));
   1.130                  return pageURL("ws", server, r.httpPath);
   1.131 @@ -322,9 +324,9 @@
   1.132                  return pageURL("http", server, r.httpPath);
   1.133              }
   1.134          }
   1.135 -        
   1.136 +
   1.137          conf.addHttpHandler(new Page(resources, harnessResource()), "/execute");
   1.138 -        
   1.139 +
   1.140          conf.addHttpHandler(new HttpHandler() {
   1.141              int cnt;
   1.142              List<InvocationContext> cases = new ArrayList<InvocationContext>();
   1.143 @@ -336,11 +338,11 @@
   1.144                  if (value != null && value.indexOf((char)0xC5) != -1) {
   1.145                      value = toUTF8(value);
   1.146                  }
   1.147 -                
   1.148 -                
   1.149 +
   1.150 +
   1.151                  InvocationContext mi = null;
   1.152                  int caseNmbr = -1;
   1.153 -                
   1.154 +
   1.155                  if (id != null && value != null) {
   1.156                      LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
   1.157                      value = decodeURL(value);
   1.158 @@ -358,7 +360,7 @@
   1.159                          caseNmbr = 0;
   1.160                      }
   1.161                  }
   1.162 -                
   1.163 +
   1.164                  if (mi == null) {
   1.165                      mi = methods.take();
   1.166                      caseNmbr = cnt++;
   1.167 @@ -376,7 +378,7 @@
   1.168                  }
   1.169                  prev = newRH;
   1.170                  conf.addHttpHandler(prev, "/dynamic");
   1.171 -                
   1.172 +
   1.173                  cases.add(mi);
   1.174                  final String cn = mi.clazz.getName();
   1.175                  final String mn = mi.methodName;
   1.176 @@ -397,7 +399,7 @@
   1.177  
   1.178          this.brwsr = launchServerAndBrwsr(server, "/execute");
   1.179      }
   1.180 -    
   1.181 +
   1.182      private static String encodeJSON(String in) {
   1.183          StringBuilder sb = new StringBuilder();
   1.184          for (int i = 0; i < in.length(); i++) {
   1.185 @@ -413,7 +415,7 @@
   1.186          }
   1.187          return sb.toString();
   1.188      }
   1.189 -    
   1.190 +
   1.191      @Override
   1.192      public void shutdown() throws IOException {
   1.193          methods.offer(END);
   1.194 @@ -428,20 +430,20 @@
   1.195              }
   1.196              if (prev == methods.size()) {
   1.197                  LOG.log(
   1.198 -                    Level.WARNING, 
   1.199 -                    "Timeout and no test has been executed meanwhile (at {0}). Giving up.", 
   1.200 +                    Level.WARNING,
   1.201 +                    "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
   1.202                      methods.size()
   1.203                  );
   1.204                  break;
   1.205              }
   1.206 -            LOG.log(Level.INFO, 
   1.207 -                "Timeout, but tests got from {0} to {1}. Trying again.", 
   1.208 +            LOG.log(Level.INFO,
   1.209 +                "Timeout, but tests got from {0} to {1}. Trying again.",
   1.210                  new Object[]{prev, methods.size()}
   1.211              );
   1.212          }
   1.213          stopServerAndBrwsr(server, brwsr);
   1.214      }
   1.215 -    
   1.216 +
   1.217      static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
   1.218          for (;;) {
   1.219              int ch = is.read();
   1.220 @@ -478,7 +480,7 @@
   1.221          }
   1.222          return new String(arr, "UTF-8");
   1.223      }
   1.224 -    
   1.225 +
   1.226      private static String decodeURL(String s) {
   1.227          for (;;) {
   1.228              int pos = s.indexOf('%');
   1.229 @@ -489,13 +491,13 @@
   1.230              s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
   1.231          }
   1.232      }
   1.233 -    
   1.234 +
   1.235      private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
   1.236          if (brwsr == null) {
   1.237              return;
   1.238          }
   1.239          Process process = (Process)brwsr[0];
   1.240 -        
   1.241 +
   1.242          server.stop();
   1.243          InputStream stdout = process.getInputStream();
   1.244          InputStream stderr = process.getErrorStream();
   1.245 @@ -512,7 +514,7 @@
   1.246  
   1.247          deleteTree((File)brwsr[1]);
   1.248      }
   1.249 -    
   1.250 +
   1.251      private static void drain(String name, InputStream is) throws IOException {
   1.252          int av = is.available();
   1.253          if (av > 0) {
   1.254 @@ -543,7 +545,7 @@
   1.255      public HttpServer call() throws Exception {
   1.256          return server;
   1.257      }
   1.258 -    
   1.259 +
   1.260      @Override
   1.261      public void close() throws IOException {
   1.262          shutdown();
   1.263 @@ -571,7 +573,7 @@
   1.264          }
   1.265          {
   1.266              String cmdName = cmd == null ? "xdg-open" : cmd;
   1.267 -            String[] cmdArr = { 
   1.268 +            String[] cmdArr = {
   1.269                  cmdName, uri.toString()
   1.270              };
   1.271              LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
   1.272 @@ -601,7 +603,7 @@
   1.273  
   1.274      final class Res {
   1.275          private final Set<URL> ignore = new HashSet<URL>();
   1.276 -        
   1.277 +
   1.278          Object compileJar(URL jarURL) throws IOException {
   1.279              List<String[]> libraries = new ArrayList<String[]>();
   1.280              for (ClassLoader loader : loaders) {
   1.281 @@ -612,12 +614,12 @@
   1.282                      for (Map.Entry<String, Attributes> entrySet : mf.getEntries().entrySet()) {
   1.283                          String key = entrySet.getKey();
   1.284                          Attributes attr = entrySet.getValue();
   1.285 -                        
   1.286 +
   1.287                          final String a = attr.getValue("Bck2BrwsrArtifactId");
   1.288                          final String g = attr.getValue("Bck2BrwsrGroupId");
   1.289                          final String v = attr.getValue("Bck2BrwsrVersion");
   1.290                          final String d = attr.getValue("Bck2BrwsrDebug");
   1.291 -                        
   1.292 +
   1.293                          if (g != null && a != null && v != null && "true".equals(d)) {
   1.294                              libraries.add(new String[] {
   1.295                                  a, g, v, key
   1.296 @@ -707,7 +709,7 @@
   1.297          final String resource;
   1.298          private final String[] args;
   1.299          private final Res res;
   1.300 -        
   1.301 +
   1.302          public Page(Res res, String resource, String... args) {
   1.303              this.res = res;
   1.304              this.resource = resource;
   1.305 @@ -732,7 +734,7 @@
   1.306                  replace = args;
   1.307              }
   1.308              OutputStream os = response.getOutputStream();
   1.309 -            try { 
   1.310 +            try {
   1.311                  InputStream is = res.get(r, 0).openStream();
   1.312                  copyStream(is, os, request.getRequestURL().toString(), replace);
   1.313              } catch (IOException ex) {
   1.314 @@ -750,7 +752,7 @@
   1.315              return r;
   1.316          }
   1.317      }
   1.318 -    
   1.319 +
   1.320      private static class SubTree extends Page {
   1.321  
   1.322          public SubTree(Res res, String resource, String... args) {
   1.323 @@ -761,17 +763,17 @@
   1.324          protected String computePage(Request request) {
   1.325              return resource + request.getHttpHandlerPath();
   1.326          }
   1.327 -        
   1.328 -        
   1.329 +
   1.330 +
   1.331      }
   1.332  
   1.333      private class VMAndPages extends StaticHttpHandler {
   1.334          private String vmResource;
   1.335 -        
   1.336 +
   1.337          public VMAndPages() {
   1.338              super((String[]) null);
   1.339          }
   1.340 -        
   1.341 +
   1.342          @Override
   1.343          public void service(Request request, Response response) throws Exception {
   1.344              if (request.getRequestURI().equals(vmResource)) {
   1.345 @@ -831,9 +833,9 @@
   1.346                      final String filePart = url.getFile();
   1.347                      if (filePart.endsWith(res)) {
   1.348                          url = new URL(
   1.349 -                            url.getProtocol(), 
   1.350 -                            url.getHost(), 
   1.351 -                            url.getPort(), 
   1.352 +                            url.getProtocol(),
   1.353 +                            url.getHost(),
   1.354 +                            url.getPort(),
   1.355                              filePart.substring(0, filePart.length() - res.length())
   1.356                          );
   1.357                      }