use of org.springframework.cloud.stream.schema.ParsedSchema in project spring-cloud-stream by spring-cloud.
the class AvroSchemaRegistryClientMessageConverter method resolveSchemaForWriting.
@Override
protected Schema resolveSchemaForWriting(Object payload, MessageHeaders headers, MimeType hintedContentType) {
Schema schema;
schema = extractSchemaForWriting(payload);
ParsedSchema parsedSchema = this.cacheManager.getCache(REFERENCE_CACHE_NAME).get(schema, ParsedSchema.class);
if (parsedSchema == null) {
parsedSchema = new ParsedSchema(schema);
this.cacheManager.getCache(REFERENCE_CACHE_NAME).putIfAbsent(schema, parsedSchema);
}
if (parsedSchema.getRegistration() == null) {
SchemaRegistrationResponse response = this.schemaRegistryClient.register(toSubject(schema), AVRO_FORMAT, parsedSchema.getRepresentation());
parsedSchema.setRegistration(response);
}
SchemaReference schemaReference = parsedSchema.getRegistration().getSchemaReference();
DirectFieldAccessor dfa = new DirectFieldAccessor(headers);
@SuppressWarnings("unchecked") Map<String, Object> _headers = (Map<String, Object>) dfa.getPropertyValue("headers");
_headers.put(MessageHeaders.CONTENT_TYPE, "application/" + this.prefix + "." + schemaReference.getSubject() + ".v" + schemaReference.getVersion() + "+" + AVRO_FORMAT);
return schema;
}
use of org.springframework.cloud.stream.schema.ParsedSchema in project spring-cloud-stream by spring-cloud.
the class AvroSchemaRegistryClientMessageConverter method resolveWriterSchemaForDeserialization.
@Override
protected Schema resolveWriterSchemaForDeserialization(MimeType mimeType) {
if (this.readerSchema == null) {
Schema schema = null;
ParsedSchema parsedSchema = null;
SchemaReference schemaReference = extractSchemaReference(mimeType);
if (schemaReference != null) {
parsedSchema = cacheManager.getCache(REFERENCE_CACHE_NAME).get(schemaReference, ParsedSchema.class);
if (parsedSchema == null) {
String schemaContent = this.schemaRegistryClient.fetch(schemaReference);
schema = new Schema.Parser().parse(schemaContent);
parsedSchema = new ParsedSchema(schema);
cacheManager.getCache(REFERENCE_CACHE_NAME).putIfAbsent(schemaReference, parsedSchema);
}
}
return parsedSchema.getSchema();
} else {
return this.readerSchema;
}
}
Aggregations