Browse Source

Pull request #12274: REPORT-98879 修复jxbrowser7在windows下路径问题

Merge in DESIGN/design from ~VITO/c-design:release/11.0 to release/11.0

* commit 'c3434d1c1f9f7d1e3126d9b01b04218b49cfb59f':
  REPORT-98879 修复jxbrowser7在windows下路径问题
newui
vito-刘恒霖 1 year ago
parent
commit
6a6a01afe8
  1. 25
      designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java
  2. 10
      designer-base/src/main/java/com/fr/design/jxbrowser/NxInterceptRequestCallback.java

25
designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java

@ -8,6 +8,7 @@ import com.fr.design.ui.ModernUIConstants;
import com.fr.design.ui.ModernUIPane;
import com.fr.stable.StringUtils;
import com.fr.stable.collections.combination.Pair;
import com.fr.stable.os.OperatingSystem;
import com.fr.web.struct.AssembleComponent;
import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.browser.callback.InjectJsCallback;
@ -47,6 +48,12 @@ import static com.fr.design.ui.ModernUIConstants.WINDOW;
*/
public class JxUIPane<T> extends ModernUIPane<T> {
/**
* 冒号
*/
public static final String COLON = ":";
private static final String COLON_ESCAPE = "\\:";
private Browser browser;
private String namespace = "Pool";
private String variable = "data";
@ -126,7 +133,7 @@ public class JxUIPane<T> extends ModernUIPane<T> {
*/
@Override
public void redirect(String url) {
browser.navigation().loadUrl(url);
browser.navigation().loadUrl(encodeWindowsPath(url));
}
/**
@ -138,7 +145,7 @@ public class JxUIPane<T> extends ModernUIPane<T> {
@Override
public void redirect(String url, Map<String, String> map) {
setMap(map);
browser.navigation().loadUrl(url);
browser.navigation().loadUrl(encodeWindowsPath(url));
}
private void setMap(Map<String, String> map) {
@ -220,6 +227,18 @@ public class JxUIPane<T> extends ModernUIPane<T> {
return Optional.ofNullable(frame.executeJavaScript(name));
}
/**
* 由于自定义scheme目前走的是url因此路径会被自动转化比如windows路径下对冒号问题
* C:\\abc 变成 /C/abc这里对冒号进行编码转义
*/
private static String encodeWindowsPath(String path) {
if (OperatingSystem.isWindows() && path.startsWith(EMB_TAG + SCHEME_HEADER)) {
String s = path.split(EMB_TAG + SCHEME_HEADER)[1];
return EMB_TAG + SCHEME_HEADER + s.replace(COLON, COLON_ESCAPE);
}
return path;
}
/**
* JxUIPane 的建造者
*
@ -492,7 +511,7 @@ public class JxUIPane<T> extends ModernUIPane<T> {
pane.browser.navigation().on(listenerPair.getFirst(), listenerPair.getSecond());
}
if (StringUtils.isNotEmpty(this.url)) {
pane.browser.navigation().loadUrl(this.url);
pane.browser.navigation().loadUrl(encodeWindowsPath(this.url));
} else if (StringUtils.isNotEmpty(this.html)) {
pane.browser.mainFrame().ifPresent(f -> f.loadHtml(html));
}

10
designer-base/src/main/java/com/fr/design/jxbrowser/NxInterceptRequestCallback.java

@ -20,6 +20,7 @@ import com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback;
import org.jetbrains.annotations.NotNull;
import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
@ -36,6 +37,8 @@ import static com.fr.design.ui.ModernUIConstants.COMPONENT_TAG;
*/
public class NxInterceptRequestCallback implements InterceptUrlRequestCallback {
private static final String COLON_DECODE_ESCAPE = "/:";
private static final String SCHEME_SPLIT = ":/";
private Supplier<AssembleComponent> component;
private Supplier<Map<String, String>> renderParameterBuild;
@ -61,7 +64,7 @@ public class NxInterceptRequestCallback implements InterceptUrlRequestCallback {
@Override
public Response on(Params params) {
UrlRequest urlRequest = params.urlRequest();
String path = urlRequest.url();
String path = urlRequest.url().replace(COLON_DECODE_ESCAPE, JxUIPane.COLON);
Optional<UrlRequestJob> urlRequestJobOptional;
if (path.startsWith(COMPONENT_TAG)) {
String text = htmlText(renderParameterBuild.get());
@ -107,7 +110,10 @@ public class NxInterceptRequestCallback implements InterceptUrlRequestCallback {
path = path.substring(index + 1);
} else {
// jxbrowser 7之后,协议会自动补齐双斜杠//
path = path.split(":/")[1];
int i = path.indexOf(SCHEME_SPLIT);
path = path.substring(i + SCHEME_SPLIT.length());
// 通过自定义协议之后的url会自动encode一些中文字符,这里做一个decode,否则会导致路径访问失败
path = URLDecoder.decode(path, StandardCharsets.UTF_8.name());
}
return IOUtils.readResource(path);
}

Loading…
Cancel
Save