1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import com.sun.net.httpserver.*;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.OutputStream;
  29 import java.net.InetSocketAddress;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 
  34 /**
  35  * Extremely simple server that only performs one task.  The server listens for
  36  * requests on the ephemeral port.  If it sees a request that begins with
  37  * "/multi-release.jar", it consumes the request and returns a stream of bytes
  38  * representing the jar file multi-release.jar found in "userdir".
  39  */
  40 class SimpleHttpServer {
  41     private static final String userdir = System.getProperty("user.dir", ".");
  42     private static final Path multirelease = Paths.get(userdir, "multi-release.jar");
  43 
  44     private final HttpServer server;
  45 
  46     public SimpleHttpServer() throws IOException {
  47         server = HttpServer.create();
  48     }
  49 
  50     public void start() throws IOException {
  51         server.bind(new InetSocketAddress(0), 0);
  52         server.createContext("/multi-release.jar", t -> {
  53             try (InputStream is = t.getRequestBody()) {
  54                 is.readAllBytes();  // probably not necessary to consume request
  55                 byte[] bytes = Files.readAllBytes(multirelease);
  56                 t.sendResponseHeaders(200, bytes.length);
  57                 try (OutputStream os = t.getResponseBody()) {
  58                     os.write(bytes);
  59                 }
  60             }
  61         });
  62         server.setExecutor(null); // creates a default executor
  63         server.start();
  64     }
  65 
  66     public void stop() {
  67         server.stop(0);
  68     }
  69 
  70     int getPort() {
  71         return server.getAddress().getPort();
  72     }
  73 }
  74