Search in sources :

Example 1 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse 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 SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class CachingRegistryClient method register.

@Override
public SchemaRegistrationResponse register(String subject, String format, String schema) {
    SchemaRegistrationResponse response = delegate.register(subject, format, schema);
    cacheManager.getCache(ID_CACHE).put(response.getId(), schema);
    cacheManager.getCache(REF_CACHE).put(response.getSchemaReference(), schema);
    return response;
}
Also used : SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse)

Example 3 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class DefaultSchemaRegistryClient method register.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public SchemaRegistrationResponse register(String subject, String format, String schema) {
    Map<String, String> requestBody = new HashMap<>();
    requestBody.put("subject", subject);
    requestBody.put("format", format);
    requestBody.put("definition", schema);
    ResponseEntity<Map> responseEntity = this.restTemplate.postForEntity(this.endpoint, requestBody, Map.class);
    if (responseEntity.getStatusCode().is2xxSuccessful()) {
        SchemaRegistrationResponse registrationResponse = new SchemaRegistrationResponse();
        Map<String, Object> responseBody = (Map<String, Object>) responseEntity.getBody();
        registrationResponse.setId((Integer) responseBody.get("id"));
        registrationResponse.setSchemaReference(new SchemaReference(subject, (Integer) responseBody.get("version"), responseBody.get("format").toString()));
        return registrationResponse;
    }
    throw new RuntimeException("Failed to register schema: " + responseEntity.toString());
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) HashMap(java.util.HashMap) SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class ConfluentSchemaRegistryClientTests method responseErrorFetch.

@Test
public void responseErrorFetch() {
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user/versions")).andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withSuccess("{\"id\":101}", MediaType.APPLICATION_JSON));
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user")).andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withBadRequest());
    ConfluentSchemaRegistryClient client = new ConfluentSchemaRegistryClient(this.restTemplate);
    Exception expected = null;
    try {
        SchemaRegistrationResponse response = client.register("user", "avro", "{}");
    } catch (Exception e) {
        expected = e;
    }
    Assert.assertTrue(expected instanceof RuntimeException);
    Assert.assertTrue(expected.getCause() instanceof HttpStatusCodeException);
    this.mockRestServiceServer.verify();
}
Also used : SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) ConfluentSchemaRegistryClient(org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient) SchemaNotFoundException(org.springframework.cloud.stream.schema.SchemaNotFoundException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) Test(org.junit.Test)

Example 5 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class StubSchemaRegistryClient method register.

@Override
public SchemaRegistrationResponse register(String subject, String format, String schema) {
    if (!this.storedSchemas.containsKey(subject)) {
        this.storedSchemas.put(subject, new TreeMap<Integer, SchemaWithId>());
    }
    Map<Integer, SchemaWithId> schemaVersions = this.storedSchemas.get(subject);
    for (Map.Entry<Integer, SchemaWithId> integerSchemaEntry : schemaVersions.entrySet()) {
        if (integerSchemaEntry.getValue().getSchema().equals(schema)) {
            SchemaRegistrationResponse schemaRegistrationResponse = new SchemaRegistrationResponse();
            schemaRegistrationResponse.setId(integerSchemaEntry.getValue().getId());
            schemaRegistrationResponse.setSchemaReference(new SchemaReference(subject, integerSchemaEntry.getKey(), AvroSchemaRegistryClientMessageConverter.AVRO_FORMAT));
            return schemaRegistrationResponse;
        }
    }
    int nextVersion = schemaVersions.size() + 1;
    int id = this.index.incrementAndGet();
    schemaVersions.put(nextVersion, new SchemaWithId(id, schema));
    SchemaRegistrationResponse schemaRegistrationResponse = new SchemaRegistrationResponse();
    schemaRegistrationResponse.setId(this.index.getAndIncrement());
    schemaRegistrationResponse.setSchemaReference(new SchemaReference(subject, nextVersion, AvroSchemaRegistryClientMessageConverter.AVRO_FORMAT));
    this.schemasById.put(id, schema);
    return schemaRegistrationResponse;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

SchemaRegistrationResponse (org.springframework.cloud.stream.schema.SchemaRegistrationResponse)9 Map (java.util.Map)4 Test (org.junit.Test)4 SchemaReference (org.springframework.cloud.stream.schema.SchemaReference)4 ConfluentSchemaRegistryClient (org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient)4 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)3 HashMap (java.util.HashMap)2 SchemaNotFoundException (org.springframework.cloud.stream.schema.SchemaNotFoundException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Schema (org.apache.avro.Schema)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ParsedSchema (org.springframework.cloud.stream.schema.ParsedSchema)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1