Class ResourceResolver


  • public class ResourceResolver
    extends java.lang.Object
    Utilities class to resolve resources.
    • Field Detail

      • BASE64_IDENTIFIER

        public static final java.lang.String BASE64_IDENTIFIER
        Identifier string used when loading in base64 images.
        See Also:
        Constant Field Values
      • DATA_SCHEMA_PREFIX

        public static final java.lang.String DATA_SCHEMA_PREFIX
        Identifier string used to detect that the source is under data URI scheme.
        See Also:
        Constant Field Values
      • logger

        private static final org.slf4j.Logger logger
    • Constructor Detail

      • ResourceResolver

        public ResourceResolver​(java.lang.String baseUri)
        Creates a new ResourceResolver instance. If baseUri is a string that represents an absolute URI with any schema except "file" - resources url values will be resolved exactly as "new URL(baseUrl, uriString)". Otherwise base URI will be handled as path in local file system.

        If empty string or relative URI string is passed as base URI, then it will be resolved against current working directory of this application instance.

        Parameters:
        baseUri - base URI against which all relative resource URIs will be resolved
      • ResourceResolver

        public ResourceResolver​(java.lang.String baseUri,
                                IResourceRetriever retriever)
        Creates a new ResourceResolver instance. If baseUri is a string that represents an absolute URI with any schema except "file" - resources url values will be resolved exactly as "new URL(baseUrl, uriString)". Otherwise base URI will be handled as path in local file system.

        If empty string or relative URI string is passed as base URI, then it will be resolved against current working directory of this application instance.

        Parameters:
        baseUri - base URI against which all relative resource URIs will be resolved
        retriever - the resource retriever with the help of which data from resources will be retrieved
    • Method Detail

      • getRetriever

        public IResourceRetriever getRetriever()
        Gets the resource retriever. The retriever is used to retrieve data from resources by URL.
        Returns:
        the resource retriever
      • setRetriever

        public ResourceResolver setRetriever​(IResourceRetriever retriever)
        Sets the resource retriever. The retriever is used to retrieve data from resources by URL.
        Parameters:
        retriever - the resource retriever
        Returns:
        the ResourceResolver instance
      • retrieveImage

        public PdfXObject retrieveImage​(java.lang.String src)
        Retrieve image as either PdfImageXObject, or PdfFormXObject.
        Parameters:
        src - either link to file or base64 encoded stream
        Returns:
        PdfXObject on success, otherwise null
      • retrieveBytesFromResource

        public byte[] retrieveBytesFromResource​(java.lang.String src)
        Retrieve a resource as a byte array from a source that can either be a link to a file, or a base64 encoded String.
        Parameters:
        src - either link to file or base64 encoded stream
        Returns:
        byte[] on success, otherwise null
      • retrieveResourceAsInputStream

        public java.io.InputStream retrieveResourceAsInputStream​(java.lang.String src)
        Retrieve the resource found in src as an InputStream
        Parameters:
        src - path to the resource
        Returns:
        InputStream for the resource on success, otherwise null
      • isDataSrc

        public static boolean isDataSrc​(java.lang.String src)
        Checks if source is under data URI scheme. (eg data:[<media type>][;base64],<data>).
        Parameters:
        src - string to test
        Returns:
        true if source is under data URI scheme
      • resolveAgainstBaseUri

        public java.net.URL resolveAgainstBaseUri​(java.lang.String uri)
                                           throws java.net.MalformedURLException
        Resolves a given URI against the base URI.
        Parameters:
        uri - the uri
        Returns:
        the url
        Throws:
        java.net.MalformedURLException - the malformed URL exception
      • getBaseUri

        public java.lang.String getBaseUri()
        Gets the base URI.
        Returns:
        the base uri
      • resetCache

        public void resetCache()
        Resets the simple image cache.
      • tryResolveBase64ImageSource

        protected PdfXObject tryResolveBase64ImageSource​(java.lang.String src)
        Creates PdfXObject based on passed base64 encoded string.
        Parameters:
        src - the base64 encoded string
        Returns:
        the PdfXObject based on passed base64 encoded string or null
      • tryResolveUrlImageSource

        protected PdfXObject tryResolveUrlImageSource​(java.lang.String uri)
        Creates PdfXObject based on the string which defines the path to local image.
        Parameters:
        uri - the path to the local image
        Returns:
        the PdfXObject based on local image or null
      • createImageByUrl

        protected PdfXObject createImageByUrl​(java.net.URL url)
                                       throws java.lang.Exception
        Create a iText XObject based on the image stored at the passed location.
        Parameters:
        url - location of the Image file.
        Returns:
        PdfXObject containing the Image loaded in.
        Throws:
        java.lang.Exception - thrown if error occurred during fetching or constructing the image.
      • retrieveBytesFromBase64Src

        private byte[] retrieveBytesFromBase64Src​(java.lang.String src)
      • isContains64Mark

        private boolean isContains64Mark​(java.lang.String src)
        Checks if string contains base64 mark. It does not guarantee that src is a correct base64 data-string.
        Parameters:
        src - string to test
        Returns:
        true if string contains base64 mark