Search in sources :

Example 1 with InvalidVersionException

use of io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException in project schema-registry by confluentinc.

the class SubjectVersionsResource method deleteSchemaVersion.

@DELETE
@Path("/{version}")
@PerformanceMetric("subjects.versions.deleteSchemaVersion-schema")
@Operation(summary = "Deletes a specific version of the schema registered under this subject. " + "This only deletes the version and the schema ID remains intact making it still possible " + "to decode data using the schema ID. This API is recommended to be used only in " + "development environments or under extreme circumstances where-in, its required to delete " + "a previously registered schema for compatibility purposes or re-register previously " + "registered schema.", responses = { @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = int.class))), @ApiResponse(responseCode = "404", description = "Error code 40401 -- Subject not found\n" + "Error code 40402 -- Version not found"), @ApiResponse(responseCode = "422", description = "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the backend " + "data store") })
public void deleteSchemaVersion(@Suspended final AsyncResponse asyncResponse, @Context HttpHeaders headers, @Parameter(description = "Name of the subject", required = true) @PathParam("subject") String subject, @Parameter(description = VERSION_PARAM_DESC, required = true) @PathParam("version") String version, @Parameter(description = "Whether to perform a permanent delete") @QueryParam("permanent") boolean permanentDelete) {
    log.info("Deleting schema version {} from subject {}", version, subject);
    subject = QualifiedSubject.normalize(schemaRegistry.tenant(), subject);
    VersionId versionId;
    try {
        versionId = new VersionId(version);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    }
    Schema schema;
    String errorMessage = "Error while retrieving schema for subject " + subject + " with version " + version + " from the schema registry";
    try {
        if (schemaRegistry.schemaVersionExists(subject, versionId, true)) {
            if (!permanentDelete && !schemaRegistry.schemaVersionExists(subject, versionId, false)) {
                throw Errors.schemaVersionSoftDeletedException(subject, version);
            }
        }
        schema = schemaRegistry.get(subject, versionId.getVersionId(), true);
        if (schema == null) {
            if (!schemaRegistry.hasSubjects(subject, true)) {
                throw Errors.subjectNotFoundException(subject);
            } else {
                throw Errors.versionNotFoundException(versionId.getVersionId());
            }
        }
    } catch (SchemaRegistryStoreException e) {
        log.debug(errorMessage, e);
        throw Errors.storeException(errorMessage, e);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException(errorMessage, e);
    }
    try {
        Map<String, String> headerProperties = requestHeaderBuilder.buildRequestHeaders(headers, schemaRegistry.config().whitelistHeaders());
        schemaRegistry.deleteSchemaVersionOrForward(headerProperties, subject, schema, permanentDelete);
    } catch (SchemaVersionNotSoftDeletedException e) {
        throw Errors.schemaVersionNotSoftDeletedException(e.getSubject(), e.getVersion());
    } catch (SchemaRegistryTimeoutException e) {
        throw Errors.operationTimeoutException("Delete Schema Version operation timed out", e);
    } catch (SchemaRegistryStoreException e) {
        throw Errors.storeException("Delete Schema Version operation failed while writing" + " to the Kafka store", e);
    } catch (SchemaRegistryRequestForwardingException e) {
        throw Errors.requestForwardingFailedException("Error while forwarding delete schema version request" + " to the leader", e);
    } catch (ReferenceExistsException e) {
        throw Errors.referenceExistsException(e.getMessage());
    } catch (UnknownLeaderException e) {
        throw Errors.unknownLeaderException("Leader not known.", e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException("Error while deleting Schema Version", e);
    }
    asyncResponse.resume(schema.getVersion());
}
Also used : VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) SchemaRegistryRequestForwardingException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException) UnknownLeaderException(io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException) InvalidVersionException(io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException) ReferenceExistsException(io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException) SchemaVersionNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SchemaVersionNotSoftDeletedException) Path(javax.ws.rs.Path) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) DELETE(javax.ws.rs.DELETE) Operation(io.swagger.v3.oas.annotations.Operation)

Example 2 with InvalidVersionException

