From 8be0a4a4dc922d2c8a08728d08bb697dd35442bd Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Tue, 12 Oct 2021 14:10:49 +0000 Subject: [PATCH] FOP-2973: Cannot use custom schemes starting with "data" in resource resolver Thanks to J Frank git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1894165 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/apps/io/InternalResourceResolver.java | 2 +- .../apps/io/URIResolverWrapperTestCase.java | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/fop-core/src/main/java/org/apache/fop/apps/io/InternalResourceResolver.java b/fop-core/src/main/java/org/apache/fop/apps/io/InternalResourceResolver.java index af0a26478..a6fbfafc9 100644 --- a/fop-core/src/main/java/org/apache/fop/apps/io/InternalResourceResolver.java +++ b/fop-core/src/main/java/org/apache/fop/apps/io/InternalResourceResolver.java @@ -86,7 +86,7 @@ public class InternalResourceResolver { * @throws IOException if an I/O error occurred */ public Resource getResource(URI uri) throws IOException { - if (uri.getScheme() != null && uri.getScheme().startsWith("data")) { + if (uri.getScheme() != null && uri.getScheme().equals("data")) { return new Resource(resolveDataURI(uri.toASCIIString())); } return resourceResolver.getResource(resolveFromBase(uri)); diff --git a/fop-core/src/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java b/fop-core/src/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java index b2044e3b9..50568db1b 100644 --- a/fop-core/src/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/apps/io/URIResolverWrapperTestCase.java @@ -19,6 +19,8 @@ package org.apache.fop.apps.io; import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; @@ -35,7 +37,11 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import org.apache.commons.io.IOUtils; + import org.apache.xmlgraphics.io.ResourceResolver; +import org.apache.xmlgraphics.util.WriterOutputStream; +import org.apache.xmlgraphics.util.io.Base64EncodeStream; public class URIResolverWrapperTestCase { @@ -60,7 +66,8 @@ public class URIResolverWrapperTestCase { @Test public void testResolveIn() throws Exception { - String[] uris = new String[] {".", "resource", "path/to/resource"}; + String[] uris = new String[]{".", "resource", "path/to/resource", + "datafoo:application/octet-stream;interpreter=fop;base64,AAECAwQF"}; for (String base : BASE_URIS) { setBase(base); for (String uriStr : uris) { @@ -71,6 +78,20 @@ public class URIResolverWrapperTestCase { } } + @Test + public void testGetResourceForURIStartingWithData() throws Exception { + String uriStr = "data:application/octet-stream;interpreter=fop;base64,AAECAwQF"; + ResourceResolver resolver = mock(ResourceResolver.class); + InternalResourceResolver sut = new InternalResourceResolver(base, resolver); + URI uri = new URI(uriStr); + InputStream actual = sut.getResource(uri); + StringWriter stringWriter = new StringWriter(); + Base64EncodeStream out = new Base64EncodeStream( + new WriterOutputStream(stringWriter, "US-ASCII"), false); + IOUtils.copy(actual, out); + assertEquals("AAECAwQF", stringWriter.toString()); + } + @Test public void testResolveInBadUri() throws Exception { String[] uris = new String[] {"path\\to\\resource", "bad resource name"}; -- 2.39.5