Search in sources :

Example 6 with BadRequestException

use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.

the class SchemaRecordSerializerImpl method serializeAvro.

private ByteString serializeAvro(String subject, RegisteredSchema schema, JsonNode data) {
    AvroSchema avroSchema = (AvroSchema) schema.getSchema();
    Object record;
    try {
        record = AvroSchemaUtils.toObject(data, avroSchema);
    } catch (AvroTypeException | IOException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
    return ByteString.copyFrom(avroSerializer.serialize(subject, avroSchema, record));
}
Also used : AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) IOException(java.io.IOException) AvroTypeException(org.apache.avro.AvroTypeException)

Example 7 with BadRequestException

use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.

the class SchemaRecordSerializerImpl method serializeJsonschema.

private ByteString serializeJsonschema(String subject, RegisteredSchema schema, JsonNode data) {
    JsonSchema jsonSchema = (JsonSchema) schema.getSchema();
    Object record;
    try {
        record = JsonSchemaUtils.toObject(data, jsonSchema);
    } catch (IOException | ValidationException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
    return ByteString.copyFrom(jsonschemaSerializer.serialize(subject, jsonSchema, record));
}
Also used : ValidationException(org.everit.json.schema.ValidationException) JsonSchema(io.confluent.kafka.schemaregistry.json.JsonSchema) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) IOException(java.io.IOException)

Example 8 with BadRequestException

use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.

the class SchemaRecordSerializerImpl method serializeProtobuf.

private ByteString serializeProtobuf(String subject, String topicName, RegisteredSchema schema, JsonNode data, boolean isKey) {
    ProtobufSchema protobufSchema = (ProtobufSchema) schema.getSchema();
    Message record;
    try {
        record = (Message) ProtobufSchemaUtils.toObject(data, protobufSchema);
    } catch (IOException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
    return ByteString.copyFrom(protobufSerializer.serialize(subject, topicName, protobufSchema, record, isKey));
}
Also used : Message(com.google.protobuf.Message) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) IOException(java.io.IOException)

Example 9 with BadRequestException

use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.

the class SchemaManagerImpl method getSchemaFromRawSchema.

private RegisteredSchema getSchemaFromRawSchema(String topicName, EmbeddedFormat format, Optional<String> subject, Optional<SubjectNameStrategy> subjectNameStrategy, String rawSchema, boolean isKey) {
    try {
        checkArgument(format.requiresSchema(), "%s does not support schemas.", format);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
    SchemaProvider schemaProvider;
    try {
        schemaProvider = format.getSchemaProvider();
    } catch (UnsupportedOperationException e) {
        throw new BadRequestException(String.format("Raw schema not supported with format = %s", format), e);
    }
    ParsedSchema schema;
    try {
        schema = schemaProvider.parseSchema(rawSchema, /* references= */
        emptyList(), /* isNew= */
        true).orElseThrow(() -> Errors.invalidSchemaException(String.format("Error when parsing raw schema. format = %s, schema = %s", format, rawSchema)));
    } catch (SchemaParseException e) {
        throw new BadRequestException(String.format("Error parsing schema with format = %s", format), e);
    }
    String actualSubject = subject.orElse(subjectNameStrategy.orElse(defaultSubjectNameStrategy).subjectName(topicName, isKey, schema));
    int schemaId;
    try {
        try {
            // Check if the schema already exists first.
            schemaId = schemaRegistryClient.getId(actualSubject, schema);
        } catch (IOException | RestClientException e) {
            // Could not find the schema. We try to register the schema in that case.
            schemaId = schemaRegistryClient.register(actualSubject, schema);
        }
    } catch (IOException | RestClientException e) {
        throw Errors.messageSerializationException(String.format("Error when registering schema. format = %s, subject = %s, schema = %s", format, actualSubject, schema.canonicalString()), e);
    }
    int schemaVersion = getSchemaVersion(actualSubject, schema);
    return RegisteredSchema.create(actualSubject, schemaId, schemaVersion, schema);
}
Also used : SchemaParseException(org.apache.avro.SchemaParseException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) IOException(java.io.IOException)

Example 10 with BadRequestException

use of io.confluent.kafkarest.exceptions.BadRequestException in project kafka-rest by confluentinc.

the class SchemaManagerImpl method getSchemaFromSchemaVersion.

private RegisteredSchema getSchemaFromSchemaVersion(String topicName, Optional<String> subject, Optional<SubjectNameStrategy> subjectNameStrategy, int schemaVersion, boolean isKey) {
    String actualSubject = subject.orElse(getSchemaSubjectUnsafe(topicName, isKey, subjectNameStrategy));
    Schema schema;
    try {
        schema = schemaRegistryClient.getByVersion(actualSubject, schemaVersion, /* lookupDeletedSchema= */
        false);
    } catch (RuntimeException e) {
        throw new BadRequestException(String.format("Schema does not exist for subject: %s, version: %s", actualSubject, schemaVersion), e);
    }
    SchemaProvider schemaProvider;
    try {
        schemaProvider = EmbeddedFormat.forSchemaType(schema.getSchemaType()).getSchemaProvider();
    } catch (UnsupportedOperationException e) {
        throw new BadRequestException(String.format("Schema version not supported for %s", schema.getSchemaType()), e);
    }
    ParsedSchema parsedSchema;
    try {
        parsedSchema = schemaProvider.parseSchema(schema.getSchema(), schema.getReferences(), /* isNew= */
        false).orElseThrow(() -> Errors.invalidSchemaException(String.format("Error when fetching schema by version. subject = %s, version = %d", actualSubject, schemaVersion)));
    } catch (SchemaParseException e) {
        throw new BadRequestException(String.format("Error parsing schema for %s", schema.getSchemaType()), e);
    }
    return RegisteredSchema.create(schema.getSubject(), schema.getId(), schemaVersion, parsedSchema);
}
Also used : SchemaParseException(org.apache.avro.SchemaParseException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema)

Aggregations

BadRequestException (io.confluent.kafkarest.exceptions.BadRequestException)13 Test (org.junit.jupiter.api.Test)7 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)5 SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)5 IOException (java.io.IOException)5 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)3 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)3 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)3 TopicNameStrategy (io.confluent.kafka.serializers.subject.TopicNameStrategy)3 SchemaParseException (org.apache.avro.SchemaParseException)3 AvroSchemaProvider (io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider)2 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)2 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)2 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)2 JsonSchemaProvider (io.confluent.kafka.schemaregistry.json.JsonSchemaProvider)2 ProtobufSchemaProvider (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider)2 EmbeddedFormat (io.confluent.kafkarest.entities.EmbeddedFormat)2 RegisteredSchema (io.confluent.kafkarest.entities.RegisteredSchema)2 Message (com.google.protobuf.Message)1 JsonSchema (io.confluent.kafka.schemaregistry.json.JsonSchema)1