use of io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException in project schema-registry by confluentinc.

the class SubjectVersionsResource method getReferencedBy.

@GET
@Path("/{version}/referencedby")
@Operation(summary = "Get the schemas that reference the specified schema.", responses = { @ApiResponse(responseCode = "404", description = "Error code 40401 -- Subject not found\n" + "Error code 40402 -- Version not found"), @ApiResponse(responseCode = "422", description = "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the backend data store") })
public List<Integer> getReferencedBy(@Parameter(description = "Name of the subject", required = true) @PathParam("subject") String subject, @Parameter(description = VERSION_PARAM_DESC, required = true) @PathParam("version") String version) {
    Schema schema = getSchemaByVersion(subject, version, true);
    if (schema == null) {
        return new ArrayList<>();
    }
    VersionId versionId = null;
    try {
        versionId = new VersionId(schema.getVersion());
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    }
    String errorMessage = "Error while retrieving schemas that reference schema with subject " + subject + " and version " + version + " from the schema registry";
    try {
        return schemaRegistry.getReferencedBy(schema.getSubject(), versionId);
    } catch (SchemaRegistryStoreException e) {
        log.debug(errorMessage, e);
        throw Errors.storeException(errorMessage, e);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException(errorMessage, e);
    }
}
Also used : VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) InvalidVersionException(io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ArrayList(java.util.ArrayList) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Example 3 with InvalidVersionException

use of io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException in project schema-registry by confluentinc.

the class CompatibilityResource method testCompatibilityBySubjectName.

@POST
@Path("/subjects/{subject}/versions/{version}")
@Operation(summary = "Test input schema against a particular version of a subject's schema for " + "compatibility.", description = "the compatibility level applied for the check is the configured compatibility level " + "for the subject (http:get:: /config/(string: subject)). If this subject's " + "compatibility level was never changed, then the global compatibility level " + "applies (http:get:: /config).", responses = { @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = CompatibilityCheckResponse.class))), @ApiResponse(responseCode = "404", description = "Error code 40401 -- Subject not found\n" + "Error code 40402 -- Version not found"), @ApiResponse(responseCode = "422", description = "Error code 42201 -- Invalid schema or schema type\n" + "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the " + "backend data store") })
@PerformanceMetric("compatibility.subjects.versions.verify")
public void testCompatibilityBySubjectName(@Suspended final AsyncResponse asyncResponse, @Parameter(description = "Subject of the schema version against which compatibility is to " + "be tested", required = true) @PathParam("subject") String subject, @Parameter(description = "Version of the subject's schema against which compatibility is to be " + "tested. Valid values for versionId are between [1,2^31-1] or the string " + "\"latest\"." + "\"latest\" checks compatibility of the input schema with the last registered " + "schema " + "under the specified subject", required = true) @PathParam("version") String version, @Parameter(description = "Schema", required = true) @NotNull RegisterSchemaRequest request, @Parameter(description = "Whether to return detailed error messages") @QueryParam("verbose") boolean verbose) {
    log.info("Testing schema subject {} compatibility between existing version {} and " + "specified version {}, id {}, type {}", subject, version, request.getVersion(), request.getId(), request.getSchemaType());
    subject = QualifiedSubject.normalize(schemaRegistry.tenant(), subject);
    // returns true if posted schema is compatible with the specified version. "latest" is
    // a special version
    List<String> errorMessages;
    VersionId versionId = parseVersionId(version);
    Schema schemaForSpecifiedVersion;
    try {
        // Don't check compatibility against deleted schema
        schemaForSpecifiedVersion = schemaRegistry.get(subject, versionId.getVersionId(), false);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    } catch (SchemaRegistryException e) {
        throw Errors.storeException("Error while retrieving schema for subject " + subject + " and version " + versionId.getVersionId(), e);
    }
    if (schemaForSpecifiedVersion == null && !versionId.isLatest()) {
        throw Errors.versionNotFoundException(versionId.getVersionId());
    }
    Schema schema = new Schema(subject, 0, -1, request.getSchemaType() != null ? request.getSchemaType() : AvroSchema.TYPE, request.getReferences(), request.getSchema());
    try {
        errorMessages = schemaRegistry.isCompatible(subject, schema, schemaForSpecifiedVersion != null ? Collections.singletonList(schemaForSpecifiedVersion) : Collections.emptyList());
    } catch (InvalidSchemaException e) {
        if (verbose) {
            errorMessages = Collections.singletonList(e.getMessage());
        } else {
            throw Errors.invalidSchemaException(e);
        }
    } catch (SchemaRegistryStoreException e) {
        throw Errors.storeException("Error while getting compatibility level for subject " + subject, e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException("Error while getting compatibility level for subject " + subject, e);
    }
    CompatibilityCheckResponse compatibilityCheckResponse = createCompatiblityCheckResponse(errorMessages, verbose);
    asyncResponse.resume(compatibilityCheckResponse);
}
Also used : VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) CompatibilityCheckResponse(io.confluent.kafka.schemaregistry.client.rest.entities.requests.CompatibilityCheckResponse) InvalidSchemaException(io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException) InvalidVersionException(io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) Path(javax.ws.rs.Path) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) POST(javax.ws.rs.POST) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with InvalidVersionException

