Search in sources :

Example 1 with LexerException

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();
}
Also used : SearchVersionRange(org.graylog2.configuration.validators.SearchVersionRange) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) SearchVersion(org.graylog2.storage.SearchVersion) LexerException(com.github.zafarkhaja.semver.expr.LexerException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

LexerException (com.github.zafarkhaja.semver.expr.LexerException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 GET (javax.ws.rs.GET)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 Path (javax.ws.rs.Path)1 SearchVersionRange (org.graylog2.configuration.validators.SearchVersionRange)1 SearchVersion (org.graylog2.storage.SearchVersion)1