refactor: 解析只用Groovy脚本,无脚本直接报错,移除Java引擎兜底
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package com.par.api.controller;
|
package com.par.api.controller;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.par.core.adapter.GroovyScriptEngine;
|
||||||
import com.par.core.adapter.AdapterConfig;
|
|
||||||
import com.par.core.adapter.AdapterEngine;
|
|
||||||
import com.par.core.dto.ApiResponse;
|
import com.par.core.dto.ApiResponse;
|
||||||
import com.par.core.entity.AccessResult;
|
import com.par.core.entity.AccessResult;
|
||||||
import com.par.core.entity.SiteConfig;
|
import com.par.core.entity.SiteConfig;
|
||||||
@@ -24,12 +22,10 @@ import java.util.stream.Collectors;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class AdapterController {
|
public class AdapterController {
|
||||||
|
|
||||||
private final AdapterEngine adapterEngine;
|
|
||||||
private final GroovyScriptEngine groovyEngine;
|
private final GroovyScriptEngine groovyEngine;
|
||||||
private final SiteConfigMapper siteConfigMapper;
|
private final SiteConfigMapper siteConfigMapper;
|
||||||
private final AccessResultMapper accessResultMapper;
|
private final AccessResultMapper accessResultMapper;
|
||||||
private final CachedStorageService storageService;
|
private final CachedStorageService storageService;
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用指定适配配置解析指定访问结果
|
* 用指定适配配置解析指定访问结果
|
||||||
@@ -45,38 +41,26 @@ public class AdapterController {
|
|||||||
return ApiResponse.error(400, "configId and accessResultId are required");
|
return ApiResponse.error(400, "configId and accessResultId are required");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载适配配置
|
|
||||||
SiteConfig siteConfig = siteConfigMapper.selectById(configId);
|
SiteConfig siteConfig = siteConfigMapper.selectById(configId);
|
||||||
if (siteConfig == null) return ApiResponse.error(404, "配置不存在");
|
if (siteConfig == null) return ApiResponse.error(404, "配置不存在");
|
||||||
|
if (siteConfig.getScriptStoragePath() == null || siteConfig.getScriptStoragePath().isBlank()) {
|
||||||
|
return ApiResponse.error(400, "该配置未绑定 Groovy 脚本");
|
||||||
|
}
|
||||||
|
|
||||||
String configJson;
|
String script;
|
||||||
try { configJson = storageService.read(siteConfig.getStoragePath()); }
|
try { script = storageService.read(siteConfig.getScriptStoragePath()); }
|
||||||
catch (Exception e) { return ApiResponse.error(500, "读取配置失败: " + e.getMessage()); }
|
catch (Exception e) { return ApiResponse.error(500, "读取脚本失败: " + e.getMessage()); }
|
||||||
if (configJson == null) return ApiResponse.error(404, "配置内容为空");
|
if (script == null || script.isBlank()) return ApiResponse.error(404, "Groovy 脚本为空");
|
||||||
|
|
||||||
AdapterConfig adapterConfig;
|
|
||||||
try { adapterConfig = objectMapper.readValue(configJson, AdapterConfig.class); }
|
|
||||||
catch (Exception e) { return ApiResponse.error(400, "配置格式无效: " + e.getMessage()); }
|
|
||||||
|
|
||||||
// 加载访问结果 HTML
|
|
||||||
AccessResult accessResult = accessResultMapper.selectById(accessResultId);
|
AccessResult accessResult = accessResultMapper.selectById(accessResultId);
|
||||||
if (accessResult == null) return ApiResponse.error(404, "访问结果不存在");
|
if (accessResult == null) return ApiResponse.error(404, "访问结果不存在");
|
||||||
|
|
||||||
String html;
|
String content;
|
||||||
try { html = storageService.read(accessResult.getStoragePath()); }
|
try { content = storageService.read(accessResult.getStoragePath()); }
|
||||||
catch (Exception e) { return ApiResponse.error(500, "读取访问结果失败: " + e.getMessage()); }
|
catch (Exception e) { return ApiResponse.error(500, "读取内容失败: " + e.getMessage()); }
|
||||||
if (html == null) return ApiResponse.error(404, "访问结果内容为空");
|
if (content == null) return ApiResponse.error(404, "内容为空");
|
||||||
|
|
||||||
// 执行解析
|
return ApiResponse.success(groovyEngine.execute(script, content, accessResult.getUrl()));
|
||||||
Map<String, Object> result;
|
|
||||||
try {
|
|
||||||
result = adapterEngine.extract(adapterConfig, html, accessResult.getUrl());
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Adapter parse error", e);
|
|
||||||
return ApiResponse.error(500, "解析异常: " + e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ApiResponse.success(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,24 +122,22 @@ public class AdapterController {
|
|||||||
item.put("invalidCount", allResults.stream().filter(r -> url.equals(r.getUrl()) && Boolean.FALSE.equals(r.getIsValid())).count());
|
item.put("invalidCount", allResults.stream().filter(r -> url.equals(r.getUrl()) && Boolean.FALSE.equals(r.getIsValid())).count());
|
||||||
item.put("uncheckedCount", allResults.stream().filter(r -> url.equals(r.getUrl()) && r.getIsValid() == null).count());
|
item.put("uncheckedCount", allResults.stream().filter(r -> url.equals(r.getUrl()) && r.getIsValid() == null).count());
|
||||||
|
|
||||||
// 解析内容:优先 Groovy 脚本,否则用 Java 引擎
|
// 解析内容:必须有 Groovy 脚本
|
||||||
try {
|
try {
|
||||||
String content = storageService.read(ar.getStoragePath());
|
if (siteConfig.getScriptStoragePath() == null || siteConfig.getScriptStoragePath().isBlank()) {
|
||||||
if (content != null) {
|
item.put("parsed", Map.of("matched", false, "message", "该配置未绑定 Groovy 脚本"));
|
||||||
Map<String, Object> parsed;
|
|
||||||
if (siteConfig.getScriptStoragePath() != null && !siteConfig.getScriptStoragePath().isBlank()) {
|
|
||||||
String script = storageService.read(siteConfig.getScriptStoragePath());
|
|
||||||
if (script != null && !script.isBlank()) {
|
|
||||||
parsed = groovyEngine.execute(script, content, url);
|
|
||||||
} else {
|
|
||||||
parsed = adapterEngine.extract(adapterConfig, content, ar.getContentType(), url);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
parsed = adapterEngine.extract(adapterConfig, content, ar.getContentType(), url);
|
|
||||||
}
|
|
||||||
item.put("parsed", parsed);
|
|
||||||
} else {
|
} else {
|
||||||
item.put("parsed", Map.of("matched", false, "message", "内容为空"));
|
String script = storageService.read(siteConfig.getScriptStoragePath());
|
||||||
|
if (script == null || script.isBlank()) {
|
||||||
|
item.put("parsed", Map.of("matched", false, "message", "Groovy 脚本为空"));
|
||||||
|
} else {
|
||||||
|
String content = storageService.read(ar.getStoragePath());
|
||||||
|
if (content == null) {
|
||||||
|
item.put("parsed", Map.of("matched", false, "message", "内容为空"));
|
||||||
|
} else {
|
||||||
|
item.put("parsed", groovyEngine.execute(script, content, url));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
item.put("parsed", Map.of("matched", false, "message", "解析异常: " + e.getMessage()));
|
item.put("parsed", Map.of("matched", false, "message", "解析异常: " + e.getMessage()));
|
||||||
|
|||||||
Reference in New Issue
Block a user