use of io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException in project schema-registry by confluentinc.

the class SubjectVersionsResource method getSchemaByVersion.

@GET
@Path("/{version}")
@PerformanceMetric("subjects.versions.get-schema")
@Operation(summary = "Get a specific version of the schema registered under this subject.", responses = { @ApiResponse(responseCode = "404", description = "Error code 40401 -- Subject not found\n" + "Error code 40402 -- Version not found"), @ApiResponse(responseCode = "422", description = "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the " + "backend data store") })
public Schema getSchemaByVersion(@Parameter(description = "Name of the subject", required = true) @PathParam("subject") String subject, @Parameter(description = VERSION_PARAM_DESC, required = true) @PathParam("version") String version, @Parameter(description = "Whether to include deleted schema") @QueryParam("deleted") boolean lookupDeletedSchema) {
    subject = QualifiedSubject.normalize(schemaRegistry.tenant(), subject);
    VersionId versionId;
    try {
        versionId = new VersionId(version);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    }
    Schema schema;
    String errorMessage = "Error while retrieving schema for subject " + subject + " with version " + version + " from the schema registry";
    try {
        schema = schemaRegistry.getUsingContexts(subject, versionId.getVersionId(), lookupDeletedSchema);
        if (schema == null) {
            if (!schemaRegistry.hasSubjects(subject, lookupDeletedSchema)) {
                throw Errors.subjectNotFoundException(subject);
            } else {
                throw Errors.versionNotFoundException(versionId.getVersionId());
            }
        }
    } catch (SchemaRegistryStoreException e) {
        log.debug(errorMessage, e);
        throw Errors.storeException(errorMessage, e);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException(errorMessage, e);
    }
    return schema;
}
Also used : VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) InvalidVersionException(io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) Path(javax.ws.rs.Path) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)4 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)4 InvalidVersionException (io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException)4 SchemaRegistryException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException)4 SchemaRegistryStoreException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException)4 VersionId (io.confluent.kafka.schemaregistry.rest.VersionId)4 Operation (io.swagger.v3.oas.annotations.Operation)4 Path (javax.ws.rs.Path)4 PerformanceMetric (io.confluent.rest.annotations.PerformanceMetric)3 GET (javax.ws.rs.GET)2 CompatibilityCheckResponse (io.confluent.kafka.schemaregistry.client.rest.entities.requests.CompatibilityCheckResponse)1 InvalidSchemaException (io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException)1 ReferenceExistsException (io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException)1 SchemaRegistryRequestForwardingException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException)1 SchemaRegistryTimeoutException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException)1 SchemaVersionNotSoftDeletedException (io.confluent.kafka.schemaregistry.exceptions.SchemaVersionNotSoftDeletedException)1 UnknownLeaderException (io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException)1 ArrayList (java.util.ArrayList)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1