forked from fanruan/design
richie
5 years ago
13 changed files with 471 additions and 392 deletions
@ -1,68 +0,0 @@
|
||||
package com.fr.design.ui; |
||||
|
||||
import com.fr.stable.StringUtils; |
||||
import com.teamdev.jxbrowser.chromium.Browser; |
||||
import com.teamdev.jxbrowser.chromium.BrowserContext; |
||||
import com.teamdev.jxbrowser.chromium.ProtocolService; |
||||
import com.teamdev.jxbrowser.chromium.URLResponse; |
||||
|
||||
import javax.activation.MimetypesFileTypeMap; |
||||
import java.io.DataInputStream; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.nio.file.Paths; |
||||
|
||||
/** |
||||
* @author richie |
||||
* @version 10.0 |
||||
* Created by richie on 2019-03-07 |
||||
*/ |
||||
public class Assistant { |
||||
|
||||
public static URLResponse inputStream2Response(InputStream inputStream, String filePath) throws Exception { |
||||
URLResponse response = new URLResponse(); |
||||
DataInputStream stream = new DataInputStream(inputStream); |
||||
byte[] data = new byte[stream.available()]; |
||||
stream.readFully(data); |
||||
response.setData(data); |
||||
String mimeType = getMimeType(filePath); |
||||
response.getHeaders().setHeader("Content-Type", mimeType); |
||||
return response; |
||||
} |
||||
|
||||
|
||||
private static 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"; |
||||
} |
||||
Path file = new File(path).toPath(); |
||||
try { |
||||
return Files.probeContentType(file); |
||||
} catch (IOException e) { |
||||
return "text/html"; |
||||
} |
||||
} |
||||
|
||||
public static void setEmbProtocolHandler(Browser browser, EmbProtocolHandler handler) { |
||||
BrowserContext browserContext = browser.getContext(); |
||||
ProtocolService protocolService = browserContext.getProtocolService(); |
||||
// 支持读取jar包中文件的自定义协议————emb:/com/fr/design/images/bbs.png
|
||||
protocolService.setProtocolHandler("emb", handler); |
||||
protocolService.setProtocolHandler("file", handler); |
||||
} |
||||
} |
@ -1,130 +0,0 @@
|
||||
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; |
||||
import com.fr.third.org.apache.commons.io.FilenameUtils; |
||||
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.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; |
||||
|
||||
/** |
||||
* @author richie |
||||
* @version 10.0 |
||||
* Created by richie on 2019-03-07 |
||||
*/ |
||||
public class EmbProtocolHandler implements ProtocolHandler { |
||||
|
||||
private AssembleComponent component; |
||||
private Map<String, String> map; |
||||
|
||||
public EmbProtocolHandler() { |
||||
|
||||
} |
||||
|
||||
public EmbProtocolHandler(AssembleComponent component) { |
||||
this.component = component; |
||||
} |
||||
|
||||
public EmbProtocolHandler(AssembleComponent component, Map<String, String> map) { |
||||
this.component = component; |
||||
this.map = map; |
||||
} |
||||
|
||||
public EmbProtocolHandler(Map<String, String> map) { |
||||
this.map = map; |
||||
} |
||||
|
||||
@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 = 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(map).getBytes()); |
||||
response.getHeaders().setHeader("Content-Type", "text/html"); |
||||
return response; |
||||
} else { |
||||
int index = path.indexOf("="); |
||||
if (index > 0) { |
||||
path = path.substring(index + 1); |
||||
} else { |
||||
path = path.substring(4); |
||||
} |
||||
inputStream = IOUtils.readResource(path); |
||||
return Assistant.inputStream2Response(inputStream, path); |
||||
} |
||||
} 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(Map<String, String> 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("<link rel=\"stylesheet\" href=\"emb:"); |
||||
styleText.append(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("<script src=\"emb:"); |
||||
scriptText.append(path.toFilePath()); |
||||
scriptText.append("\"></script>"); |
||||
} |
||||
} |
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,85 @@
|
||||
package com.fr.design.ui; |
||||
|
||||
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<String, String> 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<String, String> 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("<link rel=\"stylesheet\" href=\"emb:"); |
||||
styleText.append(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("<script src=\"emb:"); |
||||
scriptText.append(path.toFilePath()); |
||||
scriptText.append("\"></script>"); |
||||
} |
||||
} |
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,134 @@
|
||||
package com.fr.design.ui; |
||||
|
||||
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<String, String> map; |
||||
|
||||
public NxInterceptRequestCallback(Network network) { |
||||
this.network = network; |
||||
} |
||||
|
||||
public NxInterceptRequestCallback(Network network, Map<String, String> 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<UrlRequestJob> 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<UrlRequestJob> 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(path).toPath(); |
||||
try { |
||||
return Files.probeContentType(file); |
||||
} catch (IOException e) { |
||||
return "text/html"; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue