|
1 | 1 | /* |
2 | | - * Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
40 | 40 |
|
41 | 41 | public class B6431193 { |
42 | 42 |
|
43 | | - static boolean error = false; |
| 43 | + static boolean handlerIsDaemon = true; |
44 | 44 |
|
45 | | - public static void read (InputStream i) throws IOException { |
46 | | - while (i.read() != -1); |
47 | | - i.close(); |
48 | | - } |
49 | | - |
50 | | - /** |
51 | | - * @param args |
52 | | - */ |
53 | | - public static void main(String[] args) { |
| 45 | + public static void main(String[] args) throws IOException { |
54 | 46 | class MyHandler implements HttpHandler { |
55 | 47 | public void handle(HttpExchange t) throws IOException { |
56 | | - InputStream is = t.getRequestBody(); |
57 | | - read(is); |
58 | | - // .. read the request body |
| 48 | + try (InputStream is = t.getRequestBody(); |
| 49 | + OutputStream os = t.getResponseBody()) { |
| 50 | + is.readAllBytes(); |
| 51 | + // .. read the request body |
59 | 52 | String response = "This is the response"; |
60 | | - t.sendResponseHeaders(200, response.length()); |
61 | | - OutputStream os = t.getResponseBody(); |
62 | | - os.write(response.getBytes()); |
63 | | - os.close(); |
64 | | - error = Thread.currentThread().isDaemon(); |
| 53 | + t.sendResponseHeaders(200, response.length()); |
| 54 | + os.write(response.getBytes()); |
| 55 | + } finally { |
| 56 | + handlerIsDaemon = Thread.currentThread().isDaemon(); |
| 57 | + } |
65 | 58 | } |
66 | 59 | } |
67 | 60 |
|
| 61 | + InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 62 | + HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 10); |
| 63 | + server.createContext("/apps", new MyHandler()); |
| 64 | + server.setExecutor(null); |
| 65 | + server.start(); |
68 | 66 |
|
69 | | - HttpServer server; |
70 | 67 | try { |
71 | | - InetAddress loopback = InetAddress.getLoopbackAddress(); |
72 | | - server = HttpServer.create(new InetSocketAddress(loopback, 0), 10); |
73 | | - |
74 | | - server.createContext("/apps", new MyHandler()); |
75 | | - server.setExecutor(null); |
76 | | - // creates a default executor |
77 | | - server.start(); |
78 | 68 | int port = server.getAddress().getPort(); |
79 | | - String s = "http://localhost:"+port+"/apps/foo"; |
80 | 69 | URL url = URIBuilder.newBuilder() |
81 | | - .scheme("http") |
82 | | - .loopback() |
83 | | - .port(port) |
84 | | - .path("/apps/foo") |
85 | | - .toURL(); |
86 | | - InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream(); |
87 | | - read (is); |
88 | | - server.stop(0); |
89 | | - if (error) { |
90 | | - throw new RuntimeException ("error in test"); |
| 70 | + .scheme("http") |
| 71 | + .loopback() |
| 72 | + .port(port) |
| 73 | + .path("/apps/foo") |
| 74 | + .toURL(); |
| 75 | + try (InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream()) { |
| 76 | + is.readAllBytes(); |
91 | 77 | } |
92 | | - |
93 | | - } |
94 | | - catch (Exception e) { |
| 78 | + if (handlerIsDaemon) { |
| 79 | + throw new RuntimeException("request was handled by a daemon thread"); |
| 80 | + } |
| 81 | + } catch (Exception e) { |
95 | 82 | throw new AssertionError("Unexpected exception: " + e, e); |
| 83 | + } finally { |
| 84 | + server.stop(0); |
96 | 85 | } |
97 | 86 | } |
98 | 87 | } |
0 commit comments