feat(report): 钻取链接 reportviewer.jsp → EIP4 渲染路由

JOR 报表 <Hyperlink><Url>reportviewer.jsp?xmluuid=<目标报表ID>&PARAM=${PARAM}…</Url> 经 jatools
HtmlExport 渲成 <a href='reportviewer.jsp?xmluuid=…' target='_blank'>(jar 内确认 <a href' 发射);
EIP4 下老 jsp 已失效,点击钻取 404。ReportService.rewriteDrillLinks(仅 HTML 通道,渲染后处理)正则
reportviewer\.jsp\?xmluuid=([^&'"\s<>]+) → /api/report/render/$1?format=html,其余查询参原样保留
→ 点击钻取在新标签渲出目标报表(目标自身钻取链接亦被同一步递归改写,因每次 render 都过此步)。

- 短路:无 "reportviewer.jsp" 子串原样返回(302/339 报表跳过正则);Pattern 静态编译一次。
- replaceAll(Function) 字面量拼接,即便目标 id 含 $/\ 也不破坏替换(评审 MINOR-1 收口)。
- 6 单测:真发射锚格式(单引号 href+&amp;+target=_blank)/裸&分隔/多处/无链接短路同实例/null。

验证边界:钻取行在报表明细行,eip4dev 缺源数据 → dev 渲不出 drill 链接,点击直通须完整客户库;
本批=改写逻辑单测 + jar 发射证实(HtmlExport <a href')+ 评审实证全 37 张 xmluuid 恒首参 +
live 正常报表(5558497f CJK)HTML round-trip 无损、drill 报表渲染 200 不报错。code review APPROVE。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-25 23:24:59 +08:00
parent d78de96530
commit 61b8658f44
3 changed files with 97 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Map;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -120,6 +121,12 @@ public class ReportService {
case PDF -> job.printAsPDF(out);
case XLS -> job.printAsXLS(out);
}
if (fmt == Format.HTML) {
// JOR 钻取链接 <a href='reportviewer.jsp?xmluuid=X...'> 指向已失效的老 JSP →
// 改写为 EIP4 渲染路由 /api/report/render/X?format=html(目标报表在新标签渲出)。
String rewritten = rewriteDrillLinks(out.toString(StandardCharsets.UTF_8));
return rewritten.getBytes(StandardCharsets.UTF_8);
}
return out.toByteArray();
} catch (RuntimeException e) {
throw e;
@@ -130,6 +137,23 @@ public class ReportService {
}
}
/**
* JOR 钻取链接 {@code reportviewer.jsp?xmluuid=<目标报表ID>&...} → EIP4 渲染路由
* {@code /api/report/render/<目标报表ID>?format=html}(其余查询参原样保留;目标 id 后以 {@code &}/引号/
* 空白/尖括号为界)。仅作用于 HTML 输出;无该子串时短路原样返回。纯函数,便于单测。
*/
private static final Pattern DRILL_LINK =
Pattern.compile("reportviewer\\.jsp\\?xmluuid=([^&'\"\\s<>]+)");
static String rewriteDrillLinks(String html) {
if (html == null || !html.contains("reportviewer.jsp")) {
return html;
}
// 用 Function 形式 replaceAll:返回串按字面量拼接(不解释 $ / \),即便目标 id 含特殊字符也不会破坏替换。
return DRILL_LINK.matcher(html)
.replaceAll(m -> "/api/report/render/" + m.group(1) + "?format=html");
}
private static void deleteQuietly(File dir) {
if (dir == null || !dir.exists()) {
return;

View File

@@ -0,0 +1,71 @@
package com.eip4.platform.report;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
* JOR 钻取链接改写的纯逻辑单测。
*
* <p>JOR 模板的 {@code <Hyperlink><Url>reportviewer.jsp?xmluuid=<目标报表ID>&PARAM=${PARAM}...</Url>}
* 经 jatools {@code HtmlExport} 渲成 {@code <a href='reportviewer.jsp?xmluuid=...&amp;PARAM=值' target='_blank'>}
* (jar 内已确认 {@code <a href='} 发射)。EIP4 下老 {@code reportviewer.jsp} 已失效,须把它改写成
* EIP4 渲染路由 {@code /api/report/render/<目标报表ID>?format=html},其余查询参原样保留 → 点击钻取在新标签
* 渲出目标报表(目标报表自身的钻取链接也会被同一改写递归处理,因为每次 render 都过此步)。
*/
class ReportDrillLinkRewriteTest {
@Test
void rewrites_real_jor_rendered_drill_anchor() {
// 与 HtmlExport 实际发射格式一致:单引号 href + &amp; 分隔 + target=_blank
String html = "<td><a href='reportviewer.jsp?xmluuid=19482910-7cf7-44fe-9612-bc9859bfe9d9"
+ "&amp;PROJECTID=P1&amp;MREPORTINTERVALID=9' target='_blank'>人工</a></td>";
String out = ReportService.rewriteDrillLinks(html);
assertTrue(out.contains(
"href='/api/report/render/19482910-7cf7-44fe-9612-bc9859bfe9d9?format=html"
+ "&amp;PROJECTID=P1&amp;MREPORTINTERVALID=9'"), out);
assertFalse(out.contains("reportviewer.jsp"), out);
// target/锚文本不动
assertTrue(out.contains("target='_blank'") && out.contains(">人工</a>"), out);
}
@Test
void rewrites_link_without_params() {
String html = "<a href='reportviewer.jsp?xmluuid=abcd1234-0000-1111-2222-333344445555'>x</a>";
String out = ReportService.rewriteDrillLinks(html);
assertTrue(out.contains("href='/api/report/render/abcd1234-0000-1111-2222-333344445555?format=html'"), out);
}
@Test
void rewrites_all_occurrences() {
String html = "a reportviewer.jsp?xmluuid=11111111-1111-1111-1111-111111111111&amp;X=1 b "
+ "reportviewer.jsp?xmluuid=22222222-2222-2222-2222-222222222222 c";
String out = ReportService.rewriteDrillLinks(html);
assertTrue(out.contains("/api/report/render/11111111-1111-1111-1111-111111111111?format=html&amp;X=1"), out);
assertTrue(out.contains("/api/report/render/22222222-2222-2222-2222-222222222222?format=html"), out);
assertFalse(out.contains("reportviewer.jsp"), out);
}
@Test
void rewrites_with_bare_ampersand_separator() {
// 防御引擎可能发射裸 & 分隔(非 &amp;):char-class 须在 & 处停止捕获 id
String html = "x reportviewer.jsp?xmluuid=55555555-5555-5555-5555-555555555555&PROJECTID=P";
String out = ReportService.rewriteDrillLinks(html);
assertTrue(out.contains("/api/report/render/55555555-5555-5555-5555-555555555555?format=html&PROJECTID=P"), out);
assertFalse(out.contains("reportviewer.jsp"), out);
}
@Test
void no_drill_links_returns_same_instance() {
String html = "<html><body><table><tr><td>合计</td></tr></table></body></html>";
assertSame(html, ReportService.rewriteDrillLinks(html), "无钻取链接应原样返回(短路)");
}
@Test
void null_safe() {
assertEquals(null, ReportService.rewriteDrillLinks(null));
}
}