|
38 | 38 | import java.sql.Statement; |
39 | 39 | import java.util.ArrayList; |
40 | 40 | import java.util.List; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | +import java.util.concurrent.TimeoutException; |
41 | 43 | import javax.net.ssl.KeyManagerFactory; |
42 | 44 | import javax.net.ssl.SSLContext; |
43 | 45 | import javax.net.ssl.SSLParameters; |
@@ -132,10 +134,11 @@ public void configure(HttpsParameters params) { |
132 | 134 | + " }\n" |
133 | 135 | + "}"; |
134 | 136 | } |
| 137 | + byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8); |
135 | 138 | exchange.getResponseHeaders().set("Content-Type", "application/json"); |
136 | | - exchange.sendResponseHeaders(200, response.length()); |
| 139 | + exchange.sendResponseHeaders(200, responseBytes.length); |
137 | 140 | try (OutputStream os = exchange.getResponseBody()) { |
138 | | - os.write(response.getBytes()); |
| 141 | + os.write(responseBytes); |
139 | 142 | } |
140 | 143 | }); |
141 | 144 | } |
@@ -201,14 +204,24 @@ private ProcessResult runSubprocess(String trustStore, String password) throws E |
201 | 204 | builder.redirectErrorStream(true); |
202 | 205 | Process process = builder.start(); |
203 | 206 |
|
204 | | - String output; |
205 | | - try (InputStreamReader reader = |
206 | | - new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { |
207 | | - output = CharStreams.toString(reader); |
| 207 | + String output = ""; |
| 208 | + boolean finished = false; |
| 209 | + try { |
| 210 | + try (InputStreamReader reader = |
| 211 | + new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { |
| 212 | + output = CharStreams.toString(reader); |
| 213 | + } |
| 214 | + finished = process.waitFor(10, TimeUnit.SECONDS); |
| 215 | + if (!finished) { |
| 216 | + throw new TimeoutException("Subprocess timed out after 10 seconds"); |
| 217 | + } |
| 218 | + int exitCode = process.exitValue(); |
| 219 | + return new ProcessResult(exitCode, output); |
| 220 | + } finally { |
| 221 | + if (!finished && process.isAlive()) { |
| 222 | + process.destroyForcibly(); |
| 223 | + } |
208 | 224 | } |
209 | | - |
210 | | - int exitCode = process.waitFor(); |
211 | | - return new ProcessResult(exitCode, output); |
212 | 225 | } |
213 | 226 |
|
214 | 227 | @Test |
|
0 commit comments