health objects added

master
Lucio Lelii 1 year ago
parent 29e02ac587
commit 3407ce025c

@ -31,7 +31,7 @@
<distroDirectory>distro</distroDirectory>
<tomcat.version>8.0.42</tomcat.version>
<jersey.version>2.25.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
@ -117,11 +117,11 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- Added to support Java 11 JDK -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<!-- END Added to support Java 11 JDK -->

@ -0,0 +1,44 @@
package org.gcube.smartgears.extensions.resource;
import static org.gcube.smartgears.Constants.application_json;
import static org.gcube.smartgears.extensions.HttpExtension.Method.GET;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gcube.smartgears.extensions.ApiResource;
import org.gcube.smartgears.extensions.ApiSignature;
import org.gcube.smartgears.health.response.HealthResponse;
public class HealthResource extends ApiResource {
private static final long serialVersionUID = 1L;
public static final String mapping = "/health";
private static final ApiSignature signature = handles(mapping).with(method(GET).produces(application_json));
HealthResource() {
super(signature);
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
public HealthResponse liveness() {
return null;
}
public HealthResponse readiness() {
return null;
}
}

@ -1,6 +1,6 @@
package org.gcube.smartgears.extensions.resource;
import static org.gcube.smartgears.Constants.*;
import static org.gcube.smartgears.Constants.remote_management;
import javax.xml.bind.annotation.XmlRootElement;
@ -27,7 +27,7 @@ public class RemoteResource extends HttpController {
public RemoteResource() {
super(remote_management, default_mapping);
addResources(new FrontPageResource(), new ConfigurationResource(), new ProfileResource(),
new LifecycleResource(), new MetricsResource());
new LifecycleResource(), new MetricsResource(), new HealthResource());
}
@Override

@ -0,0 +1,9 @@
package org.gcube.smartgears.health;
import org.gcube.smartgears.health.response.HealthCheckResponse;
public interface HealthCheck {
HealthCheckResponse check();
}

@ -0,0 +1,25 @@
package org.gcube.smartgears.health;
import java.util.Set;
import org.gcube.smartgears.health.response.HealthCheckResponse;
import org.gcube.smartgears.provider.ProviderFactory;
@ReadinessChecker
public class KeyCloakHealthCheck implements HealthCheck{
private static final String CHECK_NAME = "authorizationCheck" ;
@Override
public HealthCheckResponse check() {
try {
Set<String> contexts = ProviderFactory.provider().containerContext().authorizationProvider().getContexts();
if (contexts.isEmpty())
return HealthCheckResponse.builder(CHECK_NAME).down().withMessage("no contexts are defined for this server").build();
return HealthCheckResponse.builder(CHECK_NAME).up().build();
}catch (Exception e) {
return HealthCheckResponse.builder(CHECK_NAME).down().withMessage(e.getMessage()).build();
}
}
}

@ -0,0 +1,12 @@
package org.gcube.smartgears.health;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ReadinessChecker {
}

@ -0,0 +1,6 @@
package org.gcube.smartgears.health;
public enum Status {
UP, DOWN, NOT_CALCULATED
}

@ -0,0 +1,30 @@
package org.gcube.smartgears.health.response;
import org.gcube.com.fasterxml.jackson.annotation.JsonInclude;
import org.gcube.com.fasterxml.jackson.annotation.JsonInclude.Include;
import org.gcube.common.validator.annotations.NotEmpty;
import org.gcube.common.validator.annotations.NotNull;
import org.gcube.smartgears.health.Status;
@JsonInclude(Include.NON_NULL)
public class HealthCheckResponse {
public static HealthCheckResponseBuilder builder(String checkName) {
return new HealthCheckResponseBuilder(checkName);
}
protected HealthCheckResponse() {}
//enum ErrorType {UNRECOVERABLE, RECOVERABLE}
@NotNull
protected Status status;
@NotNull @NotEmpty
protected String name;
//protected ErrorType errorType;
protected String errorMessage;
}

@ -0,0 +1,65 @@
package org.gcube.smartgears.health.response;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.validator.ValidationError;
import org.gcube.common.validator.Validator;
import org.gcube.common.validator.ValidatorFactory;
import org.gcube.smartgears.health.Status;
public class HealthCheckResponseBuilder {
private HealthCheckResponse healthCheckResponse = new HealthCheckResponse();
protected HealthCheckResponseBuilder(String name) {
healthCheckResponse.name = name;
}
public BuildPart up() {
healthCheckResponse.status = Status.UP;
return new BuildPart();
}
public ErrorPart down() {
healthCheckResponse.status = Status.DOWN;
return new ErrorPart();
}
private void validateResponse() {
List<String> msgs = new ArrayList<String>();
Validator validator = ValidatorFactory.validator();
for (ValidationError error : validator.validate(healthCheckResponse))
msgs.add(error.toString());
if (!msgs.isEmpty())
throw new IllegalStateException("invalid configuration: "+msgs);
}
public class ErrorPart{
private ErrorPart() {}
public BuildPart withMessage(String message) {
healthCheckResponse.errorMessage = message;
return new BuildPart();
}
}
public class BuildPart{
private BuildPart() {};
public HealthCheckResponse build() {
validateResponse();
return healthCheckResponse;
}
}
}

@ -0,0 +1,18 @@
package org.gcube.smartgears.health.response;
import java.util.List;
import org.gcube.smartgears.health.HealthCheck;
import org.gcube.smartgears.health.Status;
public class HealthResponse {
Status status;
List<HealthCheck> checks;
void register(HealthCheck check){
checks.add(check);
}
}

@ -0,0 +1,13 @@
package org.gcube.smartgears.health.response;
public class HealthResponseBuilder {
private HealthResponse healthResponse;
public HealthResponse build() {
return healthResponse;
}
}
Loading…
Cancel
Save