smolnar82 commented on a change in pull request #162: KNOX-2023 - Recording
KnoxShellTable builder/filter chain and providing rollback/replay capabilities
using the call history as well as allowing end-users to export JSON without
data (in this case only the call history will be serialized)
URL: https://github.com/apache/knox/pull/162#discussion_r331741457
##########
File path:
gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTableRowDeserializer.java
##########
@@ -46,6 +57,83 @@ protected KnoxShellTableRowDeserializer(Class<?> vc) {
@Override
public KnoxShellTable deserialize(JsonParser parser, DeserializationContext
context) throws IOException, JsonProcessingException {
final TreeNode jsonContent = parser.readValueAsTree();
+ if (jsonContent.get("callHistoryList") != null) {
+ return parseJsonWithCallHistory(jsonContent);
+ } else {
+ return parseJsonWithData(jsonContent);
+ }
+ }
+
+ private KnoxShellTable parseJsonWithCallHistory(TreeNode jsonContent) throws
IOException {
+ final List<KnoxShellTableCall> calls =
parseCallHistoryListJSONNode(jsonContent.get("callHistoryList"));
+ long tempId = KnoxShellTable.getUniqueTableId();
+ KnoxShellTableCallHistory.getInstance().saveCalls(tempId, calls);
+ final KnoxShellTable table =
KnoxShellTableCallHistory.getInstance().replay(tempId, calls.size());
+ KnoxShellTableCallHistory.getInstance().removeCallsById(tempId);
+ return table;
+ }
+
+ private List<KnoxShellTableCall> parseCallHistoryListJSONNode(TreeNode
callHistoryNode) throws IOException {
+ final List<KnoxShellTableCall> callHistoryList = new
LinkedList<KnoxShellTableCall>();
+ TreeNode callNode;
+ Map<Object, Class<?>> params;
+ String invokerClass, method;
+ Boolean builderMethod;
+ for (int i = 0; i < callHistoryNode.size(); i++) {
+ callNode = callHistoryNode.get(i);
+ invokerClass = trimJSONQuotes(callNode.get("invokerClass").toString());
+ method = trimJSONQuotes(callNode.get("method").toString());
+ builderMethod =
Boolean.valueOf(trimJSONQuotes(callNode.get("builderMethod").toString()));
+ params = fetchParameterMap(callNode.get("params"));
+ callHistoryList.add(new KnoxShellTableCall(invokerClass, method,
builderMethod, params));
+ }
+ return callHistoryList;
+ }
+
+ private Map<Object, Class<?>> fetchParameterMap(TreeNode paramsNode) throws
IOException {
+ try {
+ final Map<Object, Class<?>> parameterMap = new HashMap<>();
+ final Iterator<String> paramsFieldNamesIterator = ((ObjectNode)
paramsNode).fieldNames();
+ String parameterValueAsString;
+ Class<?> parameterType;
+ while (paramsFieldNamesIterator.hasNext()) {
+ parameterValueAsString =
trimJSONQuotes(paramsFieldNamesIterator.next());
+ parameterType =
Class.forName(trimJSONQuotes(paramsNode.get(parameterValueAsString).toString()));
+ parameterMap.put(cast(parameterValueAsString, parameterType),
parameterType);
+ }
+ return parameterMap;
+ } catch (Exception e) {
+ throw new IOException("Error while fetching parameters " + paramsNode,
e);
+ }
+ }
+
+ // This may be done in a different way or using a library; I did not find
any (I
+ // did not do a deep search though)
+ private Object cast(String valueAsString, Class<?> type) throws
ParseException {
+ if (String.class.getCanonicalName().equals(type.getCanonicalName())) {
Review comment:
The problem with `instanceof` here is that all values are represented as
Strings when we read them from the JSON -> I believe `instanceof` would not
work here.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services