This is an automated email from the ASF dual-hosted git repository.

jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5403732  GEODE-5010: have post interceptor uses ResultModel as an 
input if com… (#1974)
5403732 is described below

commit 5403732b3b97aa5bc69befab43090a05d6a5c807
Author: jinmeiliao <[email protected]>
AuthorDate: Mon May 21 14:50:31 2018 -0700

    GEODE-5010: have post interceptor uses ResultModel as an input if com… 
(#1974)
---
 .../internal/cli/CliAroundInterceptor.java         |  8 +++-
 .../internal/cli/commands/ExportConfigCommand.java | 19 ++++-----
 .../internal/cli/commands/QueryInterceptor.java    | 19 +++------
 .../internal/cli/result/ResultBuilder.java         | 17 +-------
 .../internal/cli/result/model/ResultModel.java     | 15 +++++++-
 .../internal/cli/shell/GfshExecutionStrategy.java  | 45 ++++++++++++++--------
 6 files changed, 62 insertions(+), 61 deletions(-)

diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/CliAroundInterceptor.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/CliAroundInterceptor.java
index 59431fe..5b3224d 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/CliAroundInterceptor.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/CliAroundInterceptor.java
@@ -18,6 +18,7 @@ import java.nio.file.Path;
 
 import org.apache.geode.management.internal.cli.result.CommandResult;
 import org.apache.geode.management.internal.cli.result.ResultBuilder;
+import org.apache.geode.management.internal.cli.result.model.ResultModel;
 import org.apache.geode.management.internal.cli.shell.GfshExecutionStrategy;
 
 /**
@@ -43,8 +44,13 @@ public interface CliAroundInterceptor {
    *        after the http response is processed.
    */
   default CommandResult postExecution(GfshParseResult parseResult, 
CommandResult commandResult,
-      Path tempFile) {
+      Path tempFile) throws Exception {
     return commandResult;
   }
 
+  default ResultModel postExecution(GfshParseResult parseResult, ResultModel 
resultModel,
+      Path tempFile) throws Exception {
+    return resultModel;
+  }
+
 }
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportConfigCommand.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportConfigCommand.java
index 090e299..7e10fe3 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportConfigCommand.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportConfigCommand.java
@@ -34,11 +34,10 @@ import 
org.apache.geode.management.internal.cli.GfshParseResult;
 import org.apache.geode.management.internal.cli.functions.CliFunctionResult;
 import org.apache.geode.management.internal.cli.functions.ExportConfigFunction;
 import org.apache.geode.management.internal.cli.i18n.CliStrings;
-import org.apache.geode.management.internal.cli.result.CommandResult;
 import org.apache.geode.management.internal.cli.result.ResultData;
+import org.apache.geode.management.internal.cli.result.model.FileResultModel;
 import org.apache.geode.management.internal.cli.result.model.InfoResultModel;
 import org.apache.geode.management.internal.cli.result.model.ResultModel;
-import org.apache.geode.management.internal.cli.shell.Gfsh;
 import org.apache.geode.management.internal.security.ResourceOperation;
 import org.apache.geode.security.ResourcePermission;
 
@@ -90,9 +89,9 @@ public class ExportConfigCommand extends InternalGfshCommand {
         String propsFileName = result.getMemberIdOrName() + "-gf.properties";
         String[] fileContent = (String[]) result.getSerializables();
         crm.addFile(cacheFileName, fileContent[0].getBytes(), 
ResultData.FILE_TYPE_TEXT,
-            "Downloading Cache XML file: ", false);
+            "Downloading Cache XML file: ");
         crm.addFile(propsFileName, fileContent[1].getBytes(), 
ResultData.FILE_TYPE_TEXT,
-            "Downloading properties file: ", false);
+            "Downloading properties file: ");
       }
     }
 
@@ -140,14 +139,10 @@ public class ExportConfigCommand extends 
InternalGfshCommand {
     }
 
     @Override
