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