[#207]: PR feedback
This commit is contained in:
parent
232a17b80d
commit
64e2caf55c
8 changed files with 44 additions and 73 deletions
|
@ -16,9 +16,9 @@ import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
public abstract class BaseStrategy {
|
public abstract class BaseStrategy {
|
||||||
protected static final String ksqlRequestPath = "/ksql";
|
protected static final String KSQL_REQUEST_PATH = "/ksql";
|
||||||
protected static final String queryRequestPath = "/query";
|
protected static final String QUERY_REQUEST_PATH = "/query";
|
||||||
private static final String mappingExceptionMessage = "KSQL DB response mapping error";
|
private static final String MAPPING_EXCEPTION_ERROR = "KSQL DB response mapping error";
|
||||||
protected String host = null;
|
protected String host = null;
|
||||||
protected KsqlCommand ksqlCommand = null;
|
protected KsqlCommand ksqlCommand = null;
|
||||||
|
|
||||||
|
@ -47,36 +47,39 @@ public abstract class BaseStrategy {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected KsqlCommandResponse serializeTableResponse(JsonNode response, String path) {
|
protected String getRequestPath() {
|
||||||
if (response.isArray() && response.size() > 0) {
|
return BaseStrategy.KSQL_REQUEST_PATH;
|
||||||
KsqlCommandResponse commandResponse = new KsqlCommandResponse();
|
|
||||||
JsonNode first = response.get(0);
|
|
||||||
JsonNode items = first.path(path);
|
|
||||||
Table table = items.isArray() ? getTableFromArray(items) : getTableFromObject(items);
|
|
||||||
return commandResponse.data(table);
|
|
||||||
}
|
|
||||||
throw new UnprocessableEntityException(mappingExceptionMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected KsqlCommandResponse serializeMessageResponse(JsonNode response, String path) {
|
protected KsqlCommandResponse serializeTableResponse(JsonNode response, String key) {
|
||||||
if (response.isArray() && response.size() > 0) {
|
JsonNode item = getResponseFirstItemValue(response, key);
|
||||||
KsqlCommandResponse commandResponse = new KsqlCommandResponse();
|
Table table = item.isArray() ? getTableFromArray(item) : getTableFromObject(item);
|
||||||
JsonNode first = response.get(0);
|
return (new KsqlCommandResponse()).data(table);
|
||||||
JsonNode item = first.path(path);
|
}
|
||||||
return commandResponse.message(getMessageFromObject(item));
|
|
||||||
}
|
protected KsqlCommandResponse serializeMessageResponse(JsonNode response, String key) {
|
||||||
throw new UnprocessableEntityException(mappingExceptionMessage);
|
JsonNode item = getResponseFirstItemValue(response, key);
|
||||||
|
return (new KsqlCommandResponse()).message(getMessageFromObject(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected KsqlCommandResponse serializeQueryResponse(JsonNode response) {
|
protected KsqlCommandResponse serializeQueryResponse(JsonNode response) {
|
||||||
if (response.isArray() && response.size() > 0) {
|
if (response.isArray() && response.size() > 0) {
|
||||||
KsqlCommandResponse commandResponse = new KsqlCommandResponse();
|
|
||||||
Table table = (new Table())
|
Table table = (new Table())
|
||||||
.headers(getQueryResponseHeader(response))
|
.headers(getQueryResponseHeader(response))
|
||||||
.rows(getQueryResponseRows(response));
|
.rows(getQueryResponseRows(response));
|
||||||
return commandResponse.data(table);
|
return (new KsqlCommandResponse()).data(table);
|
||||||
}
|
}
|
||||||
throw new UnprocessableEntityException(mappingExceptionMessage);
|
throw new UnprocessableEntityException(MAPPING_EXCEPTION_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonNode getResponseFirstItemValue(JsonNode response, String key) {
|
||||||
|
if (response.isArray() && response.size() > 0) {
|
||||||
|
JsonNode first = response.get(0);
|
||||||
|
if (first.has(key)) {
|
||||||
|
return first.path(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new UnprocessableEntityException(MAPPING_EXCEPTION_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getQueryResponseHeader(JsonNode response) {
|
private List<String> getQueryResponseHeader(JsonNode response) {
|
||||||
|
@ -124,7 +127,7 @@ public abstract class BaseStrategy {
|
||||||
if (node.isObject() && node.has("message")) {
|
if (node.isObject() && node.has("message")) {
|
||||||
return node.get("message").asText();
|
return node.get("message").asText();
|
||||||
}
|
}
|
||||||
throw new UnprocessableEntityException(mappingExceptionMessage);
|
throw new UnprocessableEntityException(MAPPING_EXCEPTION_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<List<String>> getTableRows(JsonNode node, List<String> keys) {
|
private List<List<String>> getTableRows(JsonNode node, List<String> keys) {
|
||||||
|
@ -140,7 +143,7 @@ public abstract class BaseStrategy {
|
||||||
if (node.isArray() && node.size() > 0) {
|
if (node.isArray() && node.size() > 0) {
|
||||||
return StreamSupport.stream(node.spliterator(), false);
|
return StreamSupport.stream(node.spliterator(), false);
|
||||||
}
|
}
|
||||||
throw new UnprocessableEntityException(mappingExceptionMessage);
|
throw new UnprocessableEntityException(MAPPING_EXCEPTION_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getJsonObjectKeys(JsonNode node) {
|
private List<String> getJsonObjectKeys(JsonNode node) {
|
||||||
|
@ -149,7 +152,7 @@ public abstract class BaseStrategy {
|
||||||
Spliterators.spliteratorUnknownSize(node.fieldNames(), Spliterator.ORDERED), false
|
Spliterators.spliteratorUnknownSize(node.fieldNames(), Spliterator.ORDERED), false
|
||||||
).collect(Collectors.toList());
|
).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
throw new UnprocessableEntityException(mappingExceptionMessage);
|
throw new UnprocessableEntityException(MAPPING_EXCEPTION_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getJsonObjectValues(JsonNode node) {
|
private List<String> getJsonObjectValues(JsonNode node) {
|
||||||
|
@ -159,7 +162,5 @@ public abstract class BaseStrategy {
|
||||||
|
|
||||||
public abstract KsqlCommandResponse serializeResponse(JsonNode response);
|
public abstract KsqlCommandResponse serializeResponse(JsonNode response);
|
||||||
|
|
||||||
protected abstract String getRequestPath();
|
|
||||||
|
|
||||||
protected abstract String getTestRegExp();
|
protected abstract String getTestRegExp();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,11 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class CreateStrategy extends BaseStrategy {
|
public class CreateStrategy extends BaseStrategy {
|
||||||
private static final String responseValueKey = "commandStatus";
|
private static final String RESPONSE_VALUE_KEY = "commandStatus";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
||||||
return serializeMessageResponse(response, responseValueKey);
|
return serializeMessageResponse(response, RESPONSE_VALUE_KEY);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getRequestPath() {
|
|
||||||
return BaseStrategy.ksqlRequestPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -6,16 +6,11 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class DescribeStrategy extends BaseStrategy {
|
public class DescribeStrategy extends BaseStrategy {
|
||||||
private static final String responseValueKey = "sourceDescription";
|
private static final String RESPONSE_VALUE_KEY = "sourceDescription";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
||||||
return serializeTableResponse(response, responseValueKey);
|
return serializeTableResponse(response, RESPONSE_VALUE_KEY);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getRequestPath() {
|
|
||||||
return BaseStrategy.ksqlRequestPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -6,16 +6,11 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class DropStrategy extends BaseStrategy {
|
public class DropStrategy extends BaseStrategy {
|
||||||
private static final String responseValueKey = "commandStatus";
|
private static final String RESPONSE_VALUE_KEY = "commandStatus";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
||||||
return serializeMessageResponse(response, responseValueKey);
|
return serializeMessageResponse(response, RESPONSE_VALUE_KEY);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getRequestPath() {
|
|
||||||
return BaseStrategy.ksqlRequestPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -6,16 +6,11 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class ExplainStrategy extends BaseStrategy {
|
public class ExplainStrategy extends BaseStrategy {
|
||||||
private static final String responseValueKey = "queryDescription";
|
private static final String RESPONSE_VALUE_KEY = "queryDescription";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
||||||
return serializeTableResponse(response, responseValueKey);
|
return serializeTableResponse(response, RESPONSE_VALUE_KEY);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getRequestPath() {
|
|
||||||
return BaseStrategy.ksqlRequestPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class SelectStrategy extends BaseStrategy {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRequestPath() {
|
protected String getRequestPath() {
|
||||||
return BaseStrategy.queryRequestPath;
|
return BaseStrategy.QUERY_REQUEST_PATH;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -8,9 +8,9 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class ShowStrategy extends BaseStrategy {
|
public class ShowStrategy extends BaseStrategy {
|
||||||
private final List<String> showStatements =
|
private static final List<String> SHOW_STATEMENTS =
|
||||||
List.of("functions", "topics", "streams", "tables", "queries", "properties");
|
List.of("functions", "topics", "streams", "tables", "queries", "properties");
|
||||||
private final List<String> listStatements =
|
private static final List<String> LIST_STATEMENTS =
|
||||||
List.of("functions", "topics", "streams", "tables");
|
List.of("functions", "topics", "streams", "tables");
|
||||||
private String responseValueKey = "";
|
private String responseValueKey = "";
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ public class ShowStrategy extends BaseStrategy {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(String sql) {
|
public boolean test(String sql) {
|
||||||
Optional<String> statement = showStatements.stream()
|
Optional<String> statement = SHOW_STATEMENTS.stream()
|
||||||
.filter(s -> testSql(sql, getShowRegExp(s)) || testSql(sql, getListRegExp(s)))
|
.filter(s -> testSql(sql, getShowRegExp(s)) || testSql(sql, getListRegExp(s)))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
if (statement.isPresent()) {
|
if (statement.isPresent()) {
|
||||||
|
@ -31,11 +31,6 @@ public class ShowStrategy extends BaseStrategy {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getRequestPath() {
|
|
||||||
return BaseStrategy.ksqlRequestPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getTestRegExp() {
|
protected String getTestRegExp() {
|
||||||
return "";
|
return "";
|
||||||
|
@ -46,7 +41,7 @@ public class ShowStrategy extends BaseStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getListRegExp(String key) {
|
protected String getListRegExp(String key) {
|
||||||
if (listStatements.contains(key)) {
|
if (LIST_STATEMENTS.contains(key)) {
|
||||||
return "list " + key + ";";
|
return "list " + key + ";";
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
|
|
|
@ -6,16 +6,11 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class TerminateStrategy extends BaseStrategy {
|
public class TerminateStrategy extends BaseStrategy {
|
||||||
private static final String responseValueKey = "commandStatus";
|
private static final String RESPONSE_VALUE_KEY = "commandStatus";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
public KsqlCommandResponse serializeResponse(JsonNode response) {
|
||||||
return serializeMessageResponse(response, responseValueKey);
|
return serializeMessageResponse(response, RESPONSE_VALUE_KEY);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getRequestPath() {
|
|
||||||
return BaseStrategy.ksqlRequestPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Add table
Reference in a new issue