|
|
|
@ -2,6 +2,8 @@ package com.fr.design.ui;
|
|
|
|
|
|
|
|
|
|
import com.fr.base.TemplateUtils; |
|
|
|
|
import com.fr.general.IOUtils; |
|
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
|
import com.fr.stable.EncodeConstants; |
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
|
import com.fr.third.org.apache.commons.codec.net.URLCodec; |
|
|
|
|
import com.fr.third.org.apache.commons.io.FileUtils; |
|
|
|
@ -15,11 +17,14 @@ import com.teamdev.jxbrowser.chromium.ProtocolHandler;
|
|
|
|
|
import com.teamdev.jxbrowser.chromium.URLRequest; |
|
|
|
|
import com.teamdev.jxbrowser.chromium.URLResponse; |
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.io.StringReader; |
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -51,21 +56,20 @@ public class EmbProtocolHandler implements ProtocolHandler {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public URLResponse onRequest(URLRequest req) { |
|
|
|
|
InputStream inputStream = null; |
|
|
|
|
try { |
|
|
|
|
String path = req.getURL(); |
|
|
|
|
if (path.startsWith("file:")) { |
|
|
|
|
String url = new URLCodec().decode(path); |
|
|
|
|
String filePath = TemplateUtils.renderParameter4Tpl(url, map); |
|
|
|
|
File file = new File(URI.create(filePath).getPath()); |
|
|
|
|
InputStream inputStream = new FileInputStream(file); |
|
|
|
|
if (path.endsWith(".svg")) { |
|
|
|
|
System.out.println(path); |
|
|
|
|
} |
|
|
|
|
return Assistant.inputStream2Response(inputStream, "file:///" + file.getAbsolutePath()); |
|
|
|
|
} |
|
|
|
|
else if (path.startsWith("emb:dynamic")) { |
|
|
|
|
inputStream = IOUtils.readResource(file.getAbsolutePath()); |
|
|
|
|
String text = IOUtils.inputStream2String(inputStream, EncodeConstants.ENCODING_UTF_8); |
|
|
|
|
text = TemplateUtils.renderParameter4Tpl(text, map); |
|
|
|
|
return Assistant.inputStream2Response(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)), path); |
|
|
|
|
} else if (path.startsWith("emb:dynamic")) { |
|
|
|
|
URLResponse response = new URLResponse(); |
|
|
|
|
response.setData(htmlText().getBytes()); |
|
|
|
|
response.setData(htmlText(map).getBytes()); |
|
|
|
|
response.getHeaders().setHeader("Content-Type", "text/html"); |
|
|
|
|
return response; |
|
|
|
|
} else { |
|
|
|
@ -75,16 +79,24 @@ public class EmbProtocolHandler implements ProtocolHandler {
|
|
|
|
|
} else { |
|
|
|
|
path = path.substring(4); |
|
|
|
|
} |
|
|
|
|
InputStream inputStream = IOUtils.readResource(path); |
|
|
|
|
inputStream = IOUtils.readResource(path); |
|
|
|
|
return Assistant.inputStream2Response(inputStream, path); |
|
|
|
|
} |
|
|
|
|
} catch (Exception ignore) { |
|
|
|
|
ignore.printStackTrace(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
FineLoggerFactory.getLogger().info(e.getMessage()); |
|
|
|
|
} finally { |
|
|
|
|
if (inputStream != null) { |
|
|
|
|
try { |
|
|
|
|
inputStream.close(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String htmlText() { |
|
|
|
|
private String htmlText(Map<String, String> map) { |
|
|
|
|
PathGroup pathGroup = AtomBuilder.create().buildAssembleFilePath(ModernRequestClient.KEY, component); |
|
|
|
|
StylePath[] stylePaths = pathGroup.toStylePathGroup(); |
|
|
|
|
StringBuilder styleText = new StringBuilder(); |
|
|
|
@ -106,6 +118,13 @@ public class EmbProtocolHandler implements ProtocolHandler {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
result = result.replaceAll("##script##", scriptText.toString()); |
|
|
|
|
if (map != null) { |
|
|
|
|
for (Map.Entry<String, String> entry : map.entrySet()) { |
|
|
|
|
String key = entry.getKey(); |
|
|
|
|
String value = entry.getValue(); |
|
|
|
|
result = result.replaceAll("\\$\\{" + key + "}", value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|