// Path: src/main/java/octopus/api/plugin/PluginExecutor.java
// public class PluginExecutor {
// 
// 	public Object executePlugin(String pluginName, String pluginClass)
// 	{
// 		return executePlugin(pluginName, pluginClass, null);
// 	}
// 
// 	public Object executePlugin(String pluginName, String pluginClass, JSONObject settings)
// 	{
// 		Plugin plugin;
// 
// 		if(!pluginName.equals("")){
// 			String pluginDir = OctopusEnvironment.PLUGINS_DIR.toString();
// 			Path pathToJar = Paths.get(pluginDir, pluginName).toAbsolutePath();
// 			plugin = PluginLoader.load(pathToJar, pluginClass);
// 		}else{
// 			plugin = PluginLoader.load(pluginClass);
// 		}
// 
// 		if (plugin == null)
// 			throw new RuntimeException(
// 					"Error while loading plugin " + pluginName);
// 
// 		try
// 		{
// 			if(settings != null)
// 				plugin.configure(settings);
// 			plugin.beforeExecution();
// 			plugin.execute();
// 			plugin.afterExecution();
// 			return plugin.result();
// 		} catch (Exception e)
// 		{
// 			System.out.println(e.getMessage());
// 			throw new RuntimeException(e.getMessage());
// 		}
// 	}
// 
// }
// 
// Path: src/main/java/octopus/server/restServer/OctopusRestHandler.java
// public interface OctopusRestHandler {
// 
// 	public Object handle(Request req, Response resp);
// 
// }


import org.json.JSONObject;
import octopus.api.plugin.PluginExecutor;
import octopus.server.restServer.OctopusRestHandler;
import spark.Request;
import spark.Response;
package octopus.server.restServer.handlers;



public class ExecutePluginHandler implements OctopusRestHandler {

	private String pluginName;
	private String pluginClass;
	private JSONObject settings;

	@Override
	public Object handle(Request req, Response resp)
	{
		parseBody(req.body());
		return executePluginAndRespond();
	}

	private void parseBody(String content)
	{
		JSONObject data = new JSONObject(content);

		pluginName = data.getString("plugin");
		pluginClass = data.getString("class");
		settings = data.getJSONObject("settings");

		if(pluginName == null || pluginClass == null)
			throw new RuntimeException("Invalid request: pluginName or pluginClass not given");
	}

	private Object executePluginAndRespond() {
// Path: src/main/java/octopus/api/plugin/PluginExecutor.java
// public class PluginExecutor {
// 
// 	public Object executePlugin(String pluginName, String pluginClass)
// 	{
// 		return executePlugin(pluginName, pluginClass, null);
// 	}
// 
// 	public Object executePlugin(String pluginName, String pluginClass, JSONObject settings)
// 	{
// 		Plugin plugin;
// 
// 		if(!pluginName.equals("")){
// 			String pluginDir = OctopusEnvironment.PLUGINS_DIR.toString();
// 			Path pathToJar = Paths.get(pluginDir, pluginName).toAbsolutePath();
// 			plugin = PluginLoader.load(pathToJar, pluginClass);
// 		}else{
// 			plugin = PluginLoader.load(pluginClass);
// 		}
// 
// 		if (plugin == null)
// 			throw new RuntimeException(
// 					"Error while loading plugin " + pluginName);
// 
// 		try
// 		{
// 			if(settings != null)
// 				plugin.configure(settings);
// 			plugin.beforeExecution();
// 			plugin.execute();
// 			plugin.afterExecution();
// 			return plugin.result();
// 		} catch (Exception e)
// 		{
// 			System.out.println(e.getMessage());
// 			throw new RuntimeException(e.getMessage());
// 		}
// 	}
// 
// }
// 
// Path: src/main/java/octopus/server/restServer/OctopusRestHandler.java
// public interface OctopusRestHandler {
// 
// 	public Object handle(Request req, Response resp);
// 
// }


// Path: src/main/java/octopus/server/restServer/handlers/ExecutePluginHandler.java
import org.json.JSONObject;
import octopus.api.plugin.PluginExecutor;
import octopus.server.restServer.OctopusRestHandler;
import spark.Request;
import spark.Response;

package octopus.server.restServer.handlers;



public class ExecutePluginHandler implements OctopusRestHandler {

	private String pluginName;
	private String pluginClass;
	private JSONObject settings;

	@Override
	public Object handle(Request req, Response resp)
	{
		parseBody(req.body());
		return executePluginAndRespond();
	}

	private void parseBody(String content)
	{
		JSONObject data = new JSONObject(content);

		pluginName = data.getString("plugin");
		pluginClass = data.getString("class");
		settings = data.getJSONObject("settings");

		if(pluginName == null || pluginClass == null)
			throw new RuntimeException("Invalid request: pluginName or pluginClass not given");
	}

	private Object executePluginAndRespond() {
		Object result = new PluginExecutor().executePlugin(pluginName, pluginClass, settings);