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 | 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 package jdk.test.lib.util; 25 26 import com.sun.net.httpserver.*; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.io.OutputStream; 31 import java.net.InetSocketAddress; 32 import java.nio.file.Files; 33 import java.nio.file.Path; 34 import java.nio.file.Paths; 35 36 /** 37 * Extremely simple server that only performs one task. The server listens for 38 * requests on the ephemeral port. If it sees a request that begins with 39 * "/multi-release.jar", it consumes the request and returns a stream of bytes 40 * representing the jar file multi-release.jar found in "userdir". 41 */ 42 public class SimpleHttpServer { 43 private static final String userdir = System.getProperty("user.dir", "."); 44 private static final Path multirelease = Paths.get(userdir, "multi-release.jar"); 45 46 private final HttpServer server; 47 48 public SimpleHttpServer() throws IOException { 49 server = HttpServer.create(); 50 } 51 52 public void start() throws IOException { 53 server.bind(new InetSocketAddress(0), 0); 54 server.createContext("/multi-release.jar", t -> { 55 try (InputStream is = t.getRequestBody()) { 56 is.readAllBytes(); // probably not necessary to consume request 57 byte[] bytes = Files.readAllBytes(multirelease); 58 t.sendResponseHeaders(200, bytes.length); 59 try (OutputStream os = t.getResponseBody()) { 60 os.write(bytes); 61 } 62 } 63 }); 64 server.setExecutor(null); // creates a default executor 65 server.start(); 66 } 67 68 public void stop() { 69 server.stop(0); 70 } 71 72 public int getPort() { 73 return server.getAddress().getPort(); 74 } 75 } 76 |