use of com.github.zafarkhaja.semver.expr.LexerException in project graylog2-server by Graylog2.
the class SatisfiesVersionResponse method satisfiesVersion.
@GET
@Path("/satisfiesVersion/{distribution}")
@ApiOperation(value = "Confirms whether the current search version satisfies a given distribution and an optional Semantic Versioning version")
public SatisfiesVersionResponse satisfiesVersion(@ApiParam(name = "distribution", required = true) @PathParam("distribution") String distribution, @ApiParam(name = "version") @QueryParam("version") String version) {
// if no version provided give default to only check distribution
if (version == null || version.isEmpty()) {
version = ">0";
}
// attempt to parse a SearchVersion.Distribution from provided distribution string
final SearchVersion.Distribution requiredDistribution;
try {
requiredDistribution = SearchVersion.Distribution.valueOf(distribution.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
LOG.error("Unsupported distribution {}. Valid values are [opensearch, elasticsearch].", distribution);
throw new InternalServerErrorException(StringUtils.f("Unsupported distribution %s. Valid values are [opensearch, elasticsearch].", distribution));
}
final SearchVersion currentVersion = versionProvider.get();
final SearchVersionRange requiredVersion = SearchVersionRange.of(requiredDistribution, version);
final boolean satisfied;
try {
LOG.debug("Checking current version {} satisfies required version {} {}", currentVersion, requiredDistribution, version);
satisfied = currentVersion.satisfies(requiredVersion);
} catch (LexerException e) {
// catch invalid SemVer expression
LOG.error("Unable to create a search version range for SemVer expression {}", version);
throw new InternalServerErrorException(StringUtils.f("Unable to create a search version range for SemVer expression %s", version));
}
// create and send response
String errorMessage = "";
if (!satisfied) {
errorMessage = StringUtils.f("Current search version %s does not satisfy required version %s %s", currentVersion, requiredDistribution, version);
}
return SatisfiesVersionResponse.Builder.create().satisfied(satisfied).errorMessage(errorMessage).build();
}
Aggregations