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