Skip to content

HealthCheckServlet: set content-type, close jsonb #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;

import java.io.IOException;

@WebServlet(urlPatterns = {"${health.servlet.uri}"}, name = "HealthServlet")
Expand All @@ -39,13 +41,21 @@ public class HealthCheckServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Jsonb jsonb = JsonbBuilder.newBuilder().build();
resp.setContentType(MediaType.APPLICATION_JSON);
HealthCheckModel healthCheckModel = healthCheckManager.performHealthChecks();
if (healthCheckModel.getOutcome().equalsIgnoreCase(HealthCheckResponse.State.UP.name())) {
resp.setStatus(200);
} else {
resp.setStatus(503);
}
jsonb.toJson(healthCheckModel, resp.getOutputStream());
try (Jsonb jsonb = JsonbBuilder.newBuilder().build()) {
jsonb.toJson(healthCheckModel, resp.getOutputStream());
} catch (IOException ex) {
// Re-throw IOException specifically to maintain existing functionality
throw ex;
} catch (Exception ex) {
// Convert other exceptions thrown by close() into a runtime exception
throw new RuntimeException(ex);
}
}
}