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));
}
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));
}
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));
}
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);
}
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);
}
Aggregations