< prev index next >

test/jdk/java/net/httpclient/LightWeightHttpServer.java

Print this page




  63     static String httpsroot;
  64     static ProxyServer proxy;
  65     static int proxyPort;
  66     static RedirectErrorHandler redirectErrorHandler, redirectErrorHandlerSecure;
  67     static RedirectHandler redirectHandler, redirectHandlerSecure;
  68     static DelayHandler delayHandler;
  69     static final String midSizedFilename = "/files/notsobigfile.txt";
  70     static final String smallFilename = "/files/smallfile.txt";
  71     static Path midSizedFile;
  72     static Path smallFile;
  73     static String fileroot;
  74 
  75     public static void initServer() throws IOException {
  76 
  77         Logger logger = Logger.getLogger("com.sun.net.httpserver");
  78         ConsoleHandler ch = new ConsoleHandler();
  79         logger.setLevel(Level.ALL);
  80         ch.setLevel(Level.ALL);
  81         logger.addHandler(ch);
  82 
  83         String root = System.getProperty("test.src") + "/docs";
  84         InetSocketAddress addr = new InetSocketAddress(0);
  85         httpServer = HttpServer.create(addr, 0);
  86         if (httpServer instanceof HttpsServer) {
  87             throw new RuntimeException("should not be httpsserver");
  88         }
  89         httpsServer = HttpsServer.create(addr, 0);
  90         HttpHandler h = new FileServerHandler(root);
  91 
  92         HttpContext c1 = httpServer.createContext("/files", h);
  93         HttpContext c2 = httpsServer.createContext("/files", h);
  94         HttpContext c3 = httpServer.createContext("/echo", new EchoHandler());
  95         redirectHandler = new RedirectHandler("/redirect");
  96         redirectHandlerSecure = new RedirectHandler("/redirect");
  97         HttpContext c4 = httpServer.createContext("/redirect", redirectHandler);
  98         HttpContext c41 = httpsServer.createContext("/redirect", redirectHandlerSecure);
  99         HttpContext c5 = httpsServer.createContext("/echo", new EchoHandler());
 100         HttpContext c6 = httpServer.createContext("/keepalive", new KeepAliveHandler());
 101         redirectErrorHandler = new RedirectErrorHandler("/redirecterror");
 102         redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror");
 103         HttpContext c7 = httpServer.createContext("/redirecterror", redirectErrorHandler);


 284             t.close();
 285         }
 286     }
 287 
 288     static class DelayHandler implements HttpHandler {
 289 
 290         CyclicBarrier bar1 = new CyclicBarrier(2);
 291         CyclicBarrier bar2 = new CyclicBarrier(2);
 292         CyclicBarrier bar3 = new CyclicBarrier(2);
 293 
 294         CyclicBarrier barrier1() {
 295             return bar1;
 296         }
 297 
 298         CyclicBarrier barrier2() {
 299             return bar2;
 300         }
 301 
 302         @Override
 303         public synchronized void handle(HttpExchange he) throws IOException {
 304             byte[] buf = Util.readAll(he.getRequestBody());
 305             try {
 306                 bar1.await();
 307                 bar2.await();
 308             } catch (InterruptedException | BrokenBarrierException e) {

 309             }
 310             he.sendResponseHeaders(200, -1); // will probably fail
 311             he.close();
 312         }
 313     }
 314 }


  63     static String httpsroot;
  64     static ProxyServer proxy;
  65     static int proxyPort;
  66     static RedirectErrorHandler redirectErrorHandler, redirectErrorHandlerSecure;
  67     static RedirectHandler redirectHandler, redirectHandlerSecure;
  68     static DelayHandler delayHandler;
  69     static final String midSizedFilename = "/files/notsobigfile.txt";
  70     static final String smallFilename = "/files/smallfile.txt";
  71     static Path midSizedFile;
  72     static Path smallFile;
  73     static String fileroot;
  74 
  75     public static void initServer() throws IOException {
  76 
  77         Logger logger = Logger.getLogger("com.sun.net.httpserver");
  78         ConsoleHandler ch = new ConsoleHandler();
  79         logger.setLevel(Level.ALL);
  80         ch.setLevel(Level.ALL);
  81         logger.addHandler(ch);
  82 
  83         String root = System.getProperty("test.src", ".") + "/docs";
  84         InetSocketAddress addr = new InetSocketAddress(0);
  85         httpServer = HttpServer.create(addr, 0);
  86         if (httpServer instanceof HttpsServer) {
  87             throw new RuntimeException("should not be httpsserver");
  88         }
  89         httpsServer = HttpsServer.create(addr, 0);
  90         HttpHandler h = new FileServerHandler(root);
  91 
  92         HttpContext c1 = httpServer.createContext("/files", h);
  93         HttpContext c2 = httpsServer.createContext("/files", h);
  94         HttpContext c3 = httpServer.createContext("/echo", new EchoHandler());
  95         redirectHandler = new RedirectHandler("/redirect");
  96         redirectHandlerSecure = new RedirectHandler("/redirect");
  97         HttpContext c4 = httpServer.createContext("/redirect", redirectHandler);
  98         HttpContext c41 = httpsServer.createContext("/redirect", redirectHandlerSecure);
  99         HttpContext c5 = httpsServer.createContext("/echo", new EchoHandler());
 100         HttpContext c6 = httpServer.createContext("/keepalive", new KeepAliveHandler());
 101         redirectErrorHandler = new RedirectErrorHandler("/redirecterror");
 102         redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror");
 103         HttpContext c7 = httpServer.createContext("/redirecterror", redirectErrorHandler);


 284             t.close();
 285         }
 286     }
 287 
 288     static class DelayHandler implements HttpHandler {
 289 
 290         CyclicBarrier bar1 = new CyclicBarrier(2);
 291         CyclicBarrier bar2 = new CyclicBarrier(2);
 292         CyclicBarrier bar3 = new CyclicBarrier(2);
 293 
 294         CyclicBarrier barrier1() {
 295             return bar1;
 296         }
 297 
 298         CyclicBarrier barrier2() {
 299             return bar2;
 300         }
 301 
 302         @Override
 303         public synchronized void handle(HttpExchange he) throws IOException {
 304             try(InputStream is = he.getRequestBody()) {
 305                 is.readAllBytes();
 306                 bar1.await();
 307                 bar2.await();
 308             } catch (InterruptedException | BrokenBarrierException e) {
 309                 throw new IOException(e);
 310             }
 311             he.sendResponseHeaders(200, -1); // will probably fail
 312             he.close();
 313         }
 314     }
 315 }
< prev index next >