use of org.xipki.common.HealthCheckResult in project xipki by xipki.
the class HealthCheckResultTest method test2.
@Test
public void test2() {
String encoded = "{\"healthy\":true,\"checks\":{\"childcheck\":{\"healthy\":false," + "\"checks\":{\"childChildCheck\":{\"healthy\":false}}}," + "\"childcheck2\":{\"healthy\":false}}}";
HealthCheckResult result = HealthCheckResult.getInstanceFromJsonMessage("default", encoded);
String noPrettyJson = "{\"healthy\":true,\"checks\":{\"childcheck\":{\"healthy\":false," + "\"checks\":{\"childChildCheck\":{\"healthy\":false}}}," + "\"childcheck2\":{\"healthy\":false}}}";
String prettyJson = "{\n" + " \"healthy\":true,\n" + " \"checks\":{\n" + " \"childcheck\":{\n" + " \"healthy\":false,\n" + " \"checks\":{\n" + " \"childChildCheck\":{\n" + " \"healthy\":false\n" + " }\n" + " }\n" + " },\n" + " \"childcheck2\":{\n" + " \"healthy\":false\n" + " }\n" + " }\n" + "}";
check(result, noPrettyJson, prettyJson);
}
use of org.xipki.common.HealthCheckResult in project xipki by xipki.
the class HealthCheckServlet method doGet.
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
resp.setHeader("Access-Control-Allow-Origin", "*");
ResponderManager responderManager = ServletHelper.getResponderManager();
if (responderManager == null) {
LOG.error("ServletHelper.responderManager not configured");
sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
try {
String caName = null;
X509CaCmpResponder responder = null;
String path = StringUtil.getRelativeRequestUri(req.getServletPath(), req.getRequestURI());
if (path.length() > 1) {
// skip the first char which is always '/'
String caAlias = path.substring(1);
caName = responderManager.getCaNameForAlias(caAlias);
if (caName == null) {
caName = caAlias.toLowerCase();
}
responder = responderManager.getX509CaResponder(caName);
}
if (caName == null || responder == null || !responder.isOnService()) {
String auditMessage;
if (caName == null) {
auditMessage = "no CA is specified";
} else if (responder == null) {
auditMessage = "unknown CA '" + caName + "'";
} else {
auditMessage = "CA '" + caName + "' is out of service";
}
LOG.warn(auditMessage);
sendError(resp, HttpServletResponse.SC_NOT_FOUND);
return;
}
HealthCheckResult healthResult = responder.healthCheck();
int status = healthResult.isHealthy() ? HttpServletResponse.SC_OK : HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
byte[] respBytes = healthResult.toJsonMessage(true).getBytes();
resp.setStatus(status);
resp.setContentLength(respBytes.length);
resp.setContentType(CT_RESPONSE);
resp.getOutputStream().write(respBytes);
} catch (Throwable th) {
if (th instanceof EOFException) {
LogUtil.warn(LOG, th, "connection reset by peer");
} else {
LOG.error("Throwable thrown, this should not happen!", th);
}
sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
resp.flushBuffer();
}
}
use of org.xipki.common.HealthCheckResult in project xipki by xipki.
the class HealthCheckServlet method service0.
private FullHttpResponse service0(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession) {
HttpVersion version = request.protocolVersion();
HttpMethod method = request.method();
if (method != HttpMethod.GET) {
return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
}
try {
if (responderManager == null) {
LOG.error("responderManager in servlet is not configured");
return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
String caName = null;
X509CaCmpResponder responder = null;
if (servletUri.getPath().length() > 1) {
// skip the first char which is always '/'
String caAlias = servletUri.getPath().substring(1);
caName = responderManager.getCaNameForAlias(caAlias);
if (caName == null) {
caName = caAlias.toLowerCase();
}
responder = responderManager.getX509CaResponder(caName);
}
if (caName == null || responder == null || !responder.isOnService()) {
String auditMessage;
if (caName == null) {
auditMessage = "no CA is specified";
} else if (responder == null) {
auditMessage = "unknown CA '" + caName + "'";
} else {
auditMessage = "CA '" + caName + "' is out of service";
}
LOG.warn(auditMessage);
return createErrorResponse(version, HttpResponseStatus.NOT_FOUND);
}
HealthCheckResult healthResult = responder.healthCheck();
HttpResponseStatus status = healthResult.isHealthy() ? HttpResponseStatus.OK : HttpResponseStatus.INTERNAL_SERVER_ERROR;
byte[] respBytes = healthResult.toJsonMessage(true).getBytes();
return createResponse(version, status, HealthCheckServlet.CT_RESPONSE, respBytes);
} catch (Throwable th) {
if (th instanceof EOFException) {
LogUtil.warn(LOG, th, "connection reset by peer");
} else {
LOG.error("Throwable thrown, this should not happen!", th);
}
return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
}
use of org.xipki.common.HealthCheckResult in project xipki by xipki.
the class CaClientImpl method getHealthCheckResult.
@Override
public HealthCheckResult getHealthCheckResult(String caName) throws CaClientException {
caName = ParamUtil.requireNonNull("caName", caName).toLowerCase();
String name = "X509CA";
HealthCheckResult healthCheckResult = new HealthCheckResult(name);
try {
init0(false);
} catch (CaClientException ex) {
LogUtil.error(LOG, ex, "could not initialize CaCleint");
healthCheckResult.setHealthy(false);
return healthCheckResult;
}
CaConf ca = casMap.get(caName);
if (ca == null) {
throw new IllegalArgumentException("unknown CA " + caName);
}
String healthUrlStr = ca.getHealthUrl();
URL serverUrl;
try {
serverUrl = new URL(healthUrlStr);
} catch (MalformedURLException ex) {
throw new CaClientException("invalid URL '" + healthUrlStr + "'");
}
try {
HttpURLConnection httpUrlConnection = IoUtil.openHttpConn(serverUrl);
InputStream inputStream = httpUrlConnection.getInputStream();
int responseCode = httpUrlConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_INTERNAL_ERROR) {
inputStream.close();
throw new IOException(String.format("bad response: code='%s', message='%s'", httpUrlConnection.getResponseCode(), httpUrlConnection.getResponseMessage()));
}
String responseContentType = httpUrlConnection.getContentType();
boolean isValidContentType = false;
if (responseContentType != null) {
if ("application/json".equalsIgnoreCase(responseContentType)) {
isValidContentType = true;
}
}
if (!isValidContentType) {
inputStream.close();
throw new IOException("bad response: mime type " + responseContentType + " not supported!");
}
byte[] responseBytes = IoUtil.read(inputStream);
if (responseBytes.length == 0) {
healthCheckResult.setHealthy(responseCode == HttpURLConnection.HTTP_OK);
} else {
String response = new String(responseBytes);
try {
healthCheckResult = HealthCheckResult.getInstanceFromJsonMessage(name, response);
} catch (IllegalArgumentException ex) {
LogUtil.error(LOG, ex, "IOException while parsing the health json message");
if (LOG.isDebugEnabled()) {
LOG.debug("json message: {}", response);
}
healthCheckResult.setHealthy(false);
}
}
} catch (IOException ex) {
LogUtil.error(LOG, ex, "IOException while fetching the URL " + healthUrlStr);
healthCheckResult.setHealthy(false);
}
return healthCheckResult;
}
Aggregations