Skip to content

Commit

Permalink
Enables rewriting of plain URLs 🔗
Browse files Browse the repository at this point in the history
…into PDF-compatible specialised URLs with custom schemes. That way we can support URLs such as `https://<server>/assets/…` directly by converting them silently into PDF-compatible form `resource://assets/…`, for example.

OX-10783
  • Loading branch information
jakobvogel committed Sep 4, 2024
1 parent d8183f6 commit 3806315
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import sirius.web.templates.pdf.handlers.PdfReplaceHandler;

import java.util.List;
import java.util.Optional;

/**
* Used by the XHTMLRenderer (creating PDFs) to replace img elements by their referenced image.
Expand Down Expand Up @@ -61,7 +62,7 @@ public ReplacedElement createReplacedElement(LayoutContext layoutContext,
return super.createReplacedElement(layoutContext, box, userAgentCallback, cssWidth, cssHeight);
}

String source = element.getAttribute(ATTR_SRC);
String source = rewriteLegacyUrl(element.getAttribute(ATTR_SRC));
if (Strings.isEmpty(source)) {
return super.createReplacedElement(layoutContext, box, userAgentCallback, cssWidth, cssHeight);
}
Expand All @@ -85,4 +86,15 @@ private PdfReplaceHandler findHandler(String protocol) {
"No handler for protocol '%s' could be found",
protocol)));
}

private String rewriteLegacyUrl(String url) {
for (PdfReplaceHandler handler : handlers) {
Optional<String> rewrittenUrl = handler.tryRewritePlainUrl(url);
if (rewrittenUrl.isPresent()) {
return rewrittenUrl.get();
}
}

return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;

/**
* Represents a image replace handler that is used by {@link sirius.web.templates.pdf.ImageReplacedElementFactory} to
Expand All @@ -34,6 +35,16 @@ public int getPriority() {
return Priorized.DEFAULT_PRIORITY;
}

/**
* Attempts to rewrite a plain URL to a PDF-compatible one.
*
* @param url the plain URL to rewrite
* @return an optional containing the new URL if the plain URL could be rewritten, an empty optional otherwise
*/
public Optional<String> tryRewritePlainUrl(String url) {
return Optional.empty();
}

/**
* Determines if this handler can resolve a URI with the given protocol.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.extend.UserAgentCallback;
import sirius.kernel.commons.Strings;
import sirius.kernel.di.std.ConfigValue;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.web.resources.Resource;
import sirius.web.resources.Resources;

import javax.annotation.Nullable;
import java.util.Optional;

/**
* Resolves resource:// URIs that are referencing {@link Resources} to resized images while maintaining the image
Expand All @@ -25,6 +27,9 @@
@Register
public class ResourcePdfReplaceHandler extends PdfReplaceHandler {

@ConfigValue("product.baseUrl")
private String productBaseUrl;

@Part
private Resources resources;

Expand All @@ -46,4 +51,19 @@ public FSImage resolveUri(String uri, UserAgentCallback userAgentCallback, int c

return null;
}

@Override
public Optional<String> tryRewritePlainUrl(String url) {
if (!url.startsWith(productBaseUrl)) {
return Optional.empty();
}

// remap plain asset URLs to the resource:// scheme
int indexOfAssets = url.indexOf("/assets/");
if (indexOfAssets >= 0) {
return Optional.of("resource:/" + url.substring(indexOfAssets));
}

return Optional.empty();
}
}

0 comments on commit 3806315

Please sign in to comment.