#4858: Let the server provide all resources around the startpage model
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 03 Apr 2013 18:23:49 +0200
branchmodel
changeset 924a85e27899a80
parent 923 c8ddf2e0c169
child 925 b486f65ac4f5
#4858: Let the server provide all resources around the startpage
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
     1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Wed Apr 03 18:21:42 2013 +0200
     1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Wed Apr 03 18:23:49 2013 +0200
     1.3 @@ -98,9 +98,9 @@
     1.4          }
     1.5          HttpServer s = initServer(".", true);
     1.6          int last = startpage.lastIndexOf('/');
     1.7 +        String prefix = startpage.substring(0, last);
     1.8          String simpleName = startpage.substring(last);
     1.9 -        s.getServerConfiguration().addHttpHandler(new Page(resources, startpage), simpleName);
    1.10 -        s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
    1.11 +        s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
    1.12          try {
    1.13              launchServerAndBrwsr(s, simpleName);
    1.14          } catch (URISyntaxException | InterruptedException ex) {
    1.15 @@ -454,7 +454,7 @@
    1.16      }
    1.17  
    1.18      private static class Page extends HttpHandler {
    1.19 -        private final String resource;
    1.20 +        final String resource;
    1.21          private final String[] args;
    1.22          private final Res res;
    1.23          
    1.24 @@ -466,10 +466,7 @@
    1.25  
    1.26          @Override
    1.27          public void service(Request request, Response response) throws Exception {
    1.28 -            String r = resource;
    1.29 -            if (r == null) {
    1.30 -                r = request.getHttpHandlerPath();
    1.31 -            }
    1.32 +            String r = computePage(request);
    1.33              if (r.startsWith("/")) {
    1.34                  r = r.substring(1);
    1.35              }
    1.36 @@ -493,6 +490,28 @@
    1.37                  response.setStatus(404);
    1.38              }
    1.39          }
    1.40 +
    1.41 +        protected String computePage(Request request) {
    1.42 +            String r = resource;
    1.43 +            if (r == null) {
    1.44 +                r = request.getHttpHandlerPath();
    1.45 +            }
    1.46 +            return r;
    1.47 +        }
    1.48 +    }
    1.49 +    
    1.50 +    private static class SubTree extends Page {
    1.51 +
    1.52 +        public SubTree(Res res, String resource, String... args) {
    1.53 +            super(res, resource, args);
    1.54 +        }
    1.55 +
    1.56 +        @Override
    1.57 +        protected String computePage(Request request) {
    1.58 +            return resource + request.getHttpHandlerPath();
    1.59 +        }
    1.60 +        
    1.61 +        
    1.62      }
    1.63  
    1.64      private static class VM extends HttpHandler {