in fractions/monitor/src/main/java/org/wildfly/swarm/monitor/runtime/HttpContexts.java [201:256]
private void invokeHealthInVM(final HttpServerExchange exchange, HealthMetaData healthCheck, List<InVMResponse> responses, CountDownLatch latch) {
try {
String delegateContext = healthCheck.getWebContext();
final InVMConnection connection = new InVMConnection(
worker,
exchange.getConnection().getLocalAddress(InetSocketAddress.class).getPort()
);
final HttpServerExchange mockExchange = new HttpServerExchange(connection);
mockExchange.setRequestScheme("http");
mockExchange.setRequestMethod(new HttpString("GET"));
mockExchange.setProtocol(Protocols.HTTP_1_0);
mockExchange.setRequestURI(delegateContext);
mockExchange.setRequestPath(delegateContext);
mockExchange.setRelativePath(delegateContext);
mockExchange.getRequestHeaders().add(Headers.HOST, exchange.getRequestHeaders().get(Headers.HOST).getFirst());
mockExchange.putAttachment(TOKEN, EPHEMERAL_TOKEN);
mockExchange.putAttachment(RESPONSES, responses);
connection.addCloseListener(new ServerConnection.CloseListener() {
@Override
public void closed(ServerConnection connection) {
LOG.trace("Mock connection closed");
StringBuffer sb = new StringBuffer();
((InVMConnection) connection).flushTo(sb);
LOG.trace("Response payload: " + sb.toString());
if ("application/json".equals(mockExchange.getResponseHeaders().getFirst(Headers.CONTENT_TYPE))) {
responses.add(new InVMResponse(mockExchange.getStatusCode(), sb.toString()));
} else {
StringBuffer json = new StringBuffer("{");
json.append("\"id\"").append(":\"").append(mockExchange.getRelativePath()).append("\",");
json.append("\"result\"").append(":\"").append("DOWN").append("\",");
json.append("\"data\"").append(":").append("{");
json.append("\"status-code\"").append(":").append(mockExchange.getStatusCode());
json.append("}");
json.append("}");
responses.add(new InVMResponse(mockExchange.getStatusCode(), json.toString()));
}
mockExchange.removeAttachment(RESPONSES);
IoUtils.safeClose(connection);
latch.countDown();
}
});
HttpServerConnection httpConnection = (HttpServerConnection) exchange.getConnection();
mockExchange.startBlocking();
Connectors.executeRootHandler(httpConnection.getRootHandler(), mockExchange);
} catch (Throwable t) {
LOG.error("Health check failed", t);
latch.countDown();
}
}