|
@@ -14,16 +14,23 @@ import org.ssssssss.magicapi.config.WebSocketSessionManager;
|
|
import org.ssssssss.magicapi.controller.MagicWebSocketDispatcher;
|
|
import org.ssssssss.magicapi.controller.MagicWebSocketDispatcher;
|
|
import org.ssssssss.magicapi.event.EventAction;
|
|
import org.ssssssss.magicapi.event.EventAction;
|
|
import org.ssssssss.magicapi.event.MagicEvent;
|
|
import org.ssssssss.magicapi.event.MagicEvent;
|
|
|
|
+import org.ssssssss.magicapi.exception.MagicAPIException;
|
|
import org.ssssssss.magicapi.exception.MagicResourceNotFoundException;
|
|
import org.ssssssss.magicapi.exception.MagicResourceNotFoundException;
|
|
import org.ssssssss.magicapi.model.*;
|
|
import org.ssssssss.magicapi.model.*;
|
|
import org.ssssssss.magicapi.provider.MagicAPIService;
|
|
import org.ssssssss.magicapi.provider.MagicAPIService;
|
|
import org.ssssssss.magicapi.provider.ResultProvider;
|
|
import org.ssssssss.magicapi.provider.ResultProvider;
|
|
|
|
+import org.ssssssss.magicapi.script.ScriptManager;
|
|
import org.ssssssss.magicapi.service.MagicResourceService;
|
|
import org.ssssssss.magicapi.service.MagicResourceService;
|
|
|
|
+import org.ssssssss.magicapi.service.impl.FunctionMagicDynamicRegistry;
|
|
|
|
+import org.ssssssss.magicapi.service.impl.RequestMagicDynamicRegistry;
|
|
|
|
+import org.ssssssss.magicapi.utils.PathUtils;
|
|
import org.ssssssss.magicapi.utils.SignUtils;
|
|
import org.ssssssss.magicapi.utils.SignUtils;
|
|
|
|
+import org.ssssssss.script.MagicScriptContext;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstants {
|
|
public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstants {
|
|
|
|
|
|
@@ -32,47 +39,45 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
|
private final ResultProvider resultProvider;
|
|
private final ResultProvider resultProvider;
|
|
private final String instanceId;
|
|
private final String instanceId;
|
|
private final MagicResourceService resourceService;
|
|
private final MagicResourceService resourceService;
|
|
-
|
|
|
|
private final ApplicationEventPublisher publisher;
|
|
private final ApplicationEventPublisher publisher;
|
|
|
|
+ private final RequestMagicDynamicRegistry requestMagicDynamicRegistry;
|
|
|
|
+ private final FunctionMagicDynamicRegistry functionMagicDynamicRegistry;
|
|
|
|
|
|
public DefaultMagicAPIService(ResultProvider resultProvider,
|
|
public DefaultMagicAPIService(ResultProvider resultProvider,
|
|
String instanceId,
|
|
String instanceId,
|
|
MagicResourceService resourceService,
|
|
MagicResourceService resourceService,
|
|
|
|
+ RequestMagicDynamicRegistry requestMagicDynamicRegistry,
|
|
|
|
+ FunctionMagicDynamicRegistry functionMagicDynamicRegistry,
|
|
boolean throwException,
|
|
boolean throwException,
|
|
ApplicationEventPublisher publisher) {
|
|
ApplicationEventPublisher publisher) {
|
|
this.resultProvider = resultProvider;
|
|
this.resultProvider = resultProvider;
|
|
|
|
+ this.requestMagicDynamicRegistry = requestMagicDynamicRegistry;
|
|
|
|
+ this.functionMagicDynamicRegistry = functionMagicDynamicRegistry;
|
|
this.throwException = throwException;
|
|
this.throwException = throwException;
|
|
this.resourceService = resourceService;
|
|
this.resourceService = resourceService;
|
|
this.instanceId = instanceId;
|
|
this.instanceId = instanceId;
|
|
this.publisher = publisher;
|
|
this.publisher = publisher;
|
|
}
|
|
}
|
|
|
|
|
|
- private <T> T execute(ApiInfo info, Map<String, Object> context) {
|
|
+ private Object execute(PathMagicEntity info, Map<String, Object> context) {
|
|
-
|
|
+
|
|
-
|
|
+ MagicScriptContext scriptContext = new MagicScriptContext();
|
|
-
|
|
+ String fullGroupName = resourceService.getGroupName(info.getGroupId());
|
|
-
|
|
+ String fullGroupPath = resourceService.getGroupPath(info.getGroupId());
|
|
-
|
|
+ String scriptName = PathUtils.replaceSlash(String.format("/%s/%s(/%s/%s)", fullGroupName, info.getName(), fullGroupPath, info.getPath()));
|
|
-
|
|
+ scriptContext.setScriptName(scriptName);
|
|
-
|
|
+ scriptContext.putMapIntoContext(context);
|
|
-
|
|
+ return ScriptManager.executeScript(info.getScript(), scriptContext);
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- return null;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public <T> T execute(String method, String path, Map<String, Object> context) {
|
|
public <T> T execute(String method, String path, Map<String, Object> context) {
|
|
-
|
|
+ String mappingKey = Objects.toString(method, "GET").toUpperCase() + ":" + PathUtils.replaceSlash("/" + Objects.toString(path, ""));
|
|
-
|
|
+ ApiInfo info = requestMagicDynamicRegistry.getMapping(mappingKey);
|
|
-
|
|
+ if (info == null) {
|
|
-
|
|
+ throw new MagicAPIException(String.format("找不到对应接口 [%s:%s]", method, path));
|
|
-
|
|
+ }
|
|
- return null;
|
|
+ return (T) execute(info, context);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -92,15 +97,11 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public <T> T invoke(String path, Map<String, Object> context) {
|
|
public <T> T invoke(String path, Map<String, Object> context) {
|
|
-
|
|
+ FunctionInfo functionInfo = functionMagicDynamicRegistry.getMapping(path);
|
|
-
|
|
+ if (functionInfo == null) {
|
|
-
|
|
+ throw new MagicAPIException(String.format("找不到对应函数 [%s]", path));
|
|
-
|
|
+ }
|
|
-
|
|
+ return (T) execute(functionInfo, context);
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- return null;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|