Java Code Examples for com.sun.net.httpserver.HttpServer#start()
The following examples show how to use
com.sun.net.httpserver.HttpServer#start() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuiltInServer.java From promagent with Apache License 2.0 | 6 votes |
static void run(String host, String portString, CollectorRegistry registry) throws Exception { try { int port = Integer.parseInt(portString); InetSocketAddress address = host == null ? new InetSocketAddress(port) : new InetSocketAddress(host, port); HttpServer httpServer = HttpServer.create(address, 10); httpServer.createContext("/", httpExchange -> { if ("/metrics".equals(httpExchange.getRequestURI().getPath())) { respondMetrics(registry, httpExchange); } else { respondRedirect(httpExchange); } }); httpServer.start(); } catch (NumberFormatException e) { throw new RuntimeException("Failed to parse command line arguments: '" + portString + "' is not a valid port number."); } }
Example 2
Source File: DasServer.java From das with Apache License 2.0 | 6 votes |
private static void startupHSServer() { HttpServer server; try { server = HttpServer.create(new InetSocketAddress(8080), 0); } catch (IOException e) { logger.error("Fail to startup HSServer", e); throw new RuntimeException(e); } server.createContext("/hs", t -> { String response = "OK"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close(); }); server.setExecutor(null); server.start(); }
Example 3
Source File: ESBJAVA2283ReturnContentTypeTestCase.java From product-ei with Apache License 2.0 | 6 votes |
@Test(groups = {"wso2.esb"}, description = "test return content type") public void testReturnContentType() throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0); String contentType = "text/xml"; server.createContext("/gettest", new MyHandler()); server.setExecutor(null); // creates a default executor server.start(); DefaultHttpClient httpclient = new DefaultHttpClient(); String url = "http://localhost:8480/serviceTest/test"; HttpGet httpGet = new HttpGet(url); HttpResponse response = null; try { response = httpclient.execute(httpGet); } catch (IOException e) { log.error("Error Occurred while sending http get request. " , e); } log.info(response.getEntity().getContentType()); log.info(response.getStatusLine().getStatusCode()); assertEquals(response.getFirstHeader("Content-Type").getValue(), contentType, "Expected content type doesn't match"); assertEquals(response.getStatusLine().getStatusCode(), HTTP_STATUS_OK, "response code doesn't match"); server.stop(5); }
Example 4
Source File: FixedLengthInputStream.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Http Server */ HttpServer startHttpServer() throws IOException { if (debug) { Logger logger = Logger.getLogger("com.sun.net.httpserver"); Handler outHandler = new StreamHandler(System.out, new SimpleFormatter()); outHandler.setLevel(Level.FINEST); logger.setLevel(Level.FINEST); logger.addHandler(outHandler); } HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); httpServer.createContext("/flis/", new MyHandler(POST_SIZE)); httpServer.start(); return httpServer; }
Example 5
Source File: HTTPResponseCodeTestCase.java From product-ei with Apache License 2.0 | 6 votes |
@Test(groups = {"wso2.esb" }, description = "Test whether ESB pass-through responses with different response codes.", dataProvider = "getResponseCodes") public void testReturnResponseCode( int responseCode) throws Exception { this.responseCode = responseCode; //Starting backend server HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0); server.createContext("/gettest", new ResponseHandler()); server.setExecutor(null); // creates a default executor server.start(); //Invoke API deployed in ESB switch (responseCode) { case 404: String contentType = "text/html"; String url = getApiInvocationURL("/serviceTest/notfound"); sendRequest(url, contentType); default: contentType = "text/xml"; url = getApiInvocationURL("/serviceTest/test"); sendRequest(url, contentType); } server.stop(5); }
Example 6
Source File: ChunkedEncodingTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Http Server */ static HttpServer startHttpServer() throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); HttpHandler httpHandler = new SimpleHandler(); httpServer.createContext("/chunked/", httpHandler); httpServer.start(); return httpServer; }
Example 7
Source File: TestClusterManager.java From rubix with Apache License 2.0 | 5 votes |
private HttpServer createServer(String endpoint1, HttpHandler handler1, String endpoint2, HttpHandler handler2) throws IOException { HttpServer server = HttpServer.create(new InetSocketAddress(45326), 0); server.createContext(endpoint1, handler1); server.createContext(endpoint2, handler2); server.setExecutor(null); // creates a default executor server.start(); return server; }
Example 8
Source File: ExpectUtilsHttpTest.java From expect4j with Apache License 2.0 | 5 votes |
public void setUp() throws Exception { HttpServer httpServer = HttpServer.create(new InetSocketAddress(address, 0), 0); httpServer.createContext("/", new TestHandler()); httpServer.setExecutor(null); httpServer.start(); port = httpServer.getAddress().getPort(); }
Example 9
Source File: App.java From templates with MIT License | 5 votes |
public static void main(String[] args) throws Exception { int port = 8082; IHandler handler = new com.openfaas.function.Handler(); HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); InvokeHandler invokeHandler = new InvokeHandler(handler); server.createContext("/", invokeHandler); server.setExecutor(null); // creates a default executor server.start(); }
Example 10
Source File: HttpNegotiateServer.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
/** * Creates and starts an HTTP or proxy server that requires * Negotiate authentication. * @param scheme "Negotiate" or "Kerberos" * @param principal the krb5 service principal the server runs with * @return the server */ public static HttpServer httpd(String scheme, boolean proxy, String principal, String ktab) throws Exception { MyHttpHandler h = new MyHttpHandler(); HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); HttpContext hc = server.createContext("/", h); hc.setAuthenticator(new MyServerAuthenticator( proxy, scheme, principal, ktab)); server.start(); return server; }
Example 11
Source File: ChunkedEncodingTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
/** * Http Server */ static HttpServer startHttpServer() throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); HttpHandler httpHandler = new SimpleHandler(); httpServer.createContext("/chunked/", httpHandler); httpServer.start(); return httpServer; }
Example 12
Source File: ClassLoad.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { boolean error = true; // Start a dummy server to return 404 HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); HttpHandler handler = new HttpHandler() { public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); while (is.read() != -1); t.sendResponseHeaders (404, -1); t.close(); } }; server.createContext("/", handler); server.start(); // Client request try { URL url = new URL("http://localhost:" + server.getAddress().getPort()); String name = args.length >= 2 ? args[1] : "foo.bar.Baz"; ClassLoader loader = new URLClassLoader(new URL[] { url }); System.out.println(url); Class c = loader.loadClass(name); System.out.println("Loaded class \"" + c.getName() + "\"."); } catch (ClassNotFoundException ex) { System.out.println(ex); error = false; } finally { server.stop(0); } if (error) throw new RuntimeException("No ClassNotFoundException generated"); }
Example 13
Source File: ChunkedEncodingTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
/** * Http Server */ static HttpServer startHttpServer() throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); HttpHandler httpHandler = new SimpleHandler(); httpServer.createContext("/chunked/", httpHandler); httpServer.start(); return httpServer; }
Example 14
Source File: ClassLoad.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { boolean error = true; // Start a dummy server to return 404 HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); HttpHandler handler = new HttpHandler() { public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); while (is.read() != -1); t.sendResponseHeaders (404, -1); t.close(); } }; server.createContext("/", handler); server.start(); // Client request try { URL url = new URL("http://localhost:" + server.getAddress().getPort()); String name = args.length >= 2 ? args[1] : "foo.bar.Baz"; ClassLoader loader = new URLClassLoader(new URL[] { url }); System.out.println(url); Class c = loader.loadClass(name); System.out.println("Loaded class \"" + c.getName() + "\"."); } catch (ClassNotFoundException ex) { System.out.println(ex); error = false; } finally { server.stop(0); } if (error) throw new RuntimeException("No ClassNotFoundException generated"); }
Example 15
Source File: BasicLongCredentials.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public static void main (String[] args) throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); try { Handler handler = new Handler(); HttpContext ctx = server.createContext("/test", handler); BasicAuthenticator a = new BasicAuthenticator(REALM) { public boolean checkCredentials (String username, String pw) { return USERNAME.equals(username) && PASSWORD.equals(pw); } }; ctx.setAuthenticator(a); server.start(); Authenticator.setDefault(new MyAuthenticator()); URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/"); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); InputStream is = urlc.getInputStream(); int c = 0; while (is.read()!= -1) { c ++; } if (c != 0) { throw new RuntimeException("Test failed c = " + c); } if (error) { throw new RuntimeException("Test failed: error"); } System.out.println ("OK"); } finally { server.stop(0); } }
Example 16
Source File: NoCache.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
static HttpServer startHttpServer() throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); httpServer.createContext("/NoCache/", new SimpleHandler()); httpServer.start(); return httpServer; }
Example 17
Source File: UnmodifiableMaps.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
HttpServer startHttpServer() throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); httpServer.createContext("/foo", new SimpleHandler()); httpServer.start(); return httpServer; }
Example 18
Source File: NoCache.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
static HttpServer startHttpServer() throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); httpServer.createContext("/NoCache/", new SimpleHandler()); httpServer.start(); return httpServer; }
Example 19
Source File: HeadersTest1.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test public void test() throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(0), 10); Handler h = new Handler(); server.createContext("/test", h); int port = server.getAddress().getPort(); System.out.println("Server port = " + port); ExecutorService e = Executors.newCachedThreadPool(); server.setExecutor(e); server.start(); HttpClient client = HttpClient.newBuilder() .executor(e) .build(); try { URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo"); HttpRequest req = HttpRequest.newBuilder(uri) .headers("X-Bar", "foo1") .headers("X-Bar", "foo2") .GET() .build(); HttpResponse<?> resp = client.send(req, asString()); if (resp.statusCode() != 200) { throw new RuntimeException("Expected 200, got: " + resp.statusCode()); } HttpHeaders hd = resp.headers(); assertTrue(!hd.firstValue("Non-Existent-Header").isPresent()); List<String> v1 = hd.allValues("Non-Existent-Header"); assertNotNull(v1); assertTrue(v1.isEmpty(), String.valueOf(v1)); TestKit.assertUnmodifiableList(v1); List<String> v2 = hd.allValues("X-Foo-Response"); assertNotNull(v2); assertEquals(new HashSet<>(v2), Set.of("resp1", "resp2")); TestKit.assertUnmodifiableList(v2); Map<String, List<String>> map = hd.map(); TestKit.assertUnmodifiableMap(map); for (List<String> values : map.values()) { TestKit.assertUnmodifiableList(values); } } finally { server.stop(0); e.shutdownNow(); } System.out.println("OK"); }
Example 20
Source File: NoCache.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
static HttpServer startHttpServer() throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); httpServer.createContext("/NoCache/", new SimpleHandler()); httpServer.start(); return httpServer; }