Search in sources :

Example 1 with ParsedSchema

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;
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) Schema(org.apache.avro.Schema) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema) Map(java.util.Map)

Example 2 with ParsedSchema

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;
    }
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) Schema(org.apache.avro.Schema) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema)

Aggregations

Schema (org.apache.avro.Schema)2 ParsedSchema (org.springframework.cloud.stream.schema.ParsedSchema)2 SchemaReference (org.springframework.cloud.stream.schema.SchemaReference)2 Map (java.util.Map)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 SchemaRegistrationResponse (org.springframework.cloud.stream.schema.SchemaRegistrationResponse)1