ResourceResolver { public byte[] retrieveBytesFromResource(String src) { byte[] bytes = retrieveBytesFromBase64Src(src); if (bytes != null) { return bytes; } try { URL url = uriResolver.resolveAgainstBaseUri(src); return retriever.getByteArrayByUrl(url); } catch (Exception e) { logger.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, uriResolver.getBaseUri(), src), e); return null; } } ResourceResolver(String baseUri);  ResourceResolver(String baseUri, IResourceRetriever retriever); IResourceRetriever getRetriever(); ResourceResolver setRetriever(IResourceRetriever retriever); @Deprecated PdfImageXObject retrieveImage(String src); PdfXObject retrieveImageExtended(String src); @Deprecated InputStream retrieveStyleSheet(String uri); @Deprecated byte[] retrieveStream(String src); byte[] retrieveBytesFromResource(String src); InputStream retrieveResourceAsInputStream(String src); boolean isDataSrc(String src); URL resolveAgainstBaseUri(String uri); void resetCache(); @Deprecated boolean isImageTypeSupportedByImageDataFactory(String src); static final String BASE64IDENTIFIER; static final String DATA_SCHEMA_PREFIX; }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, logLevel = LogLevelConstants.ERROR)) public void retrieveBytesFromResourceByMalformedResourceNameTest() { String fileName = "retrieveStyl eSheetTest.css"; ResourceResolver resourceResolver = new ResourceResolver(baseUri); byte[] bytes = resourceResolver.retrieveBytesFromResource(fileName); Assert.assertNull(bytes); }
@Test public void retrieveBytesFromResourceBase64Test() { ResourceResolver resourceResolver = new ResourceResolver(baseUri); byte[] bytes = resourceResolver.retrieveBytesFromResource(bLogo); Assert.assertNotNull(bytes); }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, logLevel = LogLevelConstants.ERROR)) public void retrieveBytesFromResourceIncorrectBase64Test() { ResourceResolver resourceResolver = new ResourceResolver(baseUri); byte[] bytes = resourceResolver.retrieveBytesFromResource(bLogoIncorrect); Assert.assertNull(bytes); }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, logLevel = LogLevelConstants.ERROR)) public void retrieveBytesFromResourceCorruptedDataBase64Test() { ResourceResolver resourceResolver = new ResourceResolver(baseUri); byte[] bytes = resourceResolver.retrieveBytesFromResource(bLogoCorruptedData); Assert.assertNull(bytes); }
@Test public void retrieveBytesFromResourcePngImageTest() throws IOException { String fileName = "resourceResolverTest.png"; ResourceResolver resourceResolver = new ResourceResolver(baseUri); byte[] expected = Files.readAllBytes(new File(baseUri + fileName).toPath()); byte[] bytes = resourceResolver.retrieveBytesFromResource(fileName); Assert.assertNotNull(bytes); Assert.assertEquals(expected.length, bytes.length); }
@Test public void retrieveBytesFromResourceStyleSheetTest() throws IOException { String fileName = "retrieveStyleSheetTest.css"; ResourceResolver resourceResolver = new ResourceResolver(baseUri); byte[] expected = Files.readAllBytes(new File(baseUri + fileName).toPath()); byte[] bytes = resourceResolver.retrieveBytesFromResource(fileName); Assert.assertNotNull(bytes); Assert.assertEquals(expected.length, bytes.length); }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI)) public void retrieveBytesMalformedResourceNameTest() { String fileName = "resourceResolverTest .png"; ResourceResolver resourceResolver = new ResourceResolver(baseUri); byte[] bytes =resourceResolver.retrieveBytesFromResource(fileName); Assert.assertNull(bytes); }
@Test public void retrieveBytesFromResourceWithRetryRetrieverTest() throws IOException { String fileName = "!invalid! StyleSheetName.css"; ResourceResolver resourceResolver = new ResourceResolver(baseUri, new RetryResourceRetriever(baseUri)); byte[] expected = Files.readAllBytes(new File(baseUri + "retrieveStyleSheetTest.css").toPath()); byte[] bytes = resourceResolver.retrieveBytesFromResource(fileName); Assert.assertNotNull(bytes); Assert.assertEquals(expected.length, bytes.length); }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_RESOURCE_WITH_GIVEN_RESOURCE_SIZE_BYTE_LIMIT, logLevel = LogLevelConstants.WARN)) public void attemptToRetrieveBytesFromLocalWithResourceSizeByteLimitTest() { String fileName = "retrieveStyleSheetTest.css.dat"; IResourceRetriever retriever = new DefaultResourceRetriever().setResourceSizeByteLimit(88); ResourceResolver resourceResolver = new ResourceResolver(baseUri, retriever); byte[] bytes = resourceResolver.retrieveBytesFromResource(fileName); Assert.assertNull(bytes); }
@Test public void retrieveBytesFromLocalWithResourceSizeByteLimitTest() { String fileName = "retrieveStyleSheetTest.css.dat"; IResourceRetriever retriever = new DefaultResourceRetriever().setResourceSizeByteLimit(89); ResourceResolver resourceResolver = new ResourceResolver(baseUri, retriever); byte[] bytes = resourceResolver.retrieveBytesFromResource(fileName); Assert.assertNotNull(bytes); Assert.assertEquals(((DefaultResourceRetriever) retriever).getResourceSizeByteLimit(), bytes.length); }