浏览代码

优化动态脚本

周浩 9 年之前
父节点
当前提交
b3bc8b88c1

+ 10 - 1
hsweb-web-controller/src/main/java/org/hsweb/web/controller/AopAccessLoggerResolverConfiguration.java

@@ -12,11 +12,13 @@ import org.hsweb.web.core.message.FastJsonHttpMessageConverter;
 import org.hsweb.web.core.message.ResponseMessage;
 import org.hsweb.web.core.utils.WebUtil;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.core.Ordered;
 import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
 import org.hsweb.commons.StringUtils;
 
+import javax.annotation.PostConstruct;
 import java.util.List;
 
 /**
@@ -25,14 +27,21 @@ import java.util.List;
 @Aspect
 @Component
 @Order(Ordered.HIGHEST_PRECEDENCE)
+@ConditionalOnProperty(prefix = "logger.access", value = "on")
 public class AopAccessLoggerResolverConfiguration extends AopAccessLoggerResolver {
 
-    @Autowired
+    @Autowired(required = false)
     private FastJsonHttpMessageConverter fastJsonHttpMessageConverter;
 
     @Autowired(required = false)
     private List<AccessLoggerPersisting> accessLoggerPersisting;
 
+    @PostConstruct
+    private void init() {
+        if (fastJsonHttpMessageConverter == null)
+            fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
+    }
+
     @Around(value = "execution(* org.hsweb.web..controller..*Controller..*(..))||@annotation(org.hsweb.web.core.logger.annotation.AccessLogger)")
     public Object around(ProceedingJoinPoint pjp) throws Throwable {
         LoggerInfo loggerInfo = resolver(pjp);

+ 57 - 5
hsweb-web-controller/src/main/java/org/hsweb/web/controller/script/DynamicScriptController.java

@@ -1,18 +1,24 @@
 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.authorize.annotation.Authorize;
 import org.hsweb.web.bean.po.role.Role;
 import org.hsweb.web.bean.po.script.DynamicScript;
 import org.hsweb.web.controller.GenericController;
 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.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 java.util.HashMap;
+import java.util.Map;
 
 /**
  * 动态脚本控制器,继承自GenericController,使用rest+json
@@ -28,12 +34,14 @@ public class DynamicScriptController extends GenericController<DynamicScript, St
     @Resource
     private DynamicScriptService dynamicScriptService;
 
+    @Resource
+    private DynamicScriptExecuteService dynamicScriptExecuteService;
+
     @Override
     public DynamicScriptService getService() {
         return this.dynamicScriptService;
     }
 
-
     @RequestMapping(value = "/compile", method = {RequestMethod.GET})
     @Authorize(action = "compile")
     public ResponseMessage compileAll() throws Exception {
@@ -47,4 +55,48 @@ public class DynamicScriptController extends GenericController<DynamicScript, St
         dynamicScriptService.compile(id);
         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());
+    }
+
+
 }