feat: java执行引擎和js执行引擎调试完成
This commit is contained in:
@@ -76,6 +76,10 @@
|
||||
<groupId>cn.fateverse</groupId>
|
||||
<artifactId>common-excel</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package cn.fateverse.query.config;
|
||||
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/22 10:28
|
||||
*/
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public RestTemplate httpRestTemplate() {
|
||||
ClientHttpRequestFactory factory = httpRequestFactory();
|
||||
RestTemplate restTemplate = new RestTemplate(factory);
|
||||
// 可以添加消息转换
|
||||
//restTemplate.setMessageConverters(...);
|
||||
// 可以增加拦截器
|
||||
//restTemplate.setInterceptors(...);
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
public ClientHttpRequestFactory httpRequestFactory() {
|
||||
return new OkHttp3ClientHttpRequestFactory(okHttpConfigClient());
|
||||
}
|
||||
|
||||
public OkHttpClient okHttpConfigClient() {
|
||||
return new OkHttpClient().newBuilder()
|
||||
.connectionPool(pool())
|
||||
.connectTimeout(1, TimeUnit.SECONDS)
|
||||
.readTimeout(3, TimeUnit.SECONDS)
|
||||
.writeTimeout(3, TimeUnit.SECONDS)
|
||||
.hostnameVerifier((hostname, session) -> true)
|
||||
// 设置代理
|
||||
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
|
||||
// 拦截器
|
||||
// .addInterceptor()
|
||||
.build();
|
||||
}
|
||||
|
||||
public ConnectionPool pool() {
|
||||
return new ConnectionPool(2000, 300, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,29 @@ package cn.fateverse.query.constant;
|
||||
*/
|
||||
public class QueryConstant {
|
||||
|
||||
|
||||
/**
|
||||
* 自定义查询权限前缀
|
||||
*/
|
||||
public static final String PERMISSIONS_KEY = "custom:query:online:";
|
||||
|
||||
/**
|
||||
* 自定义接口Redis前缀key
|
||||
*/
|
||||
public static final String PORTAL_KEY = "custom:query:portal:";
|
||||
|
||||
/**
|
||||
* 接口开发状态
|
||||
*/
|
||||
public static final Integer PORTAL_DEV = 0;
|
||||
|
||||
/**
|
||||
* 接口发布状态
|
||||
*/
|
||||
public static final Integer PORTAL_PUBLISH = 1;
|
||||
|
||||
/**
|
||||
* 接口内部使用
|
||||
*/
|
||||
public static final Integer PORTAL_INWARD = 2;
|
||||
|
||||
}
|
||||
|
||||
@@ -66,13 +66,11 @@ public class DataAdapterController {
|
||||
}
|
||||
|
||||
@ApiOperation("获取数据源适配器详细信息")
|
||||
@Encrypt
|
||||
@GetMapping("/{adapterId}")
|
||||
@PreAuthorize("@ss.hasPermission('query:adapter:info')")
|
||||
public Result<DataAdapterVo> info(@PathVariable @EncryptField String adapterId) {
|
||||
public Result<DataAdapterVo> info(@PathVariable Long adapterId) {
|
||||
ObjectUtils.checkPk(adapterId);
|
||||
Long id = Long.valueOf(adapterId);
|
||||
DataAdapterVo dataAdapter = dataAdapterService.searchById(id);
|
||||
DataAdapterVo dataAdapter = dataAdapterService.searchById(adapterId);
|
||||
return Result.ok(dataAdapter);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.fateverse.common.core.result.page.TableDataInfo;
|
||||
import cn.fateverse.common.core.utils.ObjectUtils;
|
||||
import cn.fateverse.common.decrypt.annotation.Encrypt;
|
||||
import cn.fateverse.common.decrypt.annotation.EncryptField;
|
||||
import cn.fateverse.query.entity.dto.MockParam;
|
||||
import cn.fateverse.query.entity.dto.PortalDto;
|
||||
import cn.fateverse.query.entity.query.PortalQuery;
|
||||
import cn.fateverse.query.entity.vo.PortalIdWrapper;
|
||||
@@ -36,7 +37,7 @@ public class PortalController {
|
||||
}
|
||||
|
||||
@ApiOperation("获取接口管理表详细信息")
|
||||
@Encrypt
|
||||
@Encrypt(Encrypt.Position.IN)
|
||||
@GetMapping("/{portalId}")
|
||||
@PreAuthorize("@ss.hasPermission('query:portal:info')")
|
||||
public Result<PortalVo> info(@PathVariable @EncryptField String portalId) {
|
||||
@@ -47,6 +48,17 @@ public class PortalController {
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation("详情接口")
|
||||
@Encrypt(Encrypt.Position.IN)
|
||||
@GetMapping("/detail/{portalId}")
|
||||
public Result<PortalVo> detail(@PathVariable @EncryptField String portalId) {
|
||||
ObjectUtils.checkPk(portalId);
|
||||
Long value = Long.valueOf(portalId);
|
||||
PortalVo portal = portalService.searchDetailById(value);
|
||||
return Result.ok(portal);
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation("获取接口管理表列表")
|
||||
@GetMapping
|
||||
@Encrypt
|
||||
@@ -57,7 +69,7 @@ public class PortalController {
|
||||
}
|
||||
|
||||
@ApiOperation("新增接口")
|
||||
@Encrypt
|
||||
@Encrypt(Encrypt.Position.OUT)
|
||||
@PostMapping
|
||||
@PreAuthorize("@ss.hasPermission('query:portal:add')")
|
||||
public Result<PortalIdWrapper> add(@RequestBody @Validated PortalDto portalDto) {
|
||||
@@ -66,7 +78,7 @@ public class PortalController {
|
||||
}
|
||||
|
||||
@ApiOperation("修改接口")
|
||||
@Encrypt
|
||||
@Encrypt(Encrypt.Position.OUT)
|
||||
@PutMapping
|
||||
@PreAuthorize("@ss.hasPermission('query:portal:edit')")
|
||||
public Result<PortalIdWrapper> edit(@RequestBody @Validated PortalDto portalDto) {
|
||||
@@ -76,4 +88,48 @@ public class PortalController {
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation("模拟执行")
|
||||
@PostMapping("/mock/execute")
|
||||
@PreAuthorize("@ss.hasPermission('query:portal:execute')")
|
||||
public Result<Object> mockExecute(@RequestBody @Validated MockParam mockParam) {
|
||||
if (ObjectUtils.isEmpty(mockParam.getCode())) {
|
||||
return Result.error("代码不能为空");
|
||||
}
|
||||
Object result = portalService.mockExecute(mockParam);
|
||||
return Result.ok(result);
|
||||
}
|
||||
|
||||
@ApiOperation("获取接口数据")
|
||||
@PostMapping("/mock/data")
|
||||
@PreAuthorize("@ss.hasPermission('query:portal:data')")
|
||||
public Result<Object> mockData(@RequestBody @Validated MockParam mockParam) {
|
||||
Object result = portalService.mockData(mockParam);
|
||||
return Result.ok(result);
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation("撤销发布")
|
||||
@Encrypt
|
||||
@PutMapping("/cancel/{portalId}")
|
||||
@PreAuthorize("@ss.hasPermission('query:portal:edit')")
|
||||
public Result<PortalIdWrapper> cancel(@PathVariable @EncryptField String portalId) {
|
||||
ObjectUtils.checkPk(portalId);
|
||||
Long value = Long.valueOf(portalId);
|
||||
portalService.cancel(value);
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation("删除接口")
|
||||
@Encrypt
|
||||
@DeleteMapping("/{portalId}")
|
||||
@PreAuthorize("@ss.hasPermission('query:portal:remove')")
|
||||
public Result<Void> remove(@PathVariable @EncryptField String portalId) {
|
||||
ObjectUtils.checkPk(portalId);
|
||||
Long value = Long.valueOf(portalId);
|
||||
portalService.delete(value);
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -78,6 +78,9 @@ public class DataAdapter extends BaseEntity {
|
||||
"}\n";
|
||||
} else if (DataAdapterType.JAVA_SCRIPT.equals(type)) {
|
||||
//JavaScript代码初始化
|
||||
this.code = "function execute(data) {\n" +
|
||||
"\n" +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,9 +58,16 @@ public class Portal extends BaseEntity {
|
||||
*/
|
||||
private String requestMethod;
|
||||
|
||||
|
||||
/**
|
||||
* 是否创建数据适配器
|
||||
*/
|
||||
private Boolean createDataAdapter;
|
||||
|
||||
/**
|
||||
* 是否分页
|
||||
*/
|
||||
private Boolean page;
|
||||
|
||||
/**
|
||||
* 系统暴露地址
|
||||
*/
|
||||
|
||||
@@ -43,12 +43,23 @@ public class PortalMapping {
|
||||
* 映射值 自定义查询映射值为查询条件的id 第三方接口则为接口查询的key
|
||||
*/
|
||||
private String mappingValue;
|
||||
/**
|
||||
* 输入类型
|
||||
*/
|
||||
private String inputType;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
|
||||
public Boolean check() {
|
||||
if (ObjectUtils.isEmpty(mappingKey)
|
||||
|| ObjectUtils.isEmpty(mappingValue)
|
||||
|| ObjectUtils.isEmpty(mappingType)){
|
||||
|| ObjectUtils.isEmpty(mappingType)
|
||||
// || ObjectUtils.isEmpty(inputType)
|
||||
) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
|
||||
@@ -25,6 +25,8 @@ public class PortalBo implements Serializable {
|
||||
private Long adapterId;
|
||||
private PortalEnum type;
|
||||
private String requestMethod;
|
||||
private Boolean createDataAdapter;
|
||||
private Boolean page;
|
||||
private String path;
|
||||
private String url;
|
||||
private Integer state;
|
||||
@@ -37,6 +39,8 @@ public class PortalBo implements Serializable {
|
||||
.adapterId(portal.getAdapterId())
|
||||
.type(portal.getType())
|
||||
.requestMethod(portal.getRequestMethod())
|
||||
.createDataAdapter(portal.getCreateDataAdapter())
|
||||
.page(portal.getPage())
|
||||
.path(portal.getPath())
|
||||
.url(portal.getUrl())
|
||||
.state(portal.getState())
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.fateverse.query.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/21 19:07
|
||||
*/
|
||||
@Data
|
||||
public class MockParam {
|
||||
|
||||
@NotNull(message = "id不能为空")
|
||||
private Long portalId;
|
||||
|
||||
private String code;
|
||||
|
||||
private Integer pageSize;
|
||||
|
||||
private Integer pageNum;
|
||||
|
||||
private List<Param> params;
|
||||
|
||||
@Data
|
||||
public static class Param {
|
||||
|
||||
private String key;
|
||||
|
||||
private Object value;
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,11 @@ public class PortalDto {
|
||||
@NotNull(message = "是否创建数据适配器不能为空!")
|
||||
private Boolean createDataAdapter;
|
||||
|
||||
/**
|
||||
* 是否分页
|
||||
*/
|
||||
private Boolean page;
|
||||
|
||||
/**
|
||||
* 系统暴露地址
|
||||
*/
|
||||
@@ -113,6 +118,7 @@ public class PortalDto {
|
||||
.anonymity(anonymity)
|
||||
.createDataAdapter(createDataAdapter)
|
||||
.requestMethod(requestMethod)
|
||||
.page(page)
|
||||
.type(type)
|
||||
.path(path)
|
||||
.url(url)
|
||||
@@ -120,7 +126,7 @@ public class PortalDto {
|
||||
.build();
|
||||
try {
|
||||
RequestMethod.valueOf(requestMethod);
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("请求类型不支持!");
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(portalId)) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package cn.fateverse.query.entity.query;
|
||||
package cn.fateverse.query.entity.query;
|
||||
|
||||
import cn.fateverse.query.enums.PortalEnum;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
@@ -42,4 +42,6 @@ public class PortalQuery {
|
||||
*/
|
||||
@ApiModelProperty("系统暴露地址")
|
||||
private String path;
|
||||
|
||||
private Integer state;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package cn.fateverse.query.entity.vo;
|
||||
|
||||
import cn.fateverse.query.entity.Portal;
|
||||
import cn.fateverse.query.entity.PortalMapping;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
@@ -20,12 +21,32 @@ import java.util.List;
|
||||
@ApiModel("接口详细管理表Vo")
|
||||
public class PortalVo extends SimplePortalVo {
|
||||
|
||||
/**
|
||||
* 查询类型
|
||||
*/
|
||||
private Integer queryType;
|
||||
|
||||
/**
|
||||
* 是否创建数据适配器
|
||||
*/
|
||||
private Boolean createDataAdapter;
|
||||
|
||||
/**
|
||||
* 是否分页
|
||||
*/
|
||||
private Boolean page;
|
||||
|
||||
/**
|
||||
* 条件映射
|
||||
*/
|
||||
private List<PortalMapping> mappings;
|
||||
|
||||
/**
|
||||
* 数据适配器信息
|
||||
*/
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private DataAdapterVo dataAdapter;
|
||||
|
||||
public static PortalVo toPortalVo(Portal portal) {
|
||||
return toPortalVo(portal, null);
|
||||
}
|
||||
|
||||
@@ -37,16 +37,14 @@ public class SimplePortalVo {
|
||||
* 自定义查询id
|
||||
*/
|
||||
@ApiModelProperty("自定义查询id")
|
||||
@EncryptField
|
||||
private String queryId;
|
||||
private Long queryId;
|
||||
|
||||
|
||||
/**
|
||||
* 数据适配器id
|
||||
*/
|
||||
@ApiModelProperty("数据适配器id")
|
||||
@EncryptField
|
||||
private String adapterId;
|
||||
private Long adapterId;
|
||||
|
||||
/**
|
||||
* 接口名称
|
||||
@@ -127,6 +125,8 @@ public class SimplePortalVo {
|
||||
.portalId(String.valueOf(portal.getPortalId()))
|
||||
.portalName(portal.getPortalName())
|
||||
.anonymity(portal.getAnonymity())
|
||||
.queryId(portal.getQueryId())
|
||||
.adapterId(portal.getAdapterId())
|
||||
.type(portal.getType())
|
||||
.path(portal.getPath())
|
||||
.state(portal.getState())
|
||||
@@ -136,12 +136,6 @@ public class SimplePortalVo {
|
||||
.build();
|
||||
portalVo.setCreateTime(portal.getCreateTime());
|
||||
portalVo.setUpdateTime(portal.getUpdateTime());
|
||||
if (!ObjectUtils.isEmpty(portal.getQueryId())) {
|
||||
portalVo.setQueryId(String.valueOf(portal.getQueryId()));
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(portal.getAdapterId())) {
|
||||
portalVo.setAdapterId(String.valueOf(portal.getAdapterId()));
|
||||
}
|
||||
return portalVo;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package cn.fateverse.query.handler.adapter;
|
||||
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.handler.reader.EngineExecuteHandlerReader;
|
||||
import cn.fateverse.query.mapper.DataAdapterMapper;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/19 22:44
|
||||
*/
|
||||
public abstract class AbstractDataAdapterHandler implements DataAdapterHandler {
|
||||
|
||||
protected final DataAdapterMapper dataAdapterMapper;
|
||||
|
||||
|
||||
protected final EngineExecuteHandlerReader handlerReader;
|
||||
|
||||
|
||||
protected AbstractDataAdapterHandler(DataAdapterMapper dataAdapterMapper, EngineExecuteHandlerReader handlerReader) {
|
||||
this.dataAdapterMapper = dataAdapterMapper;
|
||||
this.handlerReader = handlerReader;
|
||||
}
|
||||
|
||||
protected Object execute(Long adapterId, Object data, boolean development) {
|
||||
if (ObjectUtils.isEmpty(adapterId)) {
|
||||
return data;
|
||||
}
|
||||
//获取当当前接口对应的数据适配器
|
||||
DataAdapter dataAdapter = dataAdapterMapper.selectById(adapterId);
|
||||
if (ObjectUtils.isEmpty(dataAdapter)) {
|
||||
throw new RuntimeException("dataAdapter is null");
|
||||
}
|
||||
handlerReader.preconditioning(dataAdapter);
|
||||
EngineResult execute = handlerReader.execute(dataAdapter, data, development);
|
||||
if (ObjectUtils.isEmpty(execute)) {
|
||||
throw new RuntimeException("执行结果错误");
|
||||
}
|
||||
return execute.getResult();
|
||||
}
|
||||
|
||||
|
||||
protected EngineResult mockExecute(Long adapterId, String code, Object data, boolean development) {
|
||||
//获取当当前接口对应的数据适配器
|
||||
DataAdapter dataAdapter = dataAdapterMapper.selectById(adapterId);
|
||||
if (ObjectUtils.isEmpty(dataAdapter)) {
|
||||
throw new RuntimeException("dataAdapter is null");
|
||||
}
|
||||
dataAdapter.setCode(code);
|
||||
handlerReader.preconditioning(dataAdapter);
|
||||
EngineResult execute = handlerReader.execute(dataAdapter, data, development);
|
||||
if (ObjectUtils.isEmpty(execute)) {
|
||||
throw new RuntimeException("执行结果错误");
|
||||
}
|
||||
dataAdapterMapper.updateCode(dataAdapter);
|
||||
return execute;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean remove(Long adapterId) {
|
||||
if (ObjectUtils.isEmpty(adapterId)) {
|
||||
throw new RuntimeException("adapterId is null");
|
||||
}
|
||||
DataAdapter dataAdapter = dataAdapterMapper.selectById(adapterId);
|
||||
if (ObjectUtils.isEmpty(dataAdapter)) {
|
||||
throw new RuntimeException("dataAdapter is null");
|
||||
}
|
||||
return handlerReader.remove(dataAdapter);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package cn.fateverse.query.handler.adapter;
|
||||
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.entity.dto.MockParam;
|
||||
import cn.fateverse.query.entity.bo.PortalBo;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2023-10-31 20:52
|
||||
@@ -12,19 +14,27 @@ public interface DataAdapterHandler {
|
||||
/**
|
||||
* 模拟执行
|
||||
*
|
||||
* @param dataAdapter 数据适配器信息
|
||||
* @param portal
|
||||
* @param portal 接口对象
|
||||
* @param mockParam 请求头
|
||||
* @return 执行结果
|
||||
*/
|
||||
Object mockExecute(DataAdapter dataAdapter, PortalBo portal, Object param);
|
||||
Object mockExecute(PortalBo portal, MockParam mockParam);
|
||||
|
||||
/**
|
||||
* 真实执行
|
||||
*
|
||||
* @param dataAdapter 数据适配器信息
|
||||
* @param portal
|
||||
* @param portal 接口对象
|
||||
* @param request
|
||||
* @return 执行结果
|
||||
*/
|
||||
Object execute(DataAdapter dataAdapter, PortalBo portal, Object param);
|
||||
Object execute(PortalBo portal, HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 删除数据适配器
|
||||
*
|
||||
* @param adapterId 数据适配器id
|
||||
* @return 删除结果
|
||||
*/
|
||||
boolean remove(Long adapterId);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package cn.fateverse.query.handler.adapter.impl;
|
||||
|
||||
import cn.fateverse.common.core.exception.CustomException;
|
||||
import cn.fateverse.query.entity.dto.MockParam;
|
||||
import cn.fateverse.query.entity.bo.PortalBo;
|
||||
import cn.fateverse.query.enums.PortalEnum;
|
||||
import cn.fateverse.query.handler.adapter.AbstractDataAdapterHandler;
|
||||
import cn.fateverse.query.handler.reader.EngineExecuteHandlerReader;
|
||||
import cn.fateverse.query.mapper.DataAdapterMapper;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/19 22:12
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ExternalDataAdapterHandler extends AbstractDataAdapterHandler {
|
||||
|
||||
/**
|
||||
* 请求对象
|
||||
*/
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public ExternalDataAdapterHandler(DataAdapterMapper dataAdapterMapper,
|
||||
EngineExecuteHandlerReader handlerReader,
|
||||
RestTemplate restTemplate) {
|
||||
super(dataAdapterMapper, handlerReader);
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object mockExecute(PortalBo portal, MockParam mockParam) {
|
||||
if (!PortalEnum.EXTERNAL.equals(portal.getType())) {
|
||||
return null;
|
||||
}
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
||||
headers.setContentType(type);
|
||||
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
JSONObject response = null;
|
||||
switch (portal.getRequestMethod()) {
|
||||
case "GET":
|
||||
response = restTemplate.getForObject(portal.getUrl(), JSONObject.class, map);
|
||||
break;
|
||||
case "POST":
|
||||
response = restTemplate.postForObject(portal.getUrl(), map, JSONObject.class);
|
||||
break;
|
||||
default:
|
||||
throw new CustomException("请求方式错误");
|
||||
}
|
||||
if (portal.getCreateDataAdapter()) {
|
||||
return super.execute(portal.getAdapterId(), response, true);
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(PortalBo portal, HttpServletRequest request) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -2,20 +2,23 @@ package cn.fateverse.query.handler.adapter.impl;
|
||||
|
||||
import cn.fateverse.common.core.exception.CustomException;
|
||||
import cn.fateverse.common.core.result.page.TableDataInfo;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.entity.dto.MockParam;
|
||||
import cn.fateverse.query.entity.PortalMapping;
|
||||
import cn.fateverse.query.entity.UniQuery;
|
||||
import cn.fateverse.query.entity.bo.PortalBo;
|
||||
import cn.fateverse.query.entity.dto.SearchInfo;
|
||||
import cn.fateverse.query.entity.dto.UniConDto;
|
||||
import cn.fateverse.query.enums.PortalEnum;
|
||||
import cn.fateverse.query.handler.adapter.DataAdapterHandler;
|
||||
import cn.fateverse.query.handler.adapter.AbstractDataAdapterHandler;
|
||||
import cn.fateverse.query.handler.reader.EngineExecuteHandlerReader;
|
||||
import cn.fateverse.query.mapper.DataAdapterMapper;
|
||||
import cn.fateverse.query.mapper.UniQueryMapper;
|
||||
import cn.fateverse.query.service.DynamicDataSearchService;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -25,55 +28,86 @@ import java.util.Map;
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class LocalDataAdapterHandler implements DataAdapterHandler {
|
||||
public class LocalDataAdapterHandler extends AbstractDataAdapterHandler {
|
||||
|
||||
|
||||
private final UniQueryMapper uniQueryMapper;
|
||||
|
||||
private final EngineExecuteHandlerReader handlerReader;
|
||||
|
||||
private final DynamicDataSearchService dynamicDataSearchService;
|
||||
|
||||
public LocalDataAdapterHandler(UniQueryMapper uniQueryMapper,
|
||||
DataAdapterMapper dataAdapterMapper,
|
||||
EngineExecuteHandlerReader handlerReader,
|
||||
DynamicDataSearchService dynamicDataSearchService) {
|
||||
super(dataAdapterMapper, handlerReader);
|
||||
this.uniQueryMapper = uniQueryMapper;
|
||||
this.handlerReader = handlerReader;
|
||||
this.dynamicDataSearchService = dynamicDataSearchService;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object mockExecute(DataAdapter dataAdapter, PortalBo portal, Object param) {
|
||||
public Object mockExecute(PortalBo portal, MockParam mockParam) {
|
||||
if (portal.getType() != PortalEnum.LOCAL) {
|
||||
return null;
|
||||
}
|
||||
handlerReader.preconditioning(dataAdapter);
|
||||
if (null == param) {
|
||||
throw new CustomException("参数对象不能为空");
|
||||
//自定义查询编辑查询对象
|
||||
List<UniConDto> uniConList = new ArrayList<>();
|
||||
if (!ObjectUtils.isEmpty(mockParam.getParams())) {
|
||||
for (MockParam.Param param : mockParam.getParams()) {
|
||||
if (!ObjectUtils.isEmpty(param.getKey()) && !ObjectUtils.isEmpty(param.getValue())) {
|
||||
UniConDto uniCon = new UniConDto();
|
||||
uniCon.setQuery(param.getValue());
|
||||
uniCon.setUcId(Long.parseLong(param.getKey()));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!(param instanceof SearchInfo)) {
|
||||
throw new CustomException("数据类型不匹配");
|
||||
}
|
||||
SearchInfo info = (SearchInfo) param;
|
||||
UniQuery query = uniQueryMapper.selectSampleById(portal.getQueryId());
|
||||
if (null == query) {
|
||||
throw new CustomException("数据源为空!");
|
||||
}
|
||||
//根据设置的参数动态调整当前是否需要分页操作
|
||||
TableDataInfo<Map<String, Object>> tableDataInfo = dynamicDataSearchService.searchData(info.getList(), query, null, Boolean.TRUE);
|
||||
return handlerReader.execute(dataAdapter, tableDataInfo.getRows(), true);
|
||||
TableDataInfo<Map<String, Object>> tableDataInfo = dynamicDataSearchService.searchData(uniConList, query, null, Boolean.TRUE);
|
||||
if (portal.getCreateDataAdapter()) {
|
||||
return super.mockExecute(portal.getAdapterId(), mockParam.getCode(), tableDataInfo.getRows(), true);
|
||||
} else {
|
||||
return tableDataInfo.getRows();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object execute(DataAdapter dataAdapter, PortalBo portal, Object param) {
|
||||
public Object execute(PortalBo portal, HttpServletRequest request) {
|
||||
if (portal.getType() != PortalEnum.LOCAL) {
|
||||
return null;
|
||||
}
|
||||
handlerReader.preconditioning(dataAdapter);
|
||||
List<Map<String, Object>> data = JSON.parseObject(dataAdapter.getMockData(), new TypeReference<List<Map<String, Object>>>() {
|
||||
});
|
||||
return handlerReader.execute(dataAdapter, data, false);
|
||||
//自定义查询编辑查询对象
|
||||
List<UniConDto> uniConList = new ArrayList<>();
|
||||
//根据映射关系从request中获取请求参数
|
||||
for (PortalMapping portalMapping : portal.getMappings()) {
|
||||
UniConDto uniCon = new UniConDto();
|
||||
String mappingValue = portalMapping.getMappingValue();
|
||||
String mappingKey = portalMapping.getMappingKey();
|
||||
uniCon.setUcId(Long.parseLong(mappingValue));
|
||||
if (portalMapping.getMappingType() == 0) {
|
||||
uniCon.setQuery(request.getParameter(mappingKey));
|
||||
} else if (portalMapping.getMappingType() == 1) {
|
||||
uniCon.setQuery(request.getHeaders(mappingKey));
|
||||
} else {
|
||||
uniCon.setQuery(request.getParameter(mappingKey));
|
||||
}
|
||||
uniConList.add(uniCon);
|
||||
}
|
||||
UniQuery query = uniQueryMapper.selectSampleById(portal.getQueryId());
|
||||
if (null == query) {
|
||||
throw new CustomException("数据源为空!");
|
||||
}
|
||||
//根据设置的参数动态调整当前是否需要分页操作
|
||||
TableDataInfo<Map<String, Object>> tableDataInfo = dynamicDataSearchService.searchData(uniConList, query, null, Boolean.TRUE);
|
||||
if (portal.getCreateDataAdapter()) {
|
||||
return super.execute(portal.getAdapterId(), tableDataInfo.getRows(), false);
|
||||
} else {
|
||||
return tableDataInfo.getRows();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.fateverse.query.handler.engine;
|
||||
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
|
||||
public interface EngineExecuteHandler {
|
||||
@@ -13,7 +14,7 @@ public interface EngineExecuteHandler {
|
||||
* @param development
|
||||
* @return JSONObject对象
|
||||
*/
|
||||
Object execute(DataAdapter dataAdapter, Object data, boolean development);
|
||||
EngineResult execute(DataAdapter dataAdapter, Object data, boolean development);
|
||||
|
||||
/**
|
||||
* 预处理数据适配器
|
||||
@@ -25,6 +26,7 @@ public interface EngineExecuteHandler {
|
||||
|
||||
/**
|
||||
* 删除数据适配器
|
||||
*
|
||||
* @param dataAdapter 数据适配器
|
||||
* @return 删除结果
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.fateverse.query.handler.engine.impl;
|
||||
|
||||
import cn.fateverse.common.code.engine.JavaCodeEngine;
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
import cn.fateverse.common.core.exception.CustomException;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.enums.DataAdapterType;
|
||||
@@ -31,8 +32,8 @@ public class JavaEngineExecuteHandler implements EngineExecuteHandler {
|
||||
|
||||
|
||||
@Override
|
||||
public Object execute(DataAdapter dataAdapter, Object data, boolean development) {
|
||||
if (!DataAdapterType.JAVA.equals(dataAdapter.getType())){
|
||||
public EngineResult execute(DataAdapter dataAdapter, Object data, boolean development) {
|
||||
if (!DataAdapterType.JAVA.equals(dataAdapter.getType())) {
|
||||
return null;
|
||||
}
|
||||
return javaCodeEngine.execute(dataAdapter.getExecuteCode(), getClassName(dataAdapter),
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package cn.fateverse.query.handler.engine.impl;
|
||||
|
||||
import cn.fateverse.common.code.engine.JavaScriptEngine;
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
import cn.fateverse.common.core.exception.CustomException;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.enums.DataAdapterType;
|
||||
import cn.fateverse.query.handler.engine.EngineExecuteHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* JavaScript 代码执行器
|
||||
*
|
||||
@@ -18,19 +23,31 @@ import org.springframework.stereotype.Component;
|
||||
public class JavaScriptEngineExecuteHandler implements EngineExecuteHandler {
|
||||
|
||||
@Override
|
||||
public Object execute(DataAdapter dataAdapter, Object data, boolean development) {
|
||||
public EngineResult execute(DataAdapter dataAdapter, Object data, boolean development) {
|
||||
if (!DataAdapterType.JAVA_SCRIPT.equals(dataAdapter.getType())) {
|
||||
return null;
|
||||
}
|
||||
return JavaScriptEngine.executeScript(dataAdapter.getExecuteCode(), "execute", data);
|
||||
return JavaScriptEngine.execute(dataAdapter.getExecuteCode(), "execute" + dataAdapter.getAdapterId(), development, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean preconditioning(DataAdapter dataAdapter) {
|
||||
if (DataAdapterType.JAVA_SCRIPT.equals(dataAdapter.getType())) {
|
||||
return Boolean.TRUE;
|
||||
if (!DataAdapterType.JAVA_SCRIPT.equals(dataAdapter.getType())) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
String code = dataAdapter.getCode();
|
||||
// 正则表达式匹配类定义
|
||||
String regex = "function .*? ";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(code);
|
||||
if (matcher.find()) {
|
||||
// 执行替换操作
|
||||
String replacedCode = code.replaceFirst(regex, "function execute" + dataAdapter.getAdapterId() + "(data) ");
|
||||
dataAdapter.setExecuteCode(replacedCode);
|
||||
} else {
|
||||
throw new CustomException("请勿修改类定义");
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,12 +2,13 @@ package cn.fateverse.query.handler.reader;
|
||||
|
||||
|
||||
import cn.fateverse.common.core.exception.CustomException;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.entity.dto.MockParam;
|
||||
import cn.fateverse.query.entity.bo.PortalBo;
|
||||
import cn.fateverse.query.handler.adapter.DataAdapterHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -28,9 +29,9 @@ public class DataAdapterHandlerReader {
|
||||
}
|
||||
|
||||
|
||||
public Object mockExecute(DataAdapter dataAdapter, PortalBo portal, Object params) {
|
||||
public Object mockExecute(PortalBo portal, MockParam mockParam) {
|
||||
for (DataAdapterHandler dataAdapterHandler : handlerList) {
|
||||
Object result = dataAdapterHandler.mockExecute(dataAdapter, portal, params);
|
||||
Object result = dataAdapterHandler.mockExecute(portal, mockParam);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
@@ -39,9 +40,9 @@ public class DataAdapterHandlerReader {
|
||||
}
|
||||
|
||||
|
||||
public Object execute(DataAdapter dataAdapter, PortalBo portal, Object params) {
|
||||
public Object execute(PortalBo portal, HttpServletRequest request) {
|
||||
for (DataAdapterHandler dataAdapterHandler : handlerList) {
|
||||
Object result = dataAdapterHandler.execute(dataAdapter, portal, params);
|
||||
Object result = dataAdapterHandler.execute(portal, request);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
@@ -49,5 +50,15 @@ public class DataAdapterHandlerReader {
|
||||
throw new CustomException("当前数据源类型不支持!");
|
||||
}
|
||||
|
||||
public Boolean remove(Long adaptorId) {
|
||||
for (DataAdapterHandler dataAdapterHandler : handlerList) {
|
||||
boolean result = dataAdapterHandler.remove(adaptorId);
|
||||
if (result) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.fateverse.query.handler.reader;
|
||||
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.handler.engine.EngineExecuteHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -47,11 +48,11 @@ public class EngineExecuteHandlerReader {
|
||||
* @param data 数据列表
|
||||
* @return 执行结果
|
||||
*/
|
||||
public Object execute(DataAdapter dataAdapter, Object data, boolean development) {
|
||||
public EngineResult execute(DataAdapter dataAdapter, Object data, boolean development) {
|
||||
// 遍历引擎执行处理器列表
|
||||
for (EngineExecuteHandler engineExecuteHandler : handlerList) {
|
||||
// 执行数据适配器的处理方法
|
||||
Object result = engineExecuteHandler.execute(dataAdapter, data, development);
|
||||
EngineResult result = engineExecuteHandler.execute(dataAdapter, data, development);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -48,6 +48,14 @@ public interface DataAdapterMapper {
|
||||
*/
|
||||
int update(DataAdapter dataAdapter);
|
||||
|
||||
/**
|
||||
* 更新代码
|
||||
*
|
||||
* @param dataAdapter 数据适配器
|
||||
* @return 结果
|
||||
*/
|
||||
int updateCode(DataAdapter dataAdapter);
|
||||
|
||||
/**
|
||||
* 删除数据源适配器
|
||||
*
|
||||
|
||||
@@ -48,4 +48,28 @@ public interface PortalMapper {
|
||||
* @return 更新数量
|
||||
*/
|
||||
int insert(Portal portal);
|
||||
|
||||
/**
|
||||
* 修改接口
|
||||
*
|
||||
* @param portal 接口信息
|
||||
* @return 更新数量
|
||||
*/
|
||||
int update(Portal portal);
|
||||
|
||||
/**
|
||||
* 更新状态
|
||||
*
|
||||
* @param portalId 接口id
|
||||
* @param state 状态
|
||||
*/
|
||||
void updateState(@Param("portalId") Long portalId, @Param("state") Integer state);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param portalId id
|
||||
* @return 删除行数
|
||||
*/
|
||||
int deleteById(Long portalId);
|
||||
}
|
||||
|
||||
@@ -70,34 +70,13 @@ public class PortalDispatchServlet {
|
||||
portalBo = PortalBo.toPortalBo(portal, portalMappings);
|
||||
redisTemplate.opsForValue().set(QueryConstant.PORTAL_KEY + portalBo.getPath() + ":" + portalBo.getRequestMethod(), portalBo);
|
||||
}
|
||||
//自定义查询编辑查询对象
|
||||
SearchInfo searchInfo = new SearchInfo();
|
||||
List<UniConDto> uniConList = new ArrayList<>();
|
||||
//根据映射关系从request中获取请求参数
|
||||
for (PortalMapping portalMapping : portalBo.getMappings()) {
|
||||
UniConDto uniCon = new UniConDto();
|
||||
String mappingValue = portalMapping.getMappingValue();
|
||||
String mappingKey = portalMapping.getMappingKey();
|
||||
uniCon.setUcId(Long.parseLong(mappingValue));
|
||||
if (portalMapping.getMappingType() == 0) {
|
||||
uniCon.setQuery(request.getParameter(mappingKey));
|
||||
} else if (portalMapping.getMappingType() == 1) {
|
||||
uniCon.setQuery(request.getHeaders(mappingKey));
|
||||
} else {
|
||||
uniCon.setQuery(request.getParameter(mappingKey));
|
||||
}
|
||||
uniConList.add(uniCon);
|
||||
}
|
||||
searchInfo.setList(uniConList);
|
||||
//获取当当前接口对应的数据适配器
|
||||
DataAdapter dataAdapter = dataAdapterMapper.selectById(portalBo.getAdapterId());
|
||||
//进行数据适配器的执行逻辑
|
||||
Object result = null;
|
||||
if (portalBo.getState() == 1 || portalBo.getState() == 2) {
|
||||
result = dataAdapterHandler.execute(dataAdapter, portalBo, searchInfo);
|
||||
} else {
|
||||
result = dataAdapterHandler.mockExecute(dataAdapter, portalBo, searchInfo);
|
||||
}
|
||||
// if (portalBo.getState() == 1 || portalBo.getState() == 2) {
|
||||
result = dataAdapterHandler.execute(portalBo, request);
|
||||
// } else {
|
||||
// result = dataAdapterHandler.mockExecute(portalBo, request);
|
||||
// }
|
||||
//将返回结果放入response
|
||||
ResponseRender.renderString(response, Result.ok(result));
|
||||
}
|
||||
|
||||
@@ -140,6 +140,7 @@ public class DispatchSyncService {
|
||||
}
|
||||
}
|
||||
} catch (NacosException e) {
|
||||
log.error("NacosException: {}", e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,9 @@ public class DynamicDataSearchService {
|
||||
} else {
|
||||
uniConList = uniConMapper.selectByQueryIdAndUcIdList(query.getId(), ids);
|
||||
}
|
||||
uniConList = uniConList.stream().filter(uniCon -> null != queryMap.get(uniCon.getUcId())).peek(uniCon -> uniCon.setUcMock(queryMap.get(uniCon.getUcId()))).collect(Collectors.toList());
|
||||
uniConList = uniConList.stream().filter(uniCon -> null != queryMap.get(uniCon.getUcId()))
|
||||
.peek(uniCon -> uniCon.setUcMock(queryMap.get(uniCon.getUcId())))
|
||||
.collect(Collectors.toList());
|
||||
if (query.getType() == 1) {
|
||||
SqlSelect select = new SqlSelect();
|
||||
select.setSelectSql(query.getUqSql());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.fateverse.query.service;
|
||||
|
||||
import cn.fateverse.common.core.result.page.TableDataInfo;
|
||||
import cn.fateverse.query.entity.dto.MockParam;
|
||||
import cn.fateverse.query.entity.dto.PortalDto;
|
||||
import cn.fateverse.query.entity.query.PortalQuery;
|
||||
import cn.fateverse.query.entity.vo.PortalIdWrapper;
|
||||
@@ -21,6 +22,14 @@ public interface PortalService {
|
||||
*/
|
||||
PortalVo searchById(Long portalId);
|
||||
|
||||
/**
|
||||
* 根据id查询详情
|
||||
*
|
||||
* @param portalId id
|
||||
* @return 结果
|
||||
*/
|
||||
PortalVo searchDetailById(Long portalId);
|
||||
|
||||
/**
|
||||
* 查询接口列表
|
||||
*
|
||||
@@ -29,6 +38,23 @@ public interface PortalService {
|
||||
*/
|
||||
TableDataInfo<SimplePortalVo> searchList(PortalQuery query);
|
||||
|
||||
/**
|
||||
* 模拟执行
|
||||
*
|
||||
* @param mockParam 执行数据
|
||||
* @return 执行结果
|
||||
*/
|
||||
Object mockExecute(MockParam mockParam);
|
||||
|
||||
|
||||
/**
|
||||
* 获取到模拟数据
|
||||
*
|
||||
* @param mockParam
|
||||
* @return
|
||||
*/
|
||||
Object mockData(MockParam mockParam);
|
||||
|
||||
/**
|
||||
* 保存接口信息
|
||||
*
|
||||
@@ -44,4 +70,19 @@ public interface PortalService {
|
||||
* @return 修改完成后接口id和数据适配器id
|
||||
*/
|
||||
PortalIdWrapper edit(PortalDto portalDto);
|
||||
|
||||
/**
|
||||
* 取消发布
|
||||
*
|
||||
* @param portalId 接口id
|
||||
*/
|
||||
void cancel(Long portalId);
|
||||
|
||||
/**
|
||||
* 删除接口信息
|
||||
*
|
||||
* @param portalId 接口id
|
||||
*/
|
||||
void delete(Long portalId);
|
||||
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ package cn.fateverse.query.service.impl;
|
||||
import cn.fateverse.common.core.exception.CustomException;
|
||||
import cn.fateverse.common.core.result.page.TableDataInfo;
|
||||
import cn.fateverse.common.mybatis.utils.PageUtils;
|
||||
import cn.fateverse.query.entity.*;
|
||||
import cn.fateverse.query.entity.bo.PortalBo;
|
||||
import cn.fateverse.query.entity.dto.MockParam;
|
||||
import cn.fateverse.query.entity.vo.DataAdapterVo;
|
||||
import cn.fateverse.query.entity.vo.PortalIdWrapper;
|
||||
import cn.fateverse.query.handler.reader.DataAdapterHandlerReader;
|
||||
import cn.fateverse.query.portal.service.HandlerMethodService;
|
||||
import cn.fateverse.query.constant.QueryConstant;
|
||||
import cn.fateverse.query.entity.DataAdapter;
|
||||
import cn.fateverse.query.entity.Portal;
|
||||
import cn.fateverse.query.entity.PortalMapping;
|
||||
import cn.fateverse.query.entity.UniQuery;
|
||||
import cn.fateverse.query.entity.dto.DataAdapterDto;
|
||||
import cn.fateverse.query.entity.dto.PortalDto;
|
||||
import cn.fateverse.query.entity.query.PortalQuery;
|
||||
@@ -32,7 +32,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -57,19 +56,26 @@ public class PortalServiceImpl implements PortalService {
|
||||
|
||||
private final PortalMappingMapper portalMappingMapper;
|
||||
|
||||
|
||||
private final DataAdapterHandlerReader handlerReader;
|
||||
|
||||
public PortalServiceImpl(PortalMapper portalMapper,
|
||||
UniQueryMapper queryMapper,
|
||||
DataAdapterMapper adapterMapper,
|
||||
PortalMappingMapper portalMappingMapper,
|
||||
ThreadPoolTaskExecutor taskExecuteExecutor,
|
||||
HandlerMethodService methodService) {
|
||||
HandlerMethodService methodService,
|
||||
DataAdapterHandlerReader handlerReader) {
|
||||
this.portalMapper = portalMapper;
|
||||
this.queryMapper = queryMapper;
|
||||
this.adapterMapper = adapterMapper;
|
||||
this.portalMappingMapper = portalMappingMapper;
|
||||
this.methodService = methodService;
|
||||
this.handlerReader = handlerReader;
|
||||
taskExecuteExecutor.execute(() -> {
|
||||
List<Portal> portalList = portalMapper.selectList(new PortalQuery());
|
||||
PortalQuery query = new PortalQuery();
|
||||
// query.setState(QueryConstant.PORTAL_PUBLISH);
|
||||
List<Portal> portalList = portalMapper.selectList(query);
|
||||
if (ObjectUtils.isEmpty(portalList)) {
|
||||
log.info("portal is empty!");
|
||||
return;
|
||||
@@ -116,6 +122,20 @@ public class PortalServiceImpl implements PortalService {
|
||||
return portalVo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PortalVo searchDetailById(Long portalId) {
|
||||
PortalVo portalVo = searchById(portalId);
|
||||
if (!ObjectUtils.isEmpty(portalVo.getAdapterId()) && portalVo.getCreateDataAdapter()) {
|
||||
DataAdapter dataAdapter = adapterMapper.selectById(portalVo.getAdapterId());
|
||||
if (ObjectUtils.isEmpty(dataAdapter)) {
|
||||
return portalVo;
|
||||
}
|
||||
DataAdapterVo dataAdapterVo = DataAdapterVo.toDataAdapterVo(dataAdapter);
|
||||
portalVo.setDataAdapter(dataAdapterVo);
|
||||
}
|
||||
return portalVo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDataInfo<SimplePortalVo> searchList(PortalQuery query) {
|
||||
PageUtils.startPage();
|
||||
@@ -162,6 +182,21 @@ public class PortalServiceImpl implements PortalService {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object mockExecute(MockParam mockParam) {
|
||||
Portal portal = portalMapper.selectById(mockParam.getPortalId());
|
||||
PortalBo portalBo = PortalBo.toPortalBo(portal, null);
|
||||
return handlerReader.mockExecute(portalBo, mockParam);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object mockData(MockParam mockParam) {
|
||||
Portal portal = portalMapper.selectById(mockParam.getPortalId());
|
||||
PortalBo portalBo = PortalBo.toPortalBo(portal, null);
|
||||
portalBo.setCreateDataAdapter(false);
|
||||
return handlerReader.mockExecute(portalBo, mockParam);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@@ -176,7 +211,7 @@ public class PortalServiceImpl implements PortalService {
|
||||
if (ObjectUtils.isEmpty(uniQuery)) {
|
||||
throw new CustomException("未找到对应的查询接口!");
|
||||
}
|
||||
portal.setState(0);
|
||||
portal.setState(QueryConstant.PORTAL_DEV);
|
||||
// 判断是否需要创建数据适配器
|
||||
if (portal.getCreateDataAdapter()) {
|
||||
createDataAdapter(portalDto, portal);
|
||||
@@ -203,6 +238,7 @@ public class PortalServiceImpl implements PortalService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public PortalIdWrapper edit(PortalDto portalDto) {
|
||||
Portal portal = portalDto.toPortal();
|
||||
checkPortalType(portal);
|
||||
@@ -210,7 +246,6 @@ public class PortalServiceImpl implements PortalService {
|
||||
if (!ObjectUtils.isEmpty(old) && !old.getPortalId().equals(portal.getPortalId())) {
|
||||
throw new CustomException("系统中存在当前请求路径");
|
||||
}
|
||||
|
||||
if (portal.getCreateDataAdapter() != old.getCreateDataAdapter()) {
|
||||
if (portal.getCreateDataAdapter()) {
|
||||
createDataAdapter(portalDto, portal);
|
||||
@@ -218,51 +253,55 @@ public class PortalServiceImpl implements PortalService {
|
||||
adapterMapper.deleteById(old.getAdapterId());
|
||||
}
|
||||
}
|
||||
PortalBo portalBo = PortalBo.toPortalBo(portal, portalDto.getMappings());
|
||||
//存在接口路径或者请求类型不同则需要重新发布
|
||||
if (!old.getPath().equals(portal.getPath())
|
||||
|| !old.getRequestMethod().equals(portal.getRequestMethod())) {
|
||||
//先卸载接口,需要进行数据同步
|
||||
unpublishPortal(old, true);
|
||||
//注册新的映射
|
||||
methodService.registerMapping(portalBo.getPath(), RequestMethod.valueOf(portalBo.getRequestMethod()), true);
|
||||
portalMappingMapper.deleteByPortalId(portal.getPortalId());
|
||||
portalMapper.update(portal);
|
||||
if (!ObjectUtils.isEmpty(portalDto.getMappings())) {
|
||||
List<PortalMapping> mappings = portalDto.getMappings();
|
||||
for (PortalMapping mapping : mappings) {
|
||||
mapping.setPortalId(portal.getPortalId());
|
||||
}
|
||||
portalMappingMapper.insertBatch(mappings);
|
||||
}
|
||||
//修改Redis中的数据信息
|
||||
redisTemplate.opsForValue().set(QueryConstant.PORTAL_KEY + portalBo.getPath() + ":" + portalBo.getRequestMethod(), portalBo);
|
||||
//返回接口id和适配器id
|
||||
return PortalIdWrapper.builder()
|
||||
.portalId(String.valueOf(portal.getPortalId()))
|
||||
.adapterId(String.valueOf(portal.getAdapterId()))
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
private boolean mappingCheckEquals(List<PortalMapping> oldPortalMappingList, List<PortalMapping> mappings) {
|
||||
if (ObjectUtils.isEmpty(oldPortalMappingList) && ObjectUtils.isEmpty(mappings)) {
|
||||
return true;
|
||||
@Override
|
||||
public void cancel(Long portalId) {
|
||||
Portal portal = portalMapper.selectById(portalId);
|
||||
if (ObjectUtils.isEmpty(portal)) {
|
||||
return;
|
||||
}
|
||||
if (ObjectUtils.isEmpty(oldPortalMappingList) || ObjectUtils.isEmpty(mappings)) {
|
||||
return false;
|
||||
if (QueryConstant.PORTAL_DEV.equals(portal.getState())) {
|
||||
throw new CustomException("当前状态错误");
|
||||
}
|
||||
Optional<PortalMapping> optional = mappings.stream().filter(ObjectUtils::isEmpty).findAny();
|
||||
if (optional.isEmpty()) {
|
||||
return false;
|
||||
portalMapper.updateState(portalId, QueryConstant.PORTAL_DEV);
|
||||
if (QueryConstant.PORTAL_PUBLISH.equals(portal.getState())) {
|
||||
unpublishPortal(portal, Boolean.TRUE);
|
||||
}
|
||||
if (oldPortalMappingList.size() == mappings.size()) {
|
||||
Map<Long, PortalMapping> oldPortalMappingMap = oldPortalMappingList.stream()
|
||||
.collect(Collectors.toMap(PortalMapping::getMappingId, Function.identity()));
|
||||
Map<Long, PortalMapping> portalMappingMap = mappings.stream()
|
||||
.collect(Collectors.toMap(PortalMapping::getMappingId, Function.identity()));
|
||||
for (Long mappingId : oldPortalMappingMap.keySet()) {
|
||||
PortalMapping portalMapping = portalMappingMap.get(mappingId);
|
||||
if (ObjectUtils.isEmpty(portalMapping)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Long portalId) {
|
||||
Portal portal = portalMapper.selectById(portalId);
|
||||
if (ObjectUtils.isEmpty(portal)) {
|
||||
return;
|
||||
}
|
||||
if (!QueryConstant.PORTAL_DEV.equals(portal.getState())) {
|
||||
throw new CustomException("当前状态不允许删除");
|
||||
}
|
||||
if (portal.getCreateDataAdapter()) {
|
||||
adapterMapper.deleteById(portal.getAdapterId());
|
||||
}
|
||||
portalMappingMapper.deleteByPortalId(portalId);
|
||||
portalMapper.deleteById(portalId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发布接口
|
||||
*
|
||||
@@ -302,7 +341,7 @@ public class PortalServiceImpl implements PortalService {
|
||||
if (ObjectUtils.isEmpty(dataAdapter.getType())) {
|
||||
throw new CustomException("请选择数据适配器类型!");
|
||||
}
|
||||
dataAdapter.setAdapterName(portal.getPortalName() + "专用数据适配器!");
|
||||
dataAdapter.setAdapterName(portal.getPortalName() + "专用");
|
||||
dataAdapter.setCommon(Boolean.FALSE);
|
||||
//初始化对应的数据适配器
|
||||
dataAdapter.init();
|
||||
|
||||
@@ -20,9 +20,10 @@
|
||||
<sql id="selectDataAdapterVo">
|
||||
select adapter_id,
|
||||
adapter_name,
|
||||
`type`,
|
||||
type,
|
||||
common,
|
||||
code,
|
||||
execute_code,
|
||||
mock_data,
|
||||
create_by,
|
||||
create_time,
|
||||
@@ -35,8 +36,10 @@
|
||||
<select id="selectList" resultMap="DataAdapterResult">
|
||||
<include refid="selectDataAdapterVo"/>
|
||||
<where>
|
||||
<if test="adapterName != null and adapterName != ''">and adapter_name like concat('%', #{adapterName}, '%')</if>
|
||||
<if test="type != null">and `type` = #{type}</if>
|
||||
<if test="adapterName != null and adapterName != ''">and adapter_name like concat('%', #{adapterName},
|
||||
'%')
|
||||
</if>
|
||||
<if test="type != null">and type = #{type}</if>
|
||||
<if test="common != null">and common = #{common}</if>
|
||||
</where>
|
||||
</select>
|
||||
@@ -58,7 +61,7 @@
|
||||
insert into data_adapter
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="adapterName != null">adapter_name,</if>
|
||||
<if test="type != null">`type`,</if>
|
||||
<if test="type != null">type,</if>
|
||||
<if test="common != null">common,</if>
|
||||
<if test="code != null">code,</if>
|
||||
<if test="mockData != null">mock_data,</if>
|
||||
@@ -80,7 +83,7 @@
|
||||
update data_adapter
|
||||
<set>
|
||||
<if test="adapterName != null">adapter_name = #{adapterName},</if>
|
||||
<if test="type != null">`type` = #{type},</if>
|
||||
<if test="type != null">type = #{type},</if>
|
||||
<if test="code != null">code = #{code},</if>
|
||||
<if test="mockData != null">mock_data = #{mockData},</if>
|
||||
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
|
||||
@@ -89,6 +92,18 @@
|
||||
where adapter_id = #{adapterId}
|
||||
</update>
|
||||
|
||||
<update id="updateCode">
|
||||
update data_adapter
|
||||
<set>
|
||||
<if test="code != null and code != ''">code = #{code},</if>
|
||||
<if test="executeCode != null and executeCode != ''">execute_code = #{code},</if>
|
||||
<if test="mockData != null and mockData != ''">mock_data = #{mockData},</if>
|
||||
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
|
||||
<if test="updateTime != null ">update_time = #{updateTime},</if>
|
||||
</set>
|
||||
where adapter_id = #{adapterId}
|
||||
</update>
|
||||
|
||||
<delete id="deleteById">
|
||||
delete
|
||||
from data_adapter
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
type,
|
||||
request_method,
|
||||
create_data_adapter,
|
||||
page,
|
||||
path,
|
||||
url,
|
||||
state,
|
||||
@@ -23,7 +24,6 @@
|
||||
remark
|
||||
from portal
|
||||
</sql>
|
||||
|
||||
<select id="selectById" resultType="cn.fateverse.query.entity.Portal">
|
||||
<include refid="selectVo"/>
|
||||
where portal_id = #{portalId}
|
||||
@@ -32,9 +32,10 @@
|
||||
<select id="selectList" resultType="cn.fateverse.query.entity.Portal">
|
||||
<include refid="selectVo"/>
|
||||
<where>
|
||||
<if test="portalName != null and portalName != ''">and portal_name like concat('%', #{portalName}, '%') </if>
|
||||
<if test="portalName != null and portalName != ''">and portal_name like concat('%', #{portalName}, '%')</if>
|
||||
<if test="anonymity != null ">and anonymity = #{anonymity}</if>
|
||||
<if test="type != null">and type = #{type}</if>
|
||||
<if test="state != null">and state = #{state}</if>
|
||||
<if test="path != null and path != ''">and path like concat('%', #{path}, '%')</if>
|
||||
</where>
|
||||
</select>
|
||||
@@ -52,6 +53,7 @@
|
||||
<if test="portalName != null">portal_name,</if>
|
||||
<if test="anonymity != null">anonymity,</if>
|
||||
<if test="type != null">type,</if>
|
||||
<if test="page != null">page,</if>
|
||||
<if test="requestMethod != null">request_method,</if>
|
||||
<if test="createDataAdapter != null">create_data_adapter,</if>
|
||||
<if test="path != null">path,</if>
|
||||
@@ -66,6 +68,7 @@
|
||||
<if test="portalName != null">#{portalName},</if>
|
||||
<if test="anonymity != null">#{anonymity},</if>
|
||||
<if test="type != null">#{type},</if>
|
||||
<if test="page != null">#{page},</if>
|
||||
<if test="requestMethod != null">#{requestMethod},</if>
|
||||
<if test="createDataAdapter != null">#{createDataAdapter},</if>
|
||||
<if test="path != null">#{path},</if>
|
||||
@@ -75,4 +78,35 @@
|
||||
<if test="createTime != null ">#{createTime},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="update">
|
||||
update portal
|
||||
<set>
|
||||
<if test="queryId != null">query_id = #{queryId},</if>
|
||||
<if test="adapterId != null">adapter_id = #{adapterId},</if>
|
||||
<if test="portalName != null">portal_name = #{portalName},</if>
|
||||
<if test="anonymity != null">anonymity = #{anonymity},</if>
|
||||
<if test="type != null">type = #{type},</if>
|
||||
<if test="page != null">type = #{page},</if>
|
||||
<if test="requestMethod != null">request_method = #{requestMethod},</if>
|
||||
<if test="createDataAdapter != null">create_data_adapter = #{createDataAdapter},</if>
|
||||
<if test="path != null">path = #{path},</if>
|
||||
<if test="url != null">url = #{url},</if>
|
||||
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
|
||||
<if test="updateTime != null">update_time = #{updateTime},</if>
|
||||
</set>
|
||||
where portal_id = #{portalId}
|
||||
</update>
|
||||
|
||||
<update id="updateState">
|
||||
update portal
|
||||
set state = #{state}
|
||||
where portal_id = #{portalId}
|
||||
</update>
|
||||
|
||||
<delete id="deleteById">
|
||||
delete
|
||||
from portal
|
||||
where portal_id = #{portalId}
|
||||
</delete>
|
||||
</mapper>
|
||||
@@ -4,7 +4,7 @@
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.fateverse.query.mapper.PortalMappingMapper">
|
||||
<sql id="selectPortalMapping">
|
||||
select mapping_id, portal_id, mapping_key, mapping_type, mapping_value
|
||||
select mapping_id, portal_id, mapping_key, mapping_type, mapping_value, input_type, description
|
||||
from portal_mapping
|
||||
</sql>
|
||||
|
||||
@@ -23,10 +23,11 @@
|
||||
|
||||
|
||||
<insert id="insertBatch">
|
||||
insert into portal_mapping (portal_id, mapping_key, mapping_type, mapping_value)
|
||||
insert into portal_mapping (portal_id, mapping_key, mapping_type, mapping_value, input_type, description)
|
||||
values
|
||||
<foreach collection="list" item="item" separator=",">
|
||||
(#{item.portalId}, #{item.mappingKey}, #{item.mappingType}, #{item.mappingValue})
|
||||
(#{item.portalId}, #{item.mappingKey}, #{item.mappingType}, #{item.mappingValue}, #{item.inputType},
|
||||
#{item.description})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class ConsoleCapture {
|
||||
public static String captureOutput(Runnable codeToRun) throws IOException {
|
||||
PrintStream originalOut = System.out;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream capturingOut = new PrintStream(baos);
|
||||
|
||||
try {
|
||||
System.setOut(capturingOut);
|
||||
codeToRun.run();
|
||||
} finally {
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
return baos.toString();
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
public static void main(String[] args) throws IOException {
|
||||
String capturedText = captureOutput(() -> {
|
||||
System.out.println("Hello, Console!");
|
||||
System.out.println("This is being captured.");
|
||||
});
|
||||
|
||||
System.out.println("Captured output:");
|
||||
System.out.println(capturedText);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
public class DataAdapter {
|
||||
|
||||
public static Object execute(List<Map<String, Object>> data) {
|
||||
for (Map<String, Object> objectMap : data) {
|
||||
objectMap.remove("oper_location");
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/22 18:59
|
||||
*/
|
||||
public class DataAdapter1100 {
|
||||
|
||||
public static Object execute(String data) {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
System.out.println(i);
|
||||
}
|
||||
return "qwewe";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import cn.fateverse.common.code.engine.JavaScriptEngine;
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.graalvm.polyglot.Context;
|
||||
import org.graalvm.polyglot.Value;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.script.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/23 9:42
|
||||
*/
|
||||
public class JavaScriptParamTest {
|
||||
|
||||
|
||||
static String finalCode = "function execute1(data) {\n" +
|
||||
" let res = [data.length];\n" +
|
||||
" for (let i = 0; i < data.length; i++) {\n" +
|
||||
" console.log(data[i])\n" +
|
||||
" console.log(data[i].foo)\n" +
|
||||
" res[i] = data[i].foo\n" +
|
||||
" }\n" +
|
||||
" return res;\n" +
|
||||
"}";
|
||||
|
||||
@Test
|
||||
public void testParam() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
||||
Map<String, Object> param = new HashMap<>();
|
||||
param.put("foo", "sdsdsdsds");
|
||||
list.add(param);
|
||||
}
|
||||
|
||||
ScriptEngineManager engineManager = new ScriptEngineManager();
|
||||
ScriptEngine jsEngine = engineManager.getEngineByName("graal.js");
|
||||
Context context = Context.newBuilder()
|
||||
.allowAllAccess(true)
|
||||
.build();
|
||||
// Context context = Context.create();
|
||||
context.eval("js", finalCode);
|
||||
|
||||
Value executeFunction = context.getBindings("js").getMember("execute1");
|
||||
Value javaObjectAsValue = Value.asValue(list);
|
||||
Value result = executeFunction.execute(javaObjectAsValue);
|
||||
System.out.println(result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testParam2() throws Exception {
|
||||
|
||||
ScriptEngineManager engineManager = new ScriptEngineManager();
|
||||
ScriptEngine jsEngine = engineManager.getEngineByName("graal.js");
|
||||
Invocable funcCall = (Invocable) jsEngine;
|
||||
Bindings bindings = jsEngine.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
bindings.put("polyglot.js.allowHostAccess", true);
|
||||
bindings.put("polyglot.js.allowHostClassLookup", (Predicate<String>) s -> true);
|
||||
|
||||
jsEngine.eval(finalCode);
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("foo", "sdsdsdsds");
|
||||
Object res= funcCall.invokeFunction("execute1", jsonObject);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import cn.fateverse.common.code.config.JavaCodeProperties;
|
||||
import cn.fateverse.common.code.engine.JavaCodeEngine;
|
||||
import cn.fateverse.common.code.engine.JavaScriptEngine;
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/22 16:04
|
||||
*/
|
||||
public class JavaScriptTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
String code = "function execute(data) {\n" +
|
||||
" console.log(data)\n" +
|
||||
" console.log(\"test console!\")\n" +
|
||||
" let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
" console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
" for (let i = 0; i < 1000; i++) {\n" +
|
||||
" console.log(i)\n" +
|
||||
" }" +
|
||||
" return \"success\";\n" +
|
||||
"}";
|
||||
|
||||
|
||||
JavaCodeProperties properties = new JavaCodeProperties();
|
||||
properties.setClassPath("C:\\home\\clay\\code\\");
|
||||
JavaCodeEngine javaCodeEngine = new JavaCodeEngine(properties);
|
||||
|
||||
|
||||
// EngineResult<Object> executeScript = JavaScriptEngine.executeScript(code, "execute", "测试控制台输出");
|
||||
|
||||
String finalCode = "function execute(data) {\n" +
|
||||
" console.log(data)\n" +
|
||||
" console.log(\"test console!\")\n" +
|
||||
" let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
" console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
" for (let i = 0; i < 1000; i++) {\n" +
|
||||
" console.log(i)\n" +
|
||||
" }" +
|
||||
" return \"success\";\n" +
|
||||
"}";
|
||||
FutureTask<EngineResult> engineResultFutureTask = new FutureTask<>(() -> {
|
||||
return JavaScriptEngine.execute(finalCode, "execute", false, "测试控制台输出");
|
||||
});
|
||||
|
||||
|
||||
String finalCode1 = "import java.util.*;\n" +
|
||||
"import java.util.stream.*;\n" +
|
||||
"\n" +
|
||||
"public class DataAdapter100 {" +
|
||||
"\n" +
|
||||
" public static Object execute(String data) {\n" +
|
||||
" for (int i = 0; i < 1000; i++) {\n" +
|
||||
" System.out.println(i);\n" +
|
||||
" }\n" +
|
||||
" return \"qwewe\";\n" +
|
||||
" }" +
|
||||
"}\n";
|
||||
FutureTask<EngineResult> engineResultFutureTask1 = new FutureTask<>(() -> {
|
||||
return javaCodeEngine.execute(finalCode1, "DataAdapter100", "execute", new Class[]{String.class}, new Object[]{"test"}, true);
|
||||
});
|
||||
new Thread(engineResultFutureTask).start();
|
||||
new Thread(engineResultFutureTask1).start();
|
||||
// EngineResult<Object> execute = javaCodeEngine.execute(code, "DataAdapter100", "execute", new Class[]{String.class}, new Object[]{"test"}, true);
|
||||
|
||||
|
||||
EngineResult x = engineResultFutureTask.get();
|
||||
EngineResult x1 = engineResultFutureTask1.get();
|
||||
System.out.println(x);
|
||||
System.out.println(x1);
|
||||
|
||||
|
||||
// EngineResult<Object> capture = MultiThreadedCapture.capture(() -> {
|
||||
// ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
|
||||
// Invocable inv = (Invocable) engine;
|
||||
// try {
|
||||
// engine.eval(finalCode);
|
||||
// return inv.invokeFunction("execute", "args");
|
||||
// } catch (ScriptException | NoSuchMethodException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// });
|
||||
// System.out.println(capture);
|
||||
// EngineResult<Object> objectEngineResult = JavaScriptEngine.executeScript(finalCode, "execute", "测试控制台输出");
|
||||
//
|
||||
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
// PrintStream oldOut = System.out;
|
||||
// System.setOut(new PrintStream(baos));
|
||||
|
||||
|
||||
// EngineResult<Object> capture = MultiThreadedCapture.capture(() -> {
|
||||
// int someNumber = 42;
|
||||
// System.out.println(someNumber);
|
||||
// return null;
|
||||
// });
|
||||
|
||||
|
||||
// 执行产生数值输出的代码
|
||||
// System.out.println(objectEngineResult);
|
||||
// ScriptEngine engine = JavaScriptEngine.getEngine();
|
||||
// Invocable inv = (Invocable) engine;
|
||||
// engine.eval(finalCode);
|
||||
// inv.invokeFunction("execute", "args");
|
||||
// System.out.println(capture);
|
||||
|
||||
// MultiThreadedCapture.executor.shutdown();
|
||||
// 恢复标准输出流
|
||||
// System.setOut(oldOut);
|
||||
//
|
||||
// System.setOut(oldOut);
|
||||
// // 从捕获的字节数组输出流中获取打印的文本
|
||||
// String capturedOutput = baos.toString();
|
||||
// System.out.println("Captured output: " + capturedOutput);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import org.graalvm.polyglot.Context;
|
||||
import org.graalvm.polyglot.Value;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// 创建 GraalVM 上下文
|
||||
Context context = Context.create();
|
||||
|
||||
// 定义一个 JavaScript 函数
|
||||
String script = "function execute(data) {\n" +
|
||||
" console.log(data)\n" +
|
||||
" console.log(\"test console!\")\n" +
|
||||
" let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
" console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
" for (let i = 0; i < 1000; i++) {\n" +
|
||||
" console.log(i)\n" +
|
||||
" }" +
|
||||
" return \"success\";\n" +
|
||||
"}";
|
||||
|
||||
// 编译 JavaScript 函数
|
||||
context.eval("js", script);
|
||||
|
||||
// 获取函数对象
|
||||
Value addFunction = context.getBindings("js").getMember("execute");
|
||||
|
||||
// 执行函数并传入参数
|
||||
Value result = addFunction.execute("测试");
|
||||
Value result2 = addFunction.execute("测试");
|
||||
// 获取返回值
|
||||
// int sum = result.asInt();
|
||||
|
||||
// 输出结果
|
||||
System.out.println("Sum: "); // 输出:Sum: 5
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import cn.fateverse.common.code.engine.JavaScriptEngine;
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class SystemOut {
|
||||
public static void outTest() {
|
||||
ByteArrayOutputStream baoStream = new ByteArrayOutputStream(1024);
|
||||
PrintStream cacheStream = new PrintStream(baoStream);//临时输出
|
||||
PrintStream oldStream = System.out;//缓存系统输出
|
||||
System.setOut(cacheStream);
|
||||
System.out.print("...");//不会打印到控制台
|
||||
String result = baoStream.toString();//存放控制台输出的字符串
|
||||
System.setOut(oldStream);//还原到系统输出
|
||||
System.out.println("result");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// outTest();
|
||||
|
||||
String finalCode = "function execute1(data) {\n" +
|
||||
" console.log(data)\n" +
|
||||
" console.log(\"test console!\")\n" +
|
||||
" let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
" console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
" for (let i = 0; i < 1000; i++) {\n" +
|
||||
" console.log(i)\n" +
|
||||
" }" +
|
||||
" return \"success\";\n" +
|
||||
"}";
|
||||
// String finalCode = "function myFunction() { return 'Hello, world!'; }";
|
||||
long start1 = System.currentTimeMillis();
|
||||
EngineResult execute = JavaScriptEngine.execute(finalCode, "execute1", false, "测试控制台输出");
|
||||
long start2 = System.currentTimeMillis();
|
||||
EngineResult execute1 = JavaScriptEngine.execute(finalCode, "execute1", false, "测试控制台输出");
|
||||
long start3 = System.currentTimeMillis();
|
||||
System.out.println("耗时1:" + (start2 - start1) + "ms");
|
||||
System.out.println("耗时2:" + (start3 - start2) + "ms");
|
||||
System.out.println(execute);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user