We use Apache FOP with its XMLGraphics library to generate documents for our customers based on their designed templates. Their templates often have images which are taken from a web based resource. It has become apparent however, that in some cases we are having difficulty when some older templates were designed with external images using the http protocol and the resource now sits behind the https protocol. This is because the xmlgraphics DefaultImageSessionContext does not support redirects when attempting to preload the image from the web service. This is due to it simply performing a URL.openStream() method.
What I would like to do is to create my own implementation to replace the default one. This should be possible the DefaultImageSessionContext extends a public abstract class AbstractImageSessionContext which in turn implements the ImageSessionContext.
Does anyone have any idea on how I can register my implementation with FOP?
This was my original answer:
Sorry if I give you some wrong information, it is being a long time without using FOP.
As indicated in the Apache XML Graphics Image Loading framework documentation, in order to preload an image, probably, in some part of your code, you have something like this:
If you want to support redirects, as you suggest you can provide your custom implementation of
ImageSessionContext.Reviewing the source code of
DefaultImageSessionContext, probably the best approach will be to create a new class that overrides theresolveURImethod. Please, consider for example the following code, based onHttpURLConnection, which should follow redirects as required:Then, use the new created
ImageSessionContextinstead:But I am afraid that I did not fully understand your requirements.
As you can see, for instance, in the
FOPUserAgentsource code, FOP provides its own underlyingImageSessionContextimplementation:This implementation rely on the class
InternalResourceResolver, which in turn rely on theResourceResolverabstraction provided by the XML Graphics Image Loading Framework.In order to solve your problem, you can provide your custom
ResourceResolverimplementation when initializing FOP viaFopFactoryBuilder.Although quite dated, the Apache FOP documentation provides some useful introduction here and in this and mainly this other articles. For example, based on the information provided in last link:
Please, be aware that it is a very simple example, the actual
ResourceResolver, constructed by FOP inResourceResolverFactoryis quite complex, but maybe can give you some ideas about how to implement your own:Pay attention to
NormalResourceResolverand thegetResourcemethod implementation.All the source code mentioned is related with FOP version 2.6 downloadable from here.