// Path: rsimulator-core/src/main/java/com/github/bjuvensjo/rsimulator/core/config/Constants.java
// public final class Constants {
//     /**
//      * The REQUEST constant holds the value used as convention for the last part of the name of request files.
//      */
//     public static final String REQUEST = "Request";
//     /**
//      * The RESPONSE constant holds the value used as convention for the last part of the name of response files.
//      */
//     public static final String RESPONSE = "Response";
// 
//     /**
//      * The CONTENT_TYPE constant is used as key in the script vars.
//      */
//     public static final String CONTENT_TYPE = "contentType";
// 
//     /**
//      * The EXCEPTION constant is used as key in the script vars.
//      */
//     public static final String EXCEPTION = "exception";
// 
//     /**
//      * The SIMULATOR_RESPONSE_OPTIONAL constant is used as key in the script vars.
//      */
//     public static final String SIMULATOR_RESPONSE_OPTIONAL = "simulatorResponseOptional";
// 
//     /**
//      * The SIMULATOR_REQUEST constant is used as key in the script vars.
//      */
//     public static final String SIMULATOR_REQUEST = "simulatorRequest";
// 
//     /**
//      * The ROOT_PATH constant is used as key in the script vars.
//      */
//     public static final String ROOT_PATH = "rootPath";
// 
//     /**
//      * The ROOT_RELATIVE_PATH constant is used as key in the script vars.
//      */
//     public static final String ROOT_RELATIVE_PATH = "rootRelativePath";
// 
//     private Constants() {
//     }
// }


import com.google.inject.Singleton;
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static com.github.bjuvensjo.rsimulator.core.config.Constants.*;
package com.github.bjuvensjo.rsimulator.core;




/**
 * SimulatorScriptInterceptor is an interceptor that supports Groovy scripts intercepting invocations of
 * {@link Simulator#service(String, String, String, String, Map...)}.
 * <p>
 * The scripts supported are:
 * <ol>
 * <li>GlobalRequest.groovy; Must be put in the rootPath folder and is applied before the invocation.</li>
 * <li>&lt;TestName&gt;.groovy; Must be put in the same folder as test request and response and is applied first after
 * the invocation. The name of the groovy file must be the same as for the test request, e.g. Test1Request.txt -
 * Test1.groovy.</li>
 * <li>GlobalResponse.groovy; Must be put in the rootPath folder and is applied last after the invocation.</li>
 * </ol>
 * <p>
 * All script have a Map&lt;String, Object&gt; available through the variable vars. The keys contentType, simulatorResponse,
 * request, rootPath, rootRelativePath can be used to access invocation arguments and return value. In addition, the map
 * can be used to communicate arbitrary objects between the Groovy scripts. If a script sets a SimulatorResponse in the
 * vars map, this SimulatorResponse is directly returned.
 */
@Singleton
public class SimulatorScriptInterceptor implements MethodInterceptor {
    private static final int ROOT_PATH_INDEX = 0;
    private static final int ROOT_RELATIVE_PATH_INDEX = 1;
    private static final int SIMULATOR_REQUEST_INDEX = 2;
    private static final int CONTENT_TYPE_INDEX = 3;
    private static final int VARS_INDEX = 4;
// Path: rsimulator-core/src/main/java/com/github/bjuvensjo/rsimulator/core/config/Constants.java
// public final class Constants {
//     /**
//      * The REQUEST constant holds the value used as convention for the last part of the name of request files.
//      */
//     public static final String REQUEST = "Request";
//     /**
//      * The RESPONSE constant holds the value used as convention for the last part of the name of response files.
//      */
//     public static final String RESPONSE = "Response";
// 
//     /**
//      * The CONTENT_TYPE constant is used as key in the script vars.
//      */
//     public static final String CONTENT_TYPE = "contentType";
// 
//     /**
//      * The EXCEPTION constant is used as key in the script vars.
//      */
//     public static final String EXCEPTION = "exception";
// 
//     /**
//      * The SIMULATOR_RESPONSE_OPTIONAL constant is used as key in the script vars.
//      */
//     public static final String SIMULATOR_RESPONSE_OPTIONAL = "simulatorResponseOptional";
// 
//     /**
//      * The SIMULATOR_REQUEST constant is used as key in the script vars.
//      */
//     public static final String SIMULATOR_REQUEST = "simulatorRequest";
// 
//     /**
//      * The ROOT_PATH constant is used as key in the script vars.
//      */
//     public static final String ROOT_PATH = "rootPath";
// 
//     /**
//      * The ROOT_RELATIVE_PATH constant is used as key in the script vars.
//      */
//     public static final String ROOT_RELATIVE_PATH = "rootRelativePath";
// 
//     private Constants() {
//     }
// }


// Path: rsimulator-core/src/main/java/com/github/bjuvensjo/rsimulator/core/SimulatorScriptInterceptor.java
import com.google.inject.Singleton;
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static com.github.bjuvensjo.rsimulator.core.config.Constants.*;

package com.github.bjuvensjo.rsimulator.core;




/**
 * SimulatorScriptInterceptor is an interceptor that supports Groovy scripts intercepting invocations of
 * {@link Simulator#service(String, String, String, String, Map...)}.
 * <p>
 * The scripts supported are:
 * <ol>
 * <li>GlobalRequest.groovy; Must be put in the rootPath folder and is applied before the invocation.</li>
 * <li>&lt;TestName&gt;.groovy; Must be put in the same folder as test request and response and is applied first after
 * the invocation. The name of the groovy file must be the same as for the test request, e.g. Test1Request.txt -
 * Test1.groovy.</li>
 * <li>GlobalResponse.groovy; Must be put in the rootPath folder and is applied last after the invocation.</li>
 * </ol>
 * <p>
 * All script have a Map&lt;String, Object&gt; available through the variable vars. The keys contentType, simulatorResponse,
 * request, rootPath, rootRelativePath can be used to access invocation arguments and return value. In addition, the map
 * can be used to communicate arbitrary objects between the Groovy scripts. If a script sets a SimulatorResponse in the
 * vars map, this SimulatorResponse is directly returned.
 */
@Singleton
public class SimulatorScriptInterceptor implements MethodInterceptor {
    private static final int ROOT_PATH_INDEX = 0;
    private static final int ROOT_RELATIVE_PATH_INDEX = 1;
    private static final int SIMULATOR_REQUEST_INDEX = 2;
    private static final int CONTENT_TYPE_INDEX = 3;
    private static final int VARS_INDEX = 4;
    private static final String GROOVY_PATTERN = com.github.bjuvensjo.rsimulator.core.config.Constants.REQUEST + ".*";