io.github.pmckeown.dependencytrack.metrics.MetricsClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dependency-track-maven-plugin Show documentation
Show all versions of dependency-track-maven-plugin Show documentation
Maven plugin to integrate with a Dependency Track server to submit dependency manifests and gather project metrics.
package io.github.pmckeown.dependencytrack.metrics;
import io.github.pmckeown.dependencytrack.CommonConfig;
import io.github.pmckeown.dependencytrack.Response;
import io.github.pmckeown.dependencytrack.project.Project;
import io.github.pmckeown.util.Logger;
import kong.unirest.GenericType;
import kong.unirest.HttpResponse;
import kong.unirest.jackson.JacksonObjectMapper;
import kong.unirest.Unirest;
import javax.inject.Inject;
import java.util.Optional;
import static io.github.pmckeown.dependencytrack.ResourceConstants.V1_METRICS_PROJECT_UUID_CURRENT;
import static io.github.pmckeown.dependencytrack.ObjectMapperFactory.relaxedObjectMapper;
import static io.github.pmckeown.dependencytrack.ResourceConstants.V1_METRICS_PROJECT_UUID_REFRESH;
import static kong.unirest.HeaderNames.ACCEPT;
import static kong.unirest.HeaderNames.ACCEPT_ENCODING;
import static kong.unirest.Unirest.get;
/**
* Client for getting project Metrics from Dependency Track
*
* @author Paul McKeown
*/
class MetricsClient {
private CommonConfig commonConfig;
private Logger logger;
@Inject
MetricsClient(CommonConfig commonConfig, Logger logger) {
this.commonConfig = commonConfig;
this.logger = logger;
}
static {
Unirest.config().setObjectMapper(new JacksonObjectMapper(relaxedObjectMapper()))
.addDefaultHeader(ACCEPT_ENCODING, "gzip, deflate")
.addDefaultHeader(ACCEPT, "application/json");
}
Response getMetrics(Project project) {
logger.debug("Getting metrics for project: %s-%s", project.getName(), project.getVersion());
final HttpResponse httpResponse = get(
commonConfig.getDependencyTrackBaseUrl() + V1_METRICS_PROJECT_UUID_CURRENT)
.header("X-Api-Key", commonConfig.getApiKey())
.routeParam("uuid", project.getUuid())
.asObject(new GenericType(){});
Optional body;
if (httpResponse.isSuccess()) {
body = Optional.of(httpResponse.getBody());
} else {
body = Optional.empty();
}
return new Response<>(httpResponse.getStatus(), httpResponse.getStatusText(),
httpResponse.isSuccess(), body);
}
public Response refreshMetrics(Project project) {
logger.info("Refreshing Metrics for project: %s-%s", project.getName(), project.getVersion());
final HttpResponse> httpResponse = get(
commonConfig.getDependencyTrackBaseUrl() + V1_METRICS_PROJECT_UUID_REFRESH)
.header("X-Api-Key", commonConfig.getApiKey())
.routeParam("uuid", project.getUuid())
.asEmpty();
return new Response<>(httpResponse.getStatus(), httpResponse.getStatusText(), httpResponse.isSuccess());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy