// Path: src/main/java/org/mockenize/exception/ScriptExecutionException.java
// public class ScriptExecutionException extends WebApplicationException {
// 
// 	private static final long serialVersionUID = -4348584639704062868L;
// 
// 	public ScriptExecutionException(Throwable cause) {
// 		super(cause, Response.Status.NOT_FOUND);
// 	}
// }
// 
// Path: src/main/java/org/mockenize/exception/ScriptNotFoundException.java
// public class ScriptNotFoundException extends WebApplicationException {
// 
// 	private static final long serialVersionUID = 3414602294218839377L;
// 
// 	public ScriptNotFoundException(String scriptName) {
// 		super("JS not found: " + scriptName, Response.Status.NOT_FOUND);
// 	}
// }
// 
// Path: src/main/java/org/mockenize/model/ScriptBean.java
// @Data
// public class ScriptBean implements Serializable, Cacheable {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	private String name;
// 
// 	private String value;
// 
// 	public ScriptBean(String name, String value) {
// 		this.name = name;
// 		this.value = value;
// 	}
// 
// 	@Override
// 	public String getKey() {
// 		return this.name;
// 	}
// }
// 
// Path: src/main/java/org/mockenize/repository/ScriptRespository.java
// @Repository
// public class ScriptRespository extends AbstractRepository<ScriptBean> {
// 
// 	private static final String CACHE_KEY = "scripts";
// 
// 	@Autowired
// 	public ScriptRespository(HazelcastInstance hazelcastInstance) {
// 		super(hazelcastInstance, CACHE_KEY);
// 	}
// 
// }


import java.io.IOException;
import java.util.Collection;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.mockenize.exception.ScriptExecutionException;
import org.mockenize.exception.ScriptNotFoundException;
import org.mockenize.model.ScriptBean;
import org.mockenize.repository.ScriptRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
package org.mockenize.service;





@Service
public class ScriptService {

	private static final String ENGINE_NAME = "JavaScript";

	private static final String DEFAUL_FUNCTION_NAME = "_func";

	private static final String PARSE_FUNCTION = "function _func(url, body) {"
			+ "obj=null;try{obj=JSON.parse(body)}catch(ex){};" + "ret = func(url, body, obj);"
			+ "try{return (typeof ret === 'string') ? ret : JSON.stringify(ret)}catch(ex){};" + "return ret};";

	private static final String EMPTY = "";

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private ScriptRespository scriptRespository;

	public ScriptBean getByKey(String jsName) {
		ScriptBean scriptBean = scriptRespository.findByKey(jsName);

		if (scriptBean == null) {
// Path: src/main/java/org/mockenize/exception/ScriptExecutionException.java
// public class ScriptExecutionException extends WebApplicationException {
// 
// 	private static final long serialVersionUID = -4348584639704062868L;
// 
// 	public ScriptExecutionException(Throwable cause) {
// 		super(cause, Response.Status.NOT_FOUND);
// 	}
// }
// 
// Path: src/main/java/org/mockenize/exception/ScriptNotFoundException.java
// public class ScriptNotFoundException extends WebApplicationException {
// 
// 	private static final long serialVersionUID = 3414602294218839377L;
// 
// 	public ScriptNotFoundException(String scriptName) {
// 		super("JS not found: " + scriptName, Response.Status.NOT_FOUND);
// 	}
// }
// 
// Path: src/main/java/org/mockenize/model/ScriptBean.java
// @Data
// public class ScriptBean implements Serializable, Cacheable {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	private String name;
// 
// 	private String value;
// 
// 	public ScriptBean(String name, String value) {
// 		this.name = name;
// 		this.value = value;
// 	}
// 
// 	@Override
// 	public String getKey() {
// 		return this.name;
// 	}
// }
// 
// Path: src/main/java/org/mockenize/repository/ScriptRespository.java
// @Repository
// public class ScriptRespository extends AbstractRepository<ScriptBean> {
// 
// 	private static final String CACHE_KEY = "scripts";
// 
// 	@Autowired
// 	public ScriptRespository(HazelcastInstance hazelcastInstance) {
// 		super(hazelcastInstance, CACHE_KEY);
// 	}
// 
// }


// Path: src/main/java/org/mockenize/service/ScriptService.java
import java.io.IOException;
import java.util.Collection;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.mockenize.exception.ScriptExecutionException;
import org.mockenize.exception.ScriptNotFoundException;
import org.mockenize.model.ScriptBean;
import org.mockenize.repository.ScriptRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

package org.mockenize.service;





@Service
public class ScriptService {

	private static final String ENGINE_NAME = "JavaScript";

	private static final String DEFAUL_FUNCTION_NAME = "_func";

	private static final String PARSE_FUNCTION = "function _func(url, body) {"
			+ "obj=null;try{obj=JSON.parse(body)}catch(ex){};" + "ret = func(url, body, obj);"
			+ "try{return (typeof ret === 'string') ? ret : JSON.stringify(ret)}catch(ex){};" + "return ret};";

	private static final String EMPTY = "";

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private ScriptRespository scriptRespository;

	public ScriptBean getByKey(String jsName) {
		ScriptBean scriptBean = scriptRespository.findByKey(jsName);

		if (scriptBean == null) {
			throw new ScriptNotFoundException(jsName);