moved health part to a separate library

This commit is contained in:
lucio 2023-01-27 17:01:22 +01:00
parent 3407ce025c
commit 98c41e7463
10 changed files with 12 additions and 200 deletions

49
pom.xml
View File

@ -8,13 +8,10 @@
<version>1.1.0</version> <version>1.1.0</version>
<relativePath /> <relativePath />
</parent> </parent>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId> <artifactId>common-smartgears</artifactId>
<version>4.0.0-SNAPSHOT</version> <version>4.0.0-SNAPSHOT</version>
<name>SmartGears</name> <name>SmartGears</name>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
@ -26,7 +23,6 @@
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<properties> <properties>
<distroDirectory>distro</distroDirectory> <distroDirectory>distro</distroDirectory>
<tomcat.version>8.0.42</tomcat.version> <tomcat.version>8.0.42</tomcat.version>
@ -35,21 +31,22 @@
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
</properties> </properties>
<scm> <scm>
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/common-smartgears.git</connection> <connection>scm:git:https://code-repo.d4science.org/gCubeSystem/common-smartgears.git</connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/common-smartgears.git</developerConnection> <developerConnection>scm:git:httpstps://code-repo.d4science.org/gCubeSystem/common-smartgears.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/common-smartgears</url> <url>https://code-repo.d4science.org/gCubeSystem/common-smartgears</url>
</scm> </scm>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.reflections</groupId> <groupId>org.reflections</groupId>
<artifactId>reflections</artifactId> <artifactId>reflections</artifactId>
<version>0.9.10</version> <version>0.9.10</version>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>health-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- gCube Jackson --> <!-- gCube Jackson -->
<dependency> <dependency>
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>
@ -68,40 +65,33 @@
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>
<artifactId>common-security</artifactId> <artifactId>common-security</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.data.publishing</groupId> <groupId>org.gcube.data.publishing</groupId>
<artifactId>document-store-lib</artifactId> <artifactId>document-store-lib</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.accounting</groupId> <groupId>org.gcube.accounting</groupId>
<artifactId>accounting-lib</artifactId> <artifactId>accounting-lib</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.ini4j</groupId> <groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId> <artifactId>ini4j</artifactId>
<version>0.5.4</version> <version>0.5.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-validator</artifactId> <artifactId>common-validator</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version> <version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-events</artifactId> <artifactId>common-events</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version> <version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.common.security</groupId> <groupId>org.gcube.common.security</groupId>
<artifactId>gcube-secrets</artifactId> <artifactId>gcube-secrets</artifactId>
@ -110,37 +100,30 @@
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId> <artifactId>javax.ws.rs-api</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version> <version>4.0.1</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- Added to support Java 11 JDK --> <!-- Added to support Java 11 JDK -->
<dependency> <dependency>
<groupId>javax.xml.bind</groupId> <groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId> <artifactId>jaxb-api</artifactId>
</dependency> </dependency>
<!-- END Added to support Java 11 JDK --> <!-- END Added to support Java 11 JDK -->
<!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-core --> <!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-core -->
<dependency> <dependency>
<groupId>io.micrometer</groupId> <groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId> <artifactId>micrometer-core</artifactId>
<version>1.9.0</version> <version>1.9.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.micrometer</groupId> <groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId> <artifactId>micrometer-registry-prometheus</artifactId>
<version>1.9.0</version> <version>1.9.0</version>
</dependency> </dependency>
<!-- ***************** test ******************* --> <!-- ***************** test ******************* -->
<dependency> <dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId> <groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-depchain</artifactId> <artifactId>shrinkwrap-resolver-depchain</artifactId>
@ -148,7 +131,6 @@
<type>pom</type> <type>pom</type>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina --> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
<dependency> <dependency>
<groupId>org.apache.tomcat</groupId> <groupId>org.apache.tomcat</groupId>
@ -156,66 +138,54 @@
<version>${tomcat.version}</version> <version>${tomcat.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId> <artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version> <version>${tomcat.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.glassfish.jersey.core</groupId> <groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId> <artifactId>jersey-client</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId> <artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version> <version>${tomcat.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId> <artifactId>tomcat-embed-logging-log4j</artifactId>
<version>${tomcat.version}</version> <version>${tomcat.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId> <artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version> <version>${tomcat.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.10</version> <version>4.10</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>1.9.0</version> <version>1.9.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<!-- excludes probe package from jar --> <!-- excludes probe package from jar -->
<plugin> <plugin>
@ -236,7 +206,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
@ -249,8 +218,6 @@
</systemPropertyVariables> </systemPropertyVariables>
</configuration> </configuration>
</plugin> </plugin>
<!-- include probe in attached war --> <!-- include probe in attached war -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
@ -259,7 +226,6 @@
<configuration> <configuration>
<primaryArtifact>false</primaryArtifact> <primaryArtifact>false</primaryArtifact>
<classifier>probe</classifier> <classifier>probe</classifier>
<packagingIncludes>WEB-INF/classes/org/gcube/smartgears/probe/**/*</packagingIncludes> <packagingIncludes>WEB-INF/classes/org/gcube/smartgears/probe/**/*</packagingIncludes>
<failOnMissingWebXml>false</failOnMissingWebXml> <failOnMissingWebXml>false</failOnMissingWebXml>
</configuration> </configuration>
@ -273,8 +239,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
@ -311,7 +275,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -9,9 +9,9 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.gcube.common.health.api.response.HealthResponse;
import org.gcube.smartgears.extensions.ApiResource; import org.gcube.smartgears.extensions.ApiResource;
import org.gcube.smartgears.extensions.ApiSignature; import org.gcube.smartgears.extensions.ApiSignature;
import org.gcube.smartgears.health.response.HealthResponse;
public class HealthResource extends ApiResource { public class HealthResource extends ApiResource {

View File

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

View File

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

View File

@ -1,12 +0,0 @@
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 {
}

View File

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

View File

@ -1,30 +0,0 @@
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;
}

View File

@ -1,65 +0,0 @@
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;
}
}
}

View File

@ -1,18 +0,0 @@
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);
}
}

View File

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