use of org.apereo.cas.monitor.HealthStatus in project cas by apereo.
the class HealthCheckController method handleRequestInternal.
/**
* Handle request.
*
* @param request the request
* @param response the response
* @return the model and view
* @throws Exception the exception
*/
@GetMapping
@ResponseBody
protected WebAsyncTask<HealthStatus> handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
ensureEndpointAccessIsAuthorized(request, response);
final Callable<HealthStatus> asyncTask = () -> {
final HealthStatus healthStatus = healthCheckMonitor.observe();
response.setStatus(healthStatus.getCode().value());
if (StringUtils.equals(request.getParameter("format"), "json")) {
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
JsonUtils.render(healthStatus.getDetails(), response);
} else {
final StringBuilder sb = new StringBuilder();
sb.append("Health: ").append(healthStatus.getCode());
final AtomicInteger i = new AtomicInteger();
healthStatus.getDetails().forEach((name, status) -> {
response.addHeader("X-CAS-" + name, String.format("%s;%s", status.getCode(), status.getDescription()));
sb.append("\n\n\t").append(i.incrementAndGet()).append('.').append(name).append(": ");
sb.append(status.getCode());
if (status.getDescription() != null) {
sb.append(" - ").append(status.getDescription());
}
});
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
try (Writer writer = response.getWriter()) {
IOUtils.copy(new ByteArrayInputStream(sb.toString().getBytes(response.getCharacterEncoding())), writer, StandardCharsets.UTF_8);
writer.flush();
}
}
return null;
};
return new WebAsyncTask<>(casProperties.getHttpClient().getAsyncTimeout(), asyncTask);
}
Aggregations