|
@@ -1,18 +1,24 @@
|
|
package org.hsweb.web.controller.script;
|
|
package org.hsweb.web.controller.script;
|
|
|
|
|
|
|
|
+import org.hsweb.expands.script.engine.DynamicScriptEngine;
|
|
|
|
+import org.hsweb.expands.script.engine.DynamicScriptEngineFactory;
|
|
|
|
+import org.hsweb.expands.script.engine.ExecuteResult;
|
|
|
|
+import org.hsweb.web.bean.common.QueryParam;
|
|
|
|
+import org.hsweb.web.core.exception.NotFoundException;
|
|
import org.hsweb.web.core.logger.annotation.AccessLogger;
|
|
import org.hsweb.web.core.logger.annotation.AccessLogger;
|
|
import org.hsweb.web.core.authorize.annotation.Authorize;
|
|
import org.hsweb.web.core.authorize.annotation.Authorize;
|
|
import org.hsweb.web.bean.po.role.Role;
|
|
import org.hsweb.web.bean.po.role.Role;
|
|
import org.hsweb.web.bean.po.script.DynamicScript;
|
|
import org.hsweb.web.bean.po.script.DynamicScript;
|
|
import org.hsweb.web.controller.GenericController;
|
|
import org.hsweb.web.controller.GenericController;
|
|
import org.hsweb.web.core.message.ResponseMessage;
|
|
import org.hsweb.web.core.message.ResponseMessage;
|
|
|
|
+import org.hsweb.web.core.utils.WebUtil;
|
|
|
|
+import org.hsweb.web.service.script.DynamicScriptExecuteService;
|
|
import org.hsweb.web.service.script.DynamicScriptService;
|
|
import org.hsweb.web.service.script.DynamicScriptService;
|
|
-import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 动态脚本控制器,继承自GenericController,使用rest+json
|
|
* 动态脚本控制器,继承自GenericController,使用rest+json
|
|
@@ -28,12 +34,14 @@ public class DynamicScriptController extends GenericController<DynamicScript, St
|
|
@Resource
|
|
@Resource
|
|
private DynamicScriptService dynamicScriptService;
|
|
private DynamicScriptService dynamicScriptService;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private DynamicScriptExecuteService dynamicScriptExecuteService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public DynamicScriptService getService() {
|
|
public DynamicScriptService getService() {
|
|
return this.dynamicScriptService;
|
|
return this.dynamicScriptService;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
@RequestMapping(value = "/compile", method = {RequestMethod.GET})
|
|
@RequestMapping(value = "/compile", method = {RequestMethod.GET})
|
|
@Authorize(action = "compile")
|
|
@Authorize(action = "compile")
|
|
public ResponseMessage compileAll() throws Exception {
|
|
public ResponseMessage compileAll() throws Exception {
|
|
@@ -47,4 +55,48 @@ public class DynamicScriptController extends GenericController<DynamicScript, St
|
|
dynamicScriptService.compile(id);
|
|
dynamicScriptService.compile(id);
|
|
return ResponseMessage.ok("success");
|
|
return ResponseMessage.ok("success");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/exec/{id:.+}", method = {RequestMethod.GET})
|
|
|
|
+ @Authorize(action = "exec")
|
|
|
|
+ public ResponseMessage execGet(@PathVariable("id") String id,
|
|
|
|
+ QueryParam queryParam,
|
|
|
|
+ @RequestParam(required = false) Map<String, Object> param) throws Throwable {
|
|
|
|
+ if (param == null)
|
|
|
|
+ param = new HashMap<>();
|
|
|
|
+ param.put("queryParam", queryParam);
|
|
|
|
+ param.put("user", WebUtil.getLoginUser());
|
|
|
|
+
|
|
|
|
+ Object data = dynamicScriptExecuteService.exec(id, param);
|
|
|
|
+ return ResponseMessage.ok(data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/exec/{id:.+}", method = {RequestMethod.POST, RequestMethod.PUT})
|
|
|
|
+ @Authorize(action = "exec")
|
|
|
|
+ public ResponseMessage execPost(@PathVariable("id") String id,
|
|
|
|
+ @RequestBody Map<String, Object> param) throws Throwable {
|
|
|
|
+ if (param == null)
|
|
|
|
+ param = new HashMap<>();
|
|
|
|
+ param.put("user", WebUtil.getLoginUser());
|
|
|
|
+ Object data = dynamicScriptExecuteService.exec(id, param);
|
|
|
|
+ return ResponseMessage.ok(data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/exec/runtime/{type}", method = RequestMethod.POST)
|
|
|
|
+ @Authorize(action = "runtime")
|
|
|
|
+ public ResponseMessage runtime(@PathVariable("type") String type,
|
|
|
|
+ @RequestBody String script) throws Throwable {
|
|
|
|
+ DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(type);
|
|
|
|
+ if (engine == null) {
|
|
|
|
+ throw new NotFoundException("不支持的动态脚本引擎!");
|
|
|
|
+ }
|
|
|
|
+ String id = "script.runtime";
|
|
|
|
+ engine.compile(id, script);
|
|
|
|
+ ExecuteResult result = engine.execute(id);
|
|
|
|
+ if (!result.isSuccess()) {
|
|
|
|
+ if (result.getException() != null) throw result.getException();
|
|
|
|
+ }
|
|
|
|
+ return ResponseMessage.ok(result.getResult());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|