Polishing DefaultSchemaRegistryClient
- made RestTemplate final - removed setter Resolves #1222 #1223
This commit is contained in:
committed by
Soby Chacko
parent
c23d6ed99e
commit
0670a36ef2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016 the original author or authors.
|
* Copyright 2016-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -31,7 +31,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
*/
|
*/
|
||||||
public class DefaultSchemaRegistryClient implements SchemaRegistryClient {
|
public class DefaultSchemaRegistryClient implements SchemaRegistryClient {
|
||||||
|
|
||||||
private RestTemplate template;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
private String endpoint = "http://localhost:8990";
|
private String endpoint = "http://localhost:8990";
|
||||||
|
|
||||||
@@ -40,8 +40,8 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DefaultSchemaRegistryClient(RestTemplate restTemplate) {
|
public DefaultSchemaRegistryClient(RestTemplate restTemplate) {
|
||||||
Assert.notNull(restTemplate,"restTemplate cannot be null.");
|
Assert.notNull(restTemplate,"'restTemplate' must not be null.");
|
||||||
this.template = restTemplate;
|
this.restTemplate = restTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getEndpoint() {
|
protected String getEndpoint() {
|
||||||
@@ -54,21 +54,17 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected RestTemplate getRestTemplate() {
|
protected RestTemplate getRestTemplate() {
|
||||||
return this.template;
|
return this.restTemplate;
|
||||||
}
|
|
||||||
|
|
||||||
public void setRestTemplate(RestTemplate restTemplate) {
|
|
||||||
Assert.notNull(restTemplate,"restTemplate cannot be null.");
|
|
||||||
this.template = restTemplate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
@Override
|
@Override
|
||||||
public SchemaRegistrationResponse register(String subject, String format, String schema) {
|
public SchemaRegistrationResponse register(String subject, String format, String schema) {
|
||||||
Map<String, String> requestBody = new HashMap<>();
|
Map<String, String> requestBody = new HashMap<>();
|
||||||
requestBody.put("subject", subject);
|
requestBody.put("subject", subject);
|
||||||
requestBody.put("format", format);
|
requestBody.put("format", format);
|
||||||
requestBody.put("definition", schema);
|
requestBody.put("definition", schema);
|
||||||
ResponseEntity<Map> responseEntity = this.template.postForEntity(this.endpoint, requestBody, Map.class);
|
ResponseEntity<Map> responseEntity = this.restTemplate.postForEntity(this.endpoint, requestBody, Map.class);
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
SchemaRegistrationResponse registrationResponse = new SchemaRegistrationResponse();
|
SchemaRegistrationResponse registrationResponse = new SchemaRegistrationResponse();
|
||||||
Map<String, Object> responseBody = (Map<String, Object>) responseEntity.getBody();
|
Map<String, Object> responseBody = (Map<String, Object>) responseEntity.getBody();
|
||||||
@@ -81,9 +77,10 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient {
|
|||||||
throw new RuntimeException("Failed to register schema: " + responseEntity.toString());
|
throw new RuntimeException("Failed to register schema: " + responseEntity.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
@Override
|
@Override
|
||||||
public String fetch(SchemaReference schemaReference) {
|
public String fetch(SchemaReference schemaReference) {
|
||||||
ResponseEntity<Map> responseEntity = this.template.getForEntity(
|
ResponseEntity<Map> responseEntity = this.restTemplate.getForEntity(
|
||||||
this.endpoint + "/" + schemaReference.getSubject() + "/" + schemaReference
|
this.endpoint + "/" + schemaReference.getSubject() + "/" + schemaReference
|
||||||
.getFormat() + "/v" + schemaReference
|
.getFormat() + "/v" + schemaReference
|
||||||
.getVersion(),
|
.getVersion(),
|
||||||
@@ -94,9 +91,10 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient {
|
|||||||
return (String) responseEntity.getBody().get("definition");
|
return (String) responseEntity.getBody().get("definition");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
@Override
|
@Override
|
||||||
public String fetch(int id) {
|
public String fetch(int id) {
|
||||||
ResponseEntity<Map> responseEntity = this.template.getForEntity(
|
ResponseEntity<Map> responseEntity = this.restTemplate.getForEntity(
|
||||||
this.endpoint + "/schemas/" + id, Map.class);
|
this.endpoint + "/schemas/" + id, Map.class);
|
||||||
if (!responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (!responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
throw new RuntimeException("Failed to fetch schema: " + responseEntity.toString());
|
throw new RuntimeException("Failed to fetch schema: " + responseEntity.toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user