Search in sources :

Example 6 with SchemaReference

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

Example 7 with SchemaReference

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

the class AvroSchemaRegistryClientMessageConverter method extractSchemaReference.

private SchemaReference extractSchemaReference(MimeType mimeType) {
    SchemaReference schemaReference = null;
    Matcher schemaMatcher = this.versionedSchema.matcher(mimeType.toString());
    if (schemaMatcher.find()) {
        String subject = schemaMatcher.group(1);
        Integer version = Integer.parseInt(schemaMatcher.group(2));
        schemaReference = new SchemaReference(subject, version, AVRO_FORMAT);
    }
    return schemaReference;
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) Matcher(java.util.regex.Matcher)

Example 8 with SchemaReference

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

the class ConfluentSchemaRegistryClient method register.

@Override
public SchemaRegistrationResponse register(String subject, String format, String schema) {
    Assert.isTrue("avro".equals(format), "Only Avro is supported");
    String path = String.format("/subjects/%s/versions", subject);
    HttpHeaders headers = new HttpHeaders();
    headers.put("Accept", ACCEPT_HEADERS);
    headers.add("Content-Type", "application/json");
    Integer version = null;
    Integer id = null;
    String payload = null;
    try {
        payload = this.mapper.writeValueAsString(Collections.singletonMap("schema", schema));
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Could not parse schema, invalid JSON format", e);
    }
    try {
        HttpEntity<String> request = new HttpEntity<>(payload, headers);
        ResponseEntity<Map> response = this.template.exchange(this.endpoint + path, HttpMethod.POST, request, Map.class);
        id = (Integer) response.getBody().get("id");
        version = getSubjectVersion(subject, payload);
    } catch (HttpStatusCodeException httpException) {
        throw new RuntimeException(String.format("Failed to register subject %s, server replied with status %d", subject, httpException.getStatusCode().value()), httpException);
    }
    SchemaRegistrationResponse schemaRegistrationResponse = new SchemaRegistrationResponse();
    schemaRegistrationResponse.setId(id);
    schemaRegistrationResponse.setSchemaReference(new SchemaReference(subject, version, "avro"));
    return schemaRegistrationResponse;
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map)

Example 9 with SchemaReference

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

the class ConfluentSchemaRegistryClientTests method findByReference.

@Test
public void findByReference() {
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user/versions/1")).andExpect(method(HttpMethod.GET)).andExpect(header("Content-Type", "application/vnd.schemaregistry.v1+json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withSuccess("{\"schema\":\"\"}", MediaType.APPLICATION_JSON));
    ConfluentSchemaRegistryClient client = new ConfluentSchemaRegistryClient(this.restTemplate);
    SchemaReference reference = new SchemaReference("user", 1, "avro");
    String schema = client.fetch(reference);
    Assert.assertEquals("", schema);
    this.mockRestServiceServer.verify();
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) ConfluentSchemaRegistryClient(org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient) Test(org.junit.Test)

Example 10 with SchemaReference

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

the class ConfluentSchemaRegistryClientTests method schemaNotFound.

@Test(expected = SchemaNotFoundException.class)
public void schemaNotFound() {
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user/versions/1")).andExpect(method(HttpMethod.GET)).andExpect(header("Content-Type", "application/vnd.schemaregistry.v1+json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withStatus(HttpStatus.NOT_FOUND));
    ConfluentSchemaRegistryClient client = new ConfluentSchemaRegistryClient(this.restTemplate);
    SchemaReference reference = new SchemaReference("user", 1, "avro");
    String schema = client.fetch(reference);
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) ConfluentSchemaRegistryClient(org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient) Test(org.junit.Test)

Aggregations

SchemaReference (org.springframework.cloud.stream.schema.SchemaReference)10 Map (java.util.Map)4 SchemaRegistrationResponse (org.springframework.cloud.stream.schema.SchemaRegistrationResponse)4 Schema (org.apache.avro.Schema)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Matcher (java.util.regex.Matcher)2 ParsedSchema (org.springframework.cloud.stream.schema.ParsedSchema)2 ConfluentSchemaRegistryClient (org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 User (example.avro.User)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NoOpCacheManager (org.springframework.cache.support.NoOpCacheManager)1 AvroSchemaRegistryClientMessageConverter (org.springframework.cloud.stream.schema.avro.AvroSchemaRegistryClientMessageConverter)1 DefaultSubjectNamingStrategy (org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy)1 DefaultSchemaRegistryClient (org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient)1 SchemaRegistryClient (org.springframework.cloud.stream.schema.client.SchemaRegistryClient)1