danfickle / danfickle/openhtmltopdf

IllegalArgumentException for invalid URIs

Open
#790 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.2k
Forks
423
PR merge metrics
No merged PRs in 30d

Description

PDFRenderer.createPDF:
If an image is embedded with an invalid URI (e.g. http:///static/images/footer/wikimedia-button.png) an uncaught IllegalArgumentException is thrown in com.openthmltopdf.swing.NaiveUserAgent$DefaultHttpStreamFactory#getUrl when conn.connect is called. The PDF creation is stopped

I tried using an own HttpStreamFactory and returning null in this case, but the calling method (NaiveUserAgent#openStream) is not nullsafe.

As a workaround, I made an own UriResolver (extending DefaultUriResolver), added the extractProtocol Method (which is normally called in openStream) and made an Override of resolveURI so that null is returned in this error case:

```

@Override
public String resolveURI(String baseUri, String uri)
{
String resolvedUri= super.resolveURI(baseUri, uri);

try
{
String protocol = extractProtocol(resolvedUri);

if(resolvedUri != null && "http".equals(protocol))
{
URLConnection conn = new URL(resolvedUri).openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(30000);
conn.connect();
}
}
catch(IllegalArgumentException e)
{
resolvedUri= null;
}

return resolvedUri;
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.