From 9dc3b6b87709b24fd9c0e8e0f485f05a3574a1d8 Mon Sep 17 00:00:00 2001 From: hades Date: Mon, 7 Jun 2021 15:17:01 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-7634=20mac=E5=92=8Cwin=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E7=89=88=E6=9C=AC=E7=9A=84jxbrowser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 27 +++- .../NxComplexInterceptRequestCallback.java | 87 ++++++++++++ .../NxInterceptRequestCallback.java | 134 ++++++++++++++++++ 3 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/ui/compatible/NxComplexInterceptRequestCallback.java create mode 100644 designer-base/src/main/java/com/fr/design/ui/compatible/NxInterceptRequestCallback.java diff --git a/build.gradle b/build.gradle index 617b9d0fa8..781c65e227 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,17 @@ dependencies { api project(':designer-realize') } +configurations.all { + resolutionStrategy { + force 'com.fr.third:jxbrowser:6.23' + force 'com.fr.third:jxbrowser-mac:6.23' + force 'com.fr.third:jxbrowser-win64:6.23' + force 'com.fr.third:jxbrowser:7.5' + force 'com.fr.third:jxbrowser-mac:7.5' + force 'com.fr.third:jxbrowser-win64:7.5' + } +} + allprojects { apply plugin: 'java' apply plugin: 'java-library' @@ -57,9 +68,19 @@ allprojects { } dependencies { - implementation 'com.fr.third:jxbrowser:6.23' - implementation 'com.fr.third:jxbrowser-mac:6.23' - implementation 'com.fr.third:jxbrowser-win64:6.23' + implementation ('com.fr.third:jxbrowser:6.23') { + force = true + } + implementation ('com.fr.third:jxbrowser-mac:6.23') { + force = true + } + implementation ('com.fr.third:jxbrowser-win64:6.23') { + force = true + } + implementation 'com.fr.third:jxbrowser:7.5' + implementation 'com.fr.third:jxbrowser-mac:7.5' + implementation 'com.fr.third:jxbrowser-win64:7.5' + implementation 'com.fr.third:jxbrowser-swing:7.5' implementation 'com.fr.third.server:servlet-api:3.0' implementation 'org.swingexplorer:swexpl:2.0.1' implementation 'org.swingexplorer:swag:1.0' diff --git a/designer-base/src/main/java/com/fr/design/ui/compatible/NxComplexInterceptRequestCallback.java b/designer-base/src/main/java/com/fr/design/ui/compatible/NxComplexInterceptRequestCallback.java new file mode 100644 index 0000000000..2c430bd348 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/ui/compatible/NxComplexInterceptRequestCallback.java @@ -0,0 +1,87 @@ +package com.fr.design.ui.compatible; + +import com.fr.design.ui.ModernRequestClient; +import com.fr.design.ui.ModernUIConstants; +import com.fr.general.IOUtils; +import com.fr.stable.StringUtils; +import com.fr.web.struct.AssembleComponent; +import com.fr.web.struct.AtomBuilder; +import com.fr.web.struct.PathGroup; +import com.fr.web.struct.category.ScriptPath; +import com.fr.web.struct.category.StylePath; +import com.teamdev.jxbrowser.net.Network; +import com.teamdev.jxbrowser.net.UrlRequest; +import com.teamdev.jxbrowser.net.callback.InterceptRequestCallback; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2020/3/25 + */ +public class NxComplexInterceptRequestCallback extends NxInterceptRequestCallback { + + private AssembleComponent component; + + public NxComplexInterceptRequestCallback(Network network, AssembleComponent component) { + super(network); + this.component = component; + } + + public NxComplexInterceptRequestCallback(Network network, AssembleComponent component, Map map) { + super(network, map); + this.component = component; + } + + @Override + protected Response next(UrlRequest urlRequest, String path) { + if (path.startsWith("emb:dynamic")) { + String text = htmlText(map); + return InterceptRequestCallback.Response.intercept(generateBasicUrlRequestJob(urlRequest, "text/html", text.getBytes(StandardCharsets.UTF_8))); + } else { + int index = path.indexOf("="); + if (index > 0) { + path = path.substring(index + 1); + } else { + path = path.substring(4); + } + InputStream inputStream = IOUtils.readResource(path); + return InterceptRequestCallback.Response.intercept(generateBasicUrlRequestJob(urlRequest, getMimeType(path), IOUtils.inputStream2Bytes(inputStream))); + } + } + + private String htmlText(Map map) { + PathGroup pathGroup = AtomBuilder.create().buildAssembleFilePath(ModernRequestClient.KEY, component); + StylePath[] stylePaths = pathGroup.toStylePathGroup(); + StringBuilder styleText = new StringBuilder(); + for (StylePath path : stylePaths) { + if (StringUtils.isNotBlank(path.toFilePath())) { + styleText.append(""); + } + } + String result = ModernUIConstants.HTML_TPL.replaceAll("##style##", styleText.toString()); + ScriptPath[] scriptPaths = pathGroup.toScriptPathGroup(); + StringBuilder scriptText = new StringBuilder(); + for (ScriptPath path : scriptPaths) { + if (StringUtils.isNotBlank(path.toFilePath())) { + scriptText.append(""); + } + } + result = result.replaceAll("##script##", scriptText.toString()); + if (map != null) { + for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + result = result.replaceAll("\\$\\{" + key + "}", value); + } + } + return result; + } +} \ No newline at end of file diff --git a/designer-base/src/main/java/com/fr/design/ui/compatible/NxInterceptRequestCallback.java b/designer-base/src/main/java/com/fr/design/ui/compatible/NxInterceptRequestCallback.java new file mode 100644 index 0000000000..5ec0ff4339 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/ui/compatible/NxInterceptRequestCallback.java @@ -0,0 +1,134 @@ +package com.fr.design.ui.compatible; + +import com.fr.base.TemplateUtils; +import com.fr.general.IOUtils; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.ArrayUtils; +import com.fr.stable.EncodeConstants; +import com.fr.stable.StringUtils; +import com.fr.third.org.apache.commons.codec.net.URLCodec; +import com.teamdev.jxbrowser.net.HttpHeader; +import com.teamdev.jxbrowser.net.HttpStatus; +import com.teamdev.jxbrowser.net.Network; +import com.teamdev.jxbrowser.net.UrlRequest; +import com.teamdev.jxbrowser.net.UrlRequestJob; +import com.teamdev.jxbrowser.net.callback.InterceptRequestCallback; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.Optional; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2020/3/25 + */ +public class NxInterceptRequestCallback implements InterceptRequestCallback { + + Network network; + Map map; + + public NxInterceptRequestCallback(Network network) { + this.network = network; + } + + public NxInterceptRequestCallback(Network network, Map map) { + this.network = network; + this.map = map; + } + + @Override + public Response on(Params params) { + UrlRequest urlRequest = params.urlRequest(); + String path = urlRequest.url(); + if (path.startsWith("file:")) { + Optional optional = generateFileProtocolUrlRequestJob(urlRequest, path); + if (optional.isPresent()) { + return InterceptRequestCallback.Response.intercept(optional.get()); + } + } else { + return next(urlRequest, path); + } + return Response.proceed(); + } + + Response next(UrlRequest urlRequest, String path) { + return Response.proceed(); + } + + private Optional generateFileProtocolUrlRequestJob(UrlRequest urlRequest, String path) { + try { + String url = new URLCodec().decode(path); + String filePath = TemplateUtils.renderParameter4Tpl(url, map); + File file = new File(URI.create(filePath).getPath()); + InputStream inputStream = IOUtils.readResource(file.getAbsolutePath()); + String mimeType = getMimeType(path); + byte[] bytes; + if (isPlainText(mimeType)) { + String text = IOUtils.inputStream2String(inputStream, EncodeConstants.ENCODING_UTF_8); + text = TemplateUtils.renderParameter4Tpl(text, map); + bytes = text.getBytes(StandardCharsets.UTF_8); + } else { + bytes = IOUtils.inputStream2Bytes(inputStream); + } + return Optional.of(generateBasicUrlRequestJob(urlRequest, mimeType, bytes)); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + return Optional.empty(); + } + + private boolean isPlainText(String mimeType) { + return ArrayUtils.contains(new String[]{"text/html", "text/javascript", "text/css"}, mimeType); + } + + UrlRequestJob generateBasicUrlRequestJob(UrlRequest urlRequest, String mimeType, byte[] bytes) { + UrlRequestJob.Options options = UrlRequestJob.Options + .newBuilder(urlRequest.id(), HttpStatus.OK) + .addHttpHeader(HttpHeader.of("Content-Type", mimeType)) + .build(); + UrlRequestJob urlRequestJob = network.newUrlRequestJob(options); + urlRequestJob.write(bytes); + urlRequestJob.complete(); + return urlRequestJob; + } + + String getMimeType(String path) { + if (StringUtils.isBlank(path)) { + return "text/html"; + } + if (path.endsWith(".html")) { + return "text/html"; + } + if (path.endsWith(".css")) { + return "text/css"; + } + if (path.endsWith(".js")) { + return "text/javascript"; + } + if (path.endsWith(".svg")) { + return "image/svg+xml"; + } + if (path.endsWith(".png")) { + return "image/png"; + } + if (path.endsWith(".jpeg")) { + return "image/jpeg"; + } + if (path.endsWith(".gif")) { + return "image/gif"; + } + Path file = new File("D:\\Fanruan\\FR10.0\\final_05_27\\FineReport_10.0\\webapps\\webroot\\upm\\lib\\locale\\plugin_zh_CN.js").toPath(); + try { + return Files.probeContentType(file); + } catch (IOException e) { + return "text/html"; + } + } +} \ No newline at end of file