-    public CommandResult postExecution(GfshParseResult parseResult, 
CommandResult commandResult,
-        Path tempFile) {
-      if (commandResult.hasIncomingFiles()) {
-        try {
-          commandResult.saveIncomingFiles(saveDirString);
-        } catch (IOException ioex) {
-          Gfsh.getCurrentInstance().logSevere("Unable to export config", ioex);
-        }
+    public ResultModel postExecution(GfshParseResult parseResult, ResultModel 
commandResult,
+        Path tempFile) throws Exception {
+      for (FileResultModel file : commandResult.getFiles().values()) {
+        file.writeFile(saveDirString);
       }
       return commandResult;
     }
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/QueryInterceptor.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/QueryInterceptor.java
index 092c008..59d8f35 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/QueryInterceptor.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/QueryInterceptor.java
@@ -49,15 +49,14 @@ public class QueryInterceptor extends 
AbstractCliAroundInterceptor {
   }
 
   @Override
-  public CommandResult postExecution(GfshParseResult parseResult, 
CommandResult result,
-      Path tempFile) {
+  public ResultModel postExecution(GfshParseResult parseResult, ResultModel 
model, Path tempFile)
+      throws Exception {
     File outputFile = getOutputFile(parseResult);
 
     if (outputFile == null) {
-      return result;
+      return model;
     }
 
-    ResultModel model = ((ModelCommandResult) result).getResultData();
     Map<String, String> sectionResultData =
         model.getDataSection(DataCommandResult.DATA_INFO_SECTION).getContent();
 
@@ -66,16 +65,10 @@ public class QueryInterceptor extends 
AbstractCliAroundInterceptor {
     String rows = sectionResultData.get("Rows");
 
     if ("false".equalsIgnoreCase(resultString)) {
-      return result;
-    }
-
-    try {
-      writeResultTableToFile(outputFile, result);
-      // return a result w/ message explaining limit
-    } catch (IOException e) {
-      throw new RuntimeException(e);
+      return model;
     }
 
+    writeResultTableToFile(outputFile, new ModelCommandResult(model));
     ResultModel newModel = new ResultModel();
     DataResultModel data = 
newModel.addData(DataCommandResult.DATA_INFO_SECTION);
 
@@ -87,7 +80,7 @@ public class QueryInterceptor extends 
AbstractCliAroundInterceptor {
 
     newModel.addInfo().addLine("Query results output to " + 
outputFile.getAbsolutePath());
 
-    return new ModelCommandResult(newModel);
+    return newModel;
   }
 
   private File getOutputFile(ParseResult parseResult) {
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/ResultBuilder.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/ResultBuilder.java
index dccf281..41728ea 100755
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/ResultBuilder.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/ResultBuilder.java
@@ -14,11 +14,9 @@
  */
 package org.apache.geode.management.internal.cli.result;
 
-import java.io.IOException;
 import java.nio.file.Paths;
 import java.util.List;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.commons.lang.StringUtils;
 
 import org.apache.geode.management.cli.Result;
@@ -239,7 +237,7 @@ public class ResultBuilder {
       GfJsonObject jsonObject = new GfJsonObject(json);
 
       if (jsonObject.has("legacy") && !jsonObject.getBoolean("legacy")) {
-        return createModelBasedCommandResult(json);
+        return new ModelCommandResult(ResultModel.fromJson(json));
       }
 
       GfJsonObject data = jsonObject.getJSONObject("data");
@@ -281,19 +279,6 @@ public class ResultBuilder {
     return result;
   }
 
-  public static CommandResult createModelBasedCommandResult(String json) {
-    ObjectMapper mapper = new ObjectMapper();
-
-    ResultModel response;
-    try {
-      response = mapper.readValue(json, ResultModel.class);
-    } catch (IOException iox) {
-      throw new RuntimeException(iox);
-    }
-
-    return new ModelCommandResult(response);
-  }
-
   public static String resultAsString(Result result) {
     StringBuilder builder = new StringBuilder();
 
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/model/ResultModel.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/model/ResultModel.java
index 01972fc..65fa663 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/model/ResultModel.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/model/ResultModel.java
@@ -147,8 +147,7 @@ public class ResultModel {
     this.files = files;
   }
 
-  public void addFile(String fileName, byte[] data, int fileType, String 
message,
-      boolean addTimestampToName) {
+  public void addFile(String fileName, byte[] data, int fileType, String 
message) {
     if (fileType != FILE_TYPE_BINARY && fileType != FILE_TYPE_TEXT) {
       throw new IllegalArgumentException("Unsupported file type is 
specified.");
     }
@@ -261,6 +260,18 @@ public class ResultModel {
   // static convenience methods
   // ********************************************
 
+  public static ResultModel fromJson(String json) {
+    ObjectMapper mapper = new ObjectMapper();
+
+    ResultModel response;
+    try {
+      response = mapper.readValue(json, ResultModel.class);
+    } catch (IOException iox) {
+      throw new RuntimeException(iox);
+    }
+    return response;
+  }
+
   public static ResultModel createCommandProcessingError(String message) {
     return createError("Error processing command: " + message);
   }
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategy.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategy.java
index 9b5eb48..3e82635 100755
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategy.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategy.java
@@ -145,8 +145,6 @@ public class GfshExecutionStrategy implements 
ExecutionStrategy {
    * @throws IllegalStateException if gfsh doesn't have an active connection.
    */
   private Result executeOnRemote(GfshParseResult parseResult) {
-    CommandResult commandResult = null;
-    Object response = null;
     Path tempFile = null;
 
     if (!shell.isConnectedAndReady()) {
@@ -162,6 +160,7 @@ public class GfshExecutionStrategy implements 
ExecutionStrategy {
     CliAroundInterceptor interceptor = null;
 
     String interceptorClass = getInterceptor(parseResult.getMethod());
+    boolean useResultModel = false;
 
     // 1. Pre Remote Execution
     if (!CliMetaData.ANNOTATION_NULL_VALUE.equals(interceptorClass)) {
@@ -178,6 +177,7 @@ public class GfshExecutionStrategy implements 
ExecutionStrategy {
 
       Object preExecResult = interceptor.preExecution(parseResult);
       if (preExecResult instanceof ResultModel) {
+        useResultModel = true;
         if (((ResultModel) preExecResult).getStatus() != Status.OK) {
           return new ModelCommandResult((ResultModel) preExecResult);
         }
@@ -195,6 +195,7 @@ public class GfshExecutionStrategy implements 
ExecutionStrategy {
     }
 
     // 2. Remote Execution
+    Object response = null;
     final Map<String, String> env = shell.getEnv();
     try {
       response = shell.getOperationInvoker()
@@ -216,14 +217,17 @@ public class GfshExecutionStrategy implements 
ExecutionStrategy {
       env.clear();
     }
 
-    // the response could be a string which is a json representation of the 
CommandResult object
+    // the response could be a string which is a json representation of the
+    // CommandResult/ResultModel object
     // it can also be a Path to a temp file downloaded from the rest http 
request
+    Object commandResult = null;
     if (response instanceof String) {
       try {
-        // TODO: stuff when failedToPersist...
-        // TODO: stuff for debug info...
-        commandResult = ResultBuilder.createModelBasedCommandResult((String) 
response);
+        // if it's ResultModel
+        commandResult = ResultModel.fromJson((String) response);
+        useResultModel = true;
       } catch (Exception ex) {
+        // if it's a CommandResult
         CommandResponse commandResponse =
             CommandResponseBuilder.prepareCommandResponseFromJson((String) 
response);
 
@@ -249,17 +253,17 @@ public class GfshExecutionStrategy implements 
ExecutionStrategy {
 
     // 3. Post Remote Execution
     if (interceptor != null) {
-      CommandResult postExecResult =
-          interceptor.postExecution(parseResult, commandResult, tempFile);
-      if (postExecResult != null) {
-        if (Status.ERROR.equals(postExecResult.getStatus())) {
-          if (logWrapper.infoEnabled()) {
-            logWrapper.info("Post execution Result :: " + postExecResult);
-          }
-        } else if (logWrapper.fineEnabled()) {
-          logWrapper.fine("Post execution Result :: " + postExecResult);
+      try {
+        if (useResultModel) {
+          commandResult =
+              interceptor.postExecution(parseResult, (ResultModel) 
commandResult, tempFile);
+        } else {
+          commandResult =
+              interceptor.postExecution(parseResult, (CommandResult) 
commandResult, tempFile);
         }
-        commandResult = postExecResult;
+      } catch (Exception e) {
+        logWrapper.severe("error running post interceptor", e);
+        commandResult = ResultBuilder.createGemFireErrorResult(e.getMessage());
       }
     }
 
@@ -268,6 +272,13 @@ public class GfshExecutionStrategy implements 
ExecutionStrategy {
           .createGemFireErrorResult("Unable to build commandResult using the 
remote response.");
     }
 
-    return commandResult;
+    CommandResult gfshResult;
+    if (commandResult instanceof ResultModel) {
+      gfshResult = new ModelCommandResult((ResultModel) commandResult);
+    } else {
+      gfshResult = (CommandResult) commandResult;
+    }
+
+    return gfshResult;
   }
 }

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to