forked from fanruan/design
Browse Source
Merge in DESIGN/design from ~HADES/design:feature/10.0 to feature/10.0 * commit '9dc3b6b87709b24fd9c0e8e0f485f05a3574a1d8': KERNEL-7634 mac和win使用不同版本的jxbrowserfeature/10.0
Hades
3 years ago
3 changed files with 245 additions and 3 deletions
@ -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<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.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<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("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"; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue