Handle loading of external resources documents

GroupDocs.Parser provides the functionality to handle loading of HTML external resources.

Here are the steps to handle loading of HTML external resources.

The following code sample shows how to handle loading of HTML external resources.

// Create an instance of ParserSettings to pass External Resource Handler
ParserSettings settings = new ParserSettings(new Handler());
// Create an instance of Parser class to generate spreadsheet page previews
try (Parser parser = new Parser(Constants.SampleHtmlWithImages, settings)) {
    // Extract images from HTML document
    Iterable<PageImageArea> images = parser.getImages();
    // Iterate over extracted images
    for (PageImageArea i : images) {
        // Print the type of image
        System.out.println(i.getFileType());
    }
}

/**
 * This class provides the ability to filter extracted images.
 **/
class Handler extends ExternalResourceHandler {
    // Called before any external resource loads. It allows to skip unnecessary file loading.
    @Override
    public void onLoading(ExternalResourceLoadingArgs args) {
        // Check if the file name ends with installation.png
        if (!args.getUri().endsWith("installation.png")) {
            // Otherwise skip this file
            args.setSkipped(true);
        }

        super.onLoading(args);
    }
}

ExternalResourceLoadingArgs class provides the following members to control the loading of a resource:

MemberDescription
getUri / setUriGets or sets the URI of the external resource. Set a new value to load the resource from another location.
isSkipped / setSkippedGets or sets the value that indicates whether the loading of the external resource must be skipped.
getData / setDataGets or sets the content of the external resource. Set the bytes to provide the resource content yourself instead of loading it.
Warning

Loading of external resources is enabled by default, which means that parsing a document from an untrusted source may cause outbound HTTP requests to hosts named inside that document — for example remote images or stylesheets referenced by an HTML or MHTML file or by the HTML body of an email.

ExternalResourceHandler is applied to HTML, MHTML and XHTML documents only, when the document is laid out: image extraction, page areas and the page count. Other extraction methods (for example, getFormattedText) and other formats, including email messages, do not route through it. To guarantee that no outbound request is made, block egress at the network or process level. See Network access and data privacy for details.

External resources in email messages

The HTML body of an email message can refer to external resources, for example images stored on a remote server. Whether they are downloaded depends on the method that is called:

MethodExternal images are downloaded
getTextNo: a plain text doesn’t require the external images
getFormattedTextYes: the message body is laid out as an HTML document
getStructureYes

Email messages don’t support ExternalResourceHandler yet: onLoading isn’t called and the external images are still downloaded. Hyperlinks from a message body are never followed: the parser downloads external resources like images and style sheets only.

The following example shows the calls that cause the external images of an email message to be downloaded:

// Extract a plain text; the external image isn't required to build it
try (Parser parser = new Parser(filePath)) {
    try (TextReader reader = parser.getText()) {
        reader.readToEnd();
    }
}

// Extract a formatted text; the message body is laid out as an HTML document,
// therefore the external image is downloaded
try (Parser parser = new Parser(filePath)) {
    try (TextReader reader = parser.getFormattedText(new FormattedTextOptions(FormattedTextMode.Html))) {
        reader.readToEnd();
    }
}

// Extract a structure; the external image is downloaded as well
try (Parser parser = new Parser(filePath)) {
    parser.getStructure();
}

The complete example, which also prints the requested addresses, is available as ExternalResourcesInEmailMessages in the GitHub examples.

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document parser App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.