// Path: src/main/java/com/xti/jenkins/plugin/awslambda/invoke/JsonParameter.java
// public class JsonParameter implements Serializable {
//     private String envVarName;
//     private String jsonPath;
// 
//     public JsonParameter(String envVarName, String jsonPath) {
//         this.envVarName = envVarName;
//         this.jsonPath = jsonPath;
//     }
// 
//     public String getEnvVarName() {
//         return envVarName;
//     }
// 
//     public String getJsonPath() {
//         return jsonPath;
//     }
// 
//     public void setEnvVarName(String envVarName) {
//         this.envVarName = envVarName;
//     }
// 
//     public void setJsonPath(String jsonPath) {
//         this.jsonPath = jsonPath;
//     }
// 
// }
// 
// Path: src/main/java/com/xti/jenkins/plugin/awslambda/util/LogUtils.java
// public class LogUtils {
//     public static String getStackTrace(Throwable t){
//         StringWriter sw = new StringWriter();
//         PrintWriter pw = new PrintWriter(sw);
//         t.printStackTrace(pw);
//         String stackTrace = sw.toString();
//         pw.close();
//         try{
//             sw.close();
//         }catch(Exception ignored){
//         }
//         return stackTrace;
//     }
// }


import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.JsonPathException;
import com.xti.jenkins.plugin.awslambda.invoke.JsonParameter;
import com.xti.jenkins.plugin.awslambda.util.LogUtils;
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
package com.xti.jenkins.plugin.awslambda.service;



public class JsonPathParser {
    private List<JsonParameter> jsonParameters;
    private JenkinsLogger logger;


    public JsonPathParser(List<JsonParameter> jsonParameters, JenkinsLogger logger) {
        this.jsonParameters = jsonParameters;
        this.logger = logger;
    }

    public Map<String, String> parse(String payload){
        Map<String, String> result = new HashMap<String, String>();
        if(StringUtils.isNotEmpty(payload)) {
            DocumentContext documentContext = JsonPath.parse(payload);

            for (JsonParameter jsonParameter : jsonParameters) {
                if (StringUtils.isEmpty(jsonParameter.getJsonPath())) {
                    result.put(jsonParameter.getEnvVarName(), payload);
                } else {
                    try {
                        result.put(jsonParameter.getEnvVarName(), documentContext.read(jsonParameter.getJsonPath()).toString());
                    } catch (JsonPathException jpe) {
                        logger.log("Error while parsing path %s for environment variable %s.%nStacktrace:%n%s%n",
// Path: src/main/java/com/xti/jenkins/plugin/awslambda/invoke/JsonParameter.java
// public class JsonParameter implements Serializable {
//     private String envVarName;
//     private String jsonPath;
// 
//     public JsonParameter(String envVarName, String jsonPath) {
//         this.envVarName = envVarName;
//         this.jsonPath = jsonPath;
//     }
// 
//     public String getEnvVarName() {
//         return envVarName;
//     }
// 
//     public String getJsonPath() {
//         return jsonPath;
//     }
// 
//     public void setEnvVarName(String envVarName) {
//         this.envVarName = envVarName;
//     }
// 
//     public void setJsonPath(String jsonPath) {
//         this.jsonPath = jsonPath;
//     }
// 
// }
// 
// Path: src/main/java/com/xti/jenkins/plugin/awslambda/util/LogUtils.java
// public class LogUtils {
//     public static String getStackTrace(Throwable t){
//         StringWriter sw = new StringWriter();
//         PrintWriter pw = new PrintWriter(sw);
//         t.printStackTrace(pw);
//         String stackTrace = sw.toString();
//         pw.close();
//         try{
//             sw.close();
//         }catch(Exception ignored){
//         }
//         return stackTrace;
//     }
// }


// Path: src/main/java/com/xti/jenkins/plugin/awslambda/service/JsonPathParser.java
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.JsonPathException;
import com.xti.jenkins.plugin.awslambda.invoke.JsonParameter;
import com.xti.jenkins.plugin.awslambda.util.LogUtils;
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

package com.xti.jenkins.plugin.awslambda.service;



public class JsonPathParser {
    private List<JsonParameter> jsonParameters;
    private JenkinsLogger logger;


    public JsonPathParser(List<JsonParameter> jsonParameters, JenkinsLogger logger) {
        this.jsonParameters = jsonParameters;
        this.logger = logger;
    }

    public Map<String, String> parse(String payload){
        Map<String, String> result = new HashMap<String, String>();
        if(StringUtils.isNotEmpty(payload)) {
            DocumentContext documentContext = JsonPath.parse(payload);

            for (JsonParameter jsonParameter : jsonParameters) {
                if (StringUtils.isEmpty(jsonParameter.getJsonPath())) {
                    result.put(jsonParameter.getEnvVarName(), payload);
                } else {
                    try {
                        result.put(jsonParameter.getEnvVarName(), documentContext.read(jsonParameter.getJsonPath()).toString());
                    } catch (JsonPathException jpe) {
                        logger.log("Error while parsing path %s for environment variable %s.%nStacktrace:%n%s%n",
                                jsonParameter.getJsonPath(), jsonParameter.getEnvVarName(), LogUtils.getStackTrace(jpe));