Skip to content

Commit 788b72c

Browse files
committed
Fix processing base url as file url. Major refactoring. Add new tests.
DEVSIX-1668
1 parent 6a3781f commit 788b72c

36 files changed

+443
-56
lines changed

src/main/java/com/itextpdf/html2pdf/HtmlConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ This file is part of the iText (R) project.
6464
import java.io.IOException;
6565
import java.io.InputStream;
6666
import java.io.OutputStream;
67+
import java.nio.file.Paths;
6768
import java.util.List;
6869

6970
/**
@@ -170,9 +171,9 @@ public static void convertToPdf(File htmlFile, File pdfFile) throws IOException
170171
*/
171172
public static void convertToPdf(File htmlFile, File pdfFile, ConverterProperties converterProperties) throws IOException {
172173
if (converterProperties == null) {
173-
converterProperties = new ConverterProperties().setBaseUri(FileUtil.getParentDirectory(htmlFile.getAbsolutePath()) + File.separator);
174+
converterProperties = new ConverterProperties().setBaseUri(Paths.get(FileUtil.getParentDirectory(htmlFile.getAbsolutePath())).toUri().toURL().toExternalForm() + File.separator);
174175
} else if (converterProperties.getBaseUri() == null) {
175-
converterProperties = new ConverterProperties(converterProperties).setBaseUri(FileUtil.getParentDirectory(htmlFile.getAbsolutePath()) + File.separator);
176+
converterProperties = new ConverterProperties(converterProperties).setBaseUri(Paths.get(FileUtil.getParentDirectory(htmlFile.getAbsolutePath())).toUri().toURL().toExternalForm() + File.separator);
176177
}
177178
try (FileInputStream fileInputStream = new FileInputStream(htmlFile.getAbsolutePath());
178179
FileOutputStream fileOutputStream = new FileOutputStream(pdfFile.getAbsolutePath())) {

src/main/java/com/itextpdf/html2pdf/resolver/resource/UriResolver.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,24 @@ This file is part of the iText (R) project.
4646

4747
import java.net.MalformedURLException;
4848
import java.net.URI;
49-
import java.net.URISyntaxException;
5049
import java.net.URL;
50+
import java.nio.file.Files;
5151
import java.nio.file.Path;
5252
import java.nio.file.Paths;
53-
import java.util.regex.Matcher;
54-
import java.util.regex.Pattern;
5553

5654
/**
5755
* Utilities class to resolve URIs.
5856
*/
5957
public class UriResolver {
6058

61-
/** The base url. */
59+
/**
60+
* The base url.
61+
*/
6262
private URL baseUrl;
6363

64-
/** Indicates if the Uri refers to a local resource. */
64+
/**
65+
* Indicates if the Uri refers to a local resource.
66+
*/
6567
private boolean isLocalBaseUri;
6668

6769
/**
@@ -93,7 +95,6 @@ public URL resolveAgainstBaseUri(String uriString) throws MalformedURLException
9395
URL resolvedUrl = null;
9496
uriString = uriString.trim();
9597
// decode and then encode uri string in order to process unsafe characters correctly
96-
String scheme = getUriStringScheme(uriString);
9798
uriString = UriEncodeUtil.encode(uriString);
9899
if (isLocalBaseUri) {
99100
// remove leading slashes in order to always concatenate such resource URIs: we don't want to scatter all
@@ -119,16 +120,23 @@ public URL resolveAgainstBaseUri(String uriString) throws MalformedURLException
119120
return resolvedUrl;
120121
}
121122

123+
/**
124+
* Check if baseURI is local
125+
*
126+
* @return true if baseURI is local, otherwise false
127+
*/
128+
public boolean isLocalBaseUri() {
129+
return isLocalBaseUri;
130+
}
131+
122132
/**
123133
* Resolves the base URI to an URL or path.
124134
*
125135
* @param base the base URI
126136
*/
127137
private void resolveBaseUrlOrPath(String base) {
128138
base = base.trim();
129-
String scheme = getUriStringScheme(base);
130-
base = UriEncodeUtil.encode(base);
131-
baseUrl = baseUriAsUrl(base);
139+
baseUrl = baseUriAsUrl(UriEncodeUtil.encode(base));
132140
if (baseUrl == null) {
133141
baseUrl = uriAsFileUrl(base);
134142
}
@@ -171,37 +179,32 @@ private URL uriAsFileUrl(String baseUriString) {
171179
URL baseAsFileUrl = null;
172180
try {
173181
Path path = Paths.get(baseUriString);
174-
baseAsFileUrl = path.toAbsolutePath().normalize().toUri().toURL();
182+
if (isPathRooted(path, baseUriString)) {
183+
String str = "file:///" + encode(path, path.toAbsolutePath().normalize().toString());
184+
baseAsFileUrl = new URI(str).toURL();
185+
} else {
186+
String str = encode(path, baseUriString);
187+
URL base = Paths.get("").toUri().toURL();
188+
baseAsFileUrl = new URL(base, str);
189+
}
175190
isLocalBaseUri = true;
176191
} catch (Exception ignored) {
177192
}
178193

179194
return baseAsFileUrl;
180195
}
181196

182-
/**
183-
* Get the scheme component of this URI.
184-
*/
185-
private String getUriStringScheme(String uriString) {
186-
String result = null;
187-
Matcher matcher = Pattern.compile("^[a-zA-Z]([a-zA-Z]|\\d|\\+|-|\\.)*:").matcher(uriString);
188-
if (matcher.find()) {
189-
result = matcher.group().substring(0, matcher.group().indexOf(':'));
190-
} else if (null != baseUrl) {
191-
try {
192-
result = baseUrl.toURI().getScheme();
193-
} catch (URISyntaxException ignored) {
194-
}
197+
private String encode(Path path, String str) {
198+
str = str.replace("\\", "/");
199+
str = UriEncodeUtil.encode(str);
200+
if (Files.isDirectory(path) && !str.endsWith("/")) {
201+
str += "/";
195202
}
196-
return result;
203+
str = str.replaceFirst("/*\\\\*", "");
204+
return str;
197205
}
198206

199-
/**
200-
* Check if baseURI is local
201-
*
202-
* @return true if baseURI is local, otherwise false
203-
*/
204-
public boolean isLocalBaseUri() {
205-
return isLocalBaseUri;
207+
private boolean isPathRooted(Path path, String str) {
208+
return path.isAbsolute() || str.startsWith("/");
206209
}
207210
}

src/test/java/com/itextpdf/html2pdf/ResourceResolverTest.java

Lines changed: 165 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.test.annotations.type.IntegrationTest;
5050
import org.junit.Assert;
5151
import org.junit.BeforeClass;
52-
import org.junit.Ignore;
5352
import org.junit.Test;
5453
import org.junit.experimental.categories.Category;
5554

5655
import java.io.File;
5756
import java.io.FileInputStream;
5857
import java.io.FileOutputStream;
5958
import java.io.IOException;
60-
import java.nio.file.Paths;
6159

6260
@Category(IntegrationTest.class)
6361
public class ResourceResolverTest extends ExtendedITextTest {
@@ -89,6 +87,38 @@ public void resourceResolverTest07() throws IOException, InterruptedException {
8987
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07_"));
9088
}
9189

90+
@Test
91+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
92+
public void resourceResolverTest07A() throws IOException, InterruptedException {
93+
String baseUri = sourceFolder + "%23r%e%2525s@o%25urces/";
94+
95+
String outPdf = destinationFolder + "resourceResolverTest07A.pdf";
96+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest07A.pdf";
97+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest07A.html");
98+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
99+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
100+
}
101+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07A_"));
102+
}
103+
104+
@Test
105+
public void resourceResolverTest07B() throws IOException, InterruptedException {
106+
String outPdf = destinationFolder + "resourceResolverTest07B.pdf";
107+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest07B.pdf";
108+
HtmlConverter.convertToPdf(new File(sourceFolder + "#r%e%25s@o%urces/resourceResolverTest07B.html"), new File(outPdf));
109+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07B_"));
110+
}
111+
112+
@Test
113+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
114+
public void resourceResolverTest07C() throws IOException, InterruptedException {
115+
String outPdf = destinationFolder + "resourceResolverTest07C.pdf";
116+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest07C.pdf";
117+
HtmlConverter.convertToPdf(new File(sourceFolder + "#r%e%25s@o%urces/resourceResolverTest07C.html"), new File(outPdf),
118+
new ConverterProperties().setBaseUri(sourceFolder + "#r%e%25s@o%urces/.."));
119+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07C_"));
120+
}
121+
92122
@Test
93123
public void resourceResolverTest09() throws IOException, InterruptedException {
94124
String outPdf = destinationFolder + "resourceResolverTest09.pdf";
@@ -120,44 +150,101 @@ public void resourceResolverTest11() throws IOException, InterruptedException {
120150
}
121151

122152
@Test
123-
@Ignore("DEVSIX-1668")
124-
public void resourceResolverTest12() throws IOException, InterruptedException {
125-
String baseUri = sourceFolder + "path with spaces";
153+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
154+
public void resourceResolverTest12A() throws IOException, InterruptedException {
155+
String baseUri = sourceFolder + "path%with%spaces/";
126156

127-
String outPdf = destinationFolder + "resourceResolverTest12.pdf";
128-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12.pdf";
129-
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12.html");
157+
String outPdf = destinationFolder + "resourceResolverTest12A.pdf";
158+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12A.pdf";
159+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12A.html");
130160
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
131161
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
132162
}
133-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12_"));
163+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12A_"));
134164
}
135165

136166
@Test
137-
public void resourceResolverTest13() throws IOException, InterruptedException {
138-
String baseUri = sourceFolder;
167+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
168+
public void resourceResolverTest12B() throws IOException, InterruptedException {
169+
String baseUri = sourceFolder + "path%25with%25spaces/";
139170

140-
String outPdf = destinationFolder + "resourceResolverTest13.pdf";
141-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest13.pdf";
142-
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest13.html");
171+
String outPdf = destinationFolder + "resourceResolverTest12B.pdf";
172+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12B.pdf";
173+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12B.html");
143174
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
144175
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
145176
}
146-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff13_"));
177+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12B_"));
178+
}
179+
180+
@Test
181+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
182+
public void resourceResolverTest12C() throws IOException, InterruptedException {
183+
String baseUri = sourceFolder + "path%2525with%2525spaces/";
184+
185+
String outPdf = destinationFolder + "resourceResolverTest12C.pdf";
186+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12C.pdf";
187+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12C.html");
188+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
189+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
190+
}
191+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12C_"));
192+
}
193+
194+
195+
@Test
196+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
197+
public void resourceResolverTest12D() throws IOException, InterruptedException {
198+
String baseUri = sourceFolder + "path with spaces/";
199+
200+
String outPdf = destinationFolder + "resourceResolverTest12D.pdf";
201+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12D.pdf";
202+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12D.html");
203+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
204+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
205+
}
206+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12D_"));
207+
}
208+
209+
@Test
210+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
211+
public void resourceResolverTest12E() throws IOException, InterruptedException {
212+
String baseUri = sourceFolder + "path%20with%20spaces/";
213+
214+
String outPdf = destinationFolder + "resourceResolverTest12E.pdf";
215+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12E.pdf";
216+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12E.html");
217+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
218+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
219+
}
220+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12E_"));
147221
}
148222

149223
@Test
150-
@Ignore("DEVSIX-1668")
151-
public void resourceResolverTest14() throws IOException, InterruptedException {
152-
String baseUri = sourceFolder + "path%20with%20spaces";
224+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
225+
public void resourceResolverTest12F() throws IOException, InterruptedException {
226+
String baseUri = sourceFolder + "path%2520with%2520spaces/";
153227

154-
String outPdf = destinationFolder + "resourceResolverTest14.pdf";
155-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest14.pdf";
156-
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12.html");
228+
String outPdf = destinationFolder + "resourceResolverTest12F.pdf";
229+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12F.pdf";
230+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12F.html");
157231
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
158232
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
159233
}
160-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff14_"));
234+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12F_"));
235+
}
236+
237+
@Test
238+
public void resourceResolverTest13() throws IOException, InterruptedException {
239+
String baseUri = sourceFolder;
240+
241+
String outPdf = destinationFolder + "resourceResolverTest13.pdf";
242+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest13.pdf";
243+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest13.html");
244+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
245+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
246+
}
247+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff13_"));
161248
}
162249

163250
@Test
@@ -173,6 +260,62 @@ public void resourceResolverTest15() throws IOException, InterruptedException {
173260
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff15_"));
174261
}
175262

263+
@Test
264+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
265+
public void resourceResolverTest16A() throws IOException, InterruptedException {
266+
String baseUri = sourceFolder + "path/with/spaces/";
267+
268+
String outPdf = destinationFolder + "resourceResolverTest16A.pdf";
269+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16A.pdf";
270+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16A.html");
271+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
272+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
273+
}
274+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16A_"));
275+
}
276+
277+
@Test
278+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
279+
public void resourceResolverTest16B() throws IOException, InterruptedException {
280+
String baseUri = sourceFolder + "path%2Fwith%2Fspaces/";
281+
282+
String outPdf = destinationFolder + "resourceResolverTest16B.pdf";
283+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16B.pdf";
284+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16B.html");
285+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
286+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
287+
}
288+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16B_"));
289+
}
290+
291+
@Test
292+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
293+
public void resourceResolverTest16C() throws IOException, InterruptedException {
294+
String baseUri = sourceFolder + "path%252Fwith%252Fspaces/";
295+
296+
String outPdf = destinationFolder + "resourceResolverTest16C.pdf";
297+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16C.pdf";
298+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16C.html");
299+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
300+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
301+
}
302+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16C_"));
303+
}
304+
305+
@Test
306+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
307+
public void resourceResolverTest16D() throws IOException, InterruptedException {
308+
String baseUri = sourceFolder + "path%25252Fwith%25252Fspaces/";
309+
310+
String outPdf = destinationFolder + "resourceResolverTest16D.pdf";
311+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16D.pdf";
312+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16D.html");
313+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
314+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
315+
}
316+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16D_"));
317+
}
318+
176319
// TODO test with absolute http links for resources?
177320
// TODO test with http base URI?
178321
}

0 commit comments

Comments
 (0)