All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.cp4waiops.connectors.sdk.SDKStatusBuilder Maven / Gradle / Ivy

There is a newer version: 2.2.8
Show newest version
package com.ibm.cp4waiops.connectors.sdk;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hc.core5.http.HttpStatus;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;

public class SDKStatusBuilder {
    private static final Logger logger = Logger.getLogger(SDKStatusBuilder.class.getName());

    private static ObjectMapper _mapper = new ObjectMapper();

    boolean _up;
    HashMap _data;

    public enum SDKComponent {
        ConnectorManager, Connector,
    }

    public SDKStatusBuilder() {
        _up = true;
        _data = new HashMap<>();
    }

    public void addResponse(HealthCheckResponse response, String nameOverride) {
        String name = response.getName();
        if (nameOverride != null) {
            name = nameOverride;
        }
        if (response.getStatus().equals(HealthCheckResponse.Status.DOWN)) {
            _up = false;
            _data.put(name, "is down");
        }
        if (response.getData().isPresent()) {
            for (var entry : response.getData().get().entrySet()) {
                String key = name + "." + entry.getKey();
                String value = String.valueOf(entry.getValue());
                _data.put(key, value);
            }
        }
    }

    public HealthCheckResponse toHealthCheckResponse(String name) {
        HealthCheckResponseBuilder builder = HealthCheckResponse.builder();
        builder.name(name);
        if (_up) {
            builder.up();
        } else {
            builder.down();
        }
        for (var entry : _data.entrySet()) {
            builder.withData(entry.getKey(), entry.getValue());
        }
        return builder.build();
    }

    public void writeToResponse(HttpServletResponse resp) throws IOException {
        try {
            String body = _mapper.writeValueAsString(_data);
            resp.setStatus(_up ? HttpStatus.SC_OK : HttpStatus.SC_SERVICE_UNAVAILABLE);
            resp.setContentType(Constant.JSON_CONTENT_TYPE);
            resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
            PrintWriter writer = resp.getWriter();
            writer.print(body);
            writer.close();
            return;
        } catch (JsonProcessingException error) {
            logger.log(Level.WARNING, "failed to serialize response", error);
            resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy