Skip to content

Commit

Permalink
Merge pull request #1456 from scireum/feature/jvo/OX-10783-Automagic-…
Browse files Browse the repository at this point in the history
…URL-Fixing

PDF Rendering: Automagic Rewriting of Plain URLs
  • Loading branch information
jakobvogel authored Sep 5, 2024
2 parents 42be846 + a155ee0 commit 8d4c1ce
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<description>Provides a modern and scalable web server as SIRIUS module</description>

<properties>
<sirius.kernel>dev-43.0.0</sirius.kernel>
<sirius.kernel>dev-43.1.0</sirius.kernel>
</properties>

<repositories>
Expand Down
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 @@ -51,27 +52,27 @@ public ReplacedElement createReplacedElement(LayoutContext layoutContext,
UserAgentCallback userAgentCallback,
int cssWidth,
int cssHeight) {
Element e = box.getElement();
if (e == null) {
Element element = box.getElement();
if (element == null) {
return null;
}

String nodeName = e.getNodeName();
String nodeName = element.getNodeName();
if (!TAG_TYPE_IMG.equals(nodeName)) {
return super.createReplacedElement(layoutContext, box, userAgentCallback, cssWidth, cssHeight);
}

String src = e.getAttribute(ATTR_SRC);
if (Strings.isEmpty(src)) {
String source = rewriteLegacyUrl(element.getAttribute(ATTR_SRC));
if (Strings.isEmpty(source)) {
return super.createReplacedElement(layoutContext, box, userAgentCallback, cssWidth, cssHeight);
}

try {
String protocol = Strings.split(src, "://").getFirst();
String protocol = Strings.split(source, "://").getFirst();
PdfReplaceHandler handler = findHandler(protocol);
return new AsyncLoadedImageElement(handler, userAgentCallback, src, cssWidth, cssHeight);
} catch (Exception ex) {
Exceptions.handle(ex);
return new AsyncLoadedImageElement(handler, userAgentCallback, source, cssWidth, cssHeight);
} catch (Exception exception) {
Exceptions.handle(exception);
}

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 Expand Up @@ -119,7 +130,10 @@ private Tuple<Integer, Integer> computeResizeBox(int cssWidth, int cssHeight, FS
}

@Nonnull
private static Tuple<Integer, Integer> downscaleResource(int cssWidth, int cssHeight, int imageWidth, int imageHeight) {
private static Tuple<Integer, Integer> downscaleResource(int cssWidth,
int cssHeight,
int imageWidth,
int imageHeight) {

// First, check if we need to scale down the width
if (imageWidth > cssWidth) {
Expand All @@ -138,7 +152,10 @@ private static Tuple<Integer, Integer> downscaleResource(int cssWidth, int cssHe
}

@Nonnull
private static Tuple<Integer, Integer> upscaleResource(int cssWidth, int cssHeight, int imageWidth, int imageHeight) {
private static Tuple<Integer, Integer> upscaleResource(int cssWidth,
int cssHeight,
int imageWidth,
int imageHeight) {

// First, scale up only the width
imageHeight = cssWidth * imageHeight / imageWidth;
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 8d4c1ce

Please sign in to comment.