Added "qdnContext" variable, with possible values of "render", "gateway", or "domainMap".

This is used internally to allow Q-Apps to determine how to handle certain requests.
This commit is contained in:
CalDescent 2023-01-28 14:42:29 +00:00
parent 5a1cc7a0de
commit 3b6e1ea27f
5 changed files with 14 additions and 6 deletions

View File

@ -12,11 +12,13 @@ public class HTMLParser {
private String linkPrefix;
private byte[] data;
private String qdnContext;
public HTMLParser(String resourceId, String inPath, String prefix, boolean usePrefix, byte[] data) {
public HTMLParser(String resourceId, String inPath, String prefix, boolean usePrefix, byte[] data, String qdnContext) {
String inPathWithoutFilename = inPath.substring(0, inPath.lastIndexOf('/'));
this.linkPrefix = usePrefix ? String.format("%s/%s%s", prefix, resourceId, inPathWithoutFilename) : "";
this.data = data;
this.qdnContext = qdnContext;
}
public void addAdditionalHeaderTags() {
@ -29,6 +31,10 @@ public class HTMLParser {
String qAppsScriptElement = String.format("<script src=\"/apps/q-apps.js?time=%d\">", System.currentTimeMillis());
head.get(0).prepend(qAppsScriptElement);
// Add QDN context var
String qdnContextVar = String.format("<script>var qdnContext=\"%s\";</script>", this.qdnContext);
head.get(0).prepend(qdnContextVar);
// Add base href tag
String baseElement = String.format("<base href=\"%s\">", baseUrl);
head.get(0).prepend(baseElement);

View File

@ -51,7 +51,7 @@ public class DomainMapResource {
String secret58, String prefix, boolean usePrefix, boolean async) {
ArbitraryDataRenderer renderer = new ArbitraryDataRenderer(resourceId, resourceIdType, service, inPath,
secret58, prefix, usePrefix, async, request, response, context);
secret58, prefix, usePrefix, async, "domainMap", request, response, context);
return renderer.render();
}

View File

@ -119,7 +119,7 @@ public class GatewayResource {
String secret58, String prefix, boolean usePrefix, boolean async) {
ArbitraryDataRenderer renderer = new ArbitraryDataRenderer(resourceId, resourceIdType, service, inPath,
secret58, prefix, usePrefix, async, request, response, context);
secret58, prefix, usePrefix, async, "gateway", request, response, context);
return renderer.render();
}

View File

@ -216,7 +216,7 @@ public class RenderResource {
String secret58, String prefix, boolean usePrefix, boolean async, String theme) {
ArbitraryDataRenderer renderer = new ArbitraryDataRenderer(resourceId, resourceIdType, service, inPath,
secret58, prefix, usePrefix, async, request, response, context);
secret58, prefix, usePrefix, async, "render", request, response, context);
if (theme != null) {
renderer.setTheme(theme);

View File

@ -40,12 +40,13 @@ public class ArbitraryDataRenderer {
private final String prefix;
private final boolean usePrefix;
private final boolean async;
private final String qdnContext;
private final HttpServletRequest request;
private final HttpServletResponse response;
private final ServletContext context;
public ArbitraryDataRenderer(String resourceId, ResourceIdType resourceIdType, Service service, String inPath,
String secret58, String prefix, boolean usePrefix, boolean async,
String secret58, String prefix, boolean usePrefix, boolean async, String qdnContext,
HttpServletRequest request, HttpServletResponse response, ServletContext context) {
this.resourceId = resourceId;
@ -56,6 +57,7 @@ public class ArbitraryDataRenderer {
this.prefix = prefix;
this.usePrefix = usePrefix;
this.async = async;
this.qdnContext = qdnContext;
this.request = request;
this.response = response;
this.context = context;
@ -118,7 +120,7 @@ public class ArbitraryDataRenderer {
if (HTMLParser.isHtmlFile(filename)) {
// HTML file - needs to be parsed
byte[] data = Files.readAllBytes(Paths.get(filePath)); // TODO: limit file size that can be read into memory
HTMLParser htmlParser = new HTMLParser(resourceId, inPath, prefix, usePrefix, data);
HTMLParser htmlParser = new HTMLParser(resourceId, inPath, prefix, usePrefix, data, qdnContext);
htmlParser.addAdditionalHeaderTags();
response.addHeader("Content-Security-Policy", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; media-src 'self' blob:; img-src 'self' data: blob:;");
response.setContentType(context.getMimeType(filename));