ResourceResolver { public InputStream retrieveResourceAsInputStream(String src) { byte[] bytes = retrieveBytesFromBase64Src(src); if (bytes != null) { return new ByteArrayInputStream(bytes); } try { URL url = uriResolver.resolveAgainstBaseUri(src); return retriever.getInputStreamByUrl(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 retrieveResourceAsInputStreamByMalformedResourceNameTest() { String fileName = "retrieveStyl eSheetTest.css"; ResourceResolver resourceResolver = new ResourceResolver(baseUri); InputStream stream = resourceResolver.retrieveResourceAsInputStream(fileName); Assert.assertNull(stream); }
@Test public void attemptToReadBytesFromLimitedInputStreamTest() throws IOException { junitExpectedException.expect(ReadingByteLimitException.class); String fileName = "retrieveStyleSheetTest.css.dat"; IResourceRetriever retriever = new DefaultResourceRetriever().setResourceSizeByteLimit(40); ResourceResolver resourceResolver = new ResourceResolver(baseUri, retriever); InputStream stream = resourceResolver.retrieveResourceAsInputStream(fileName); for (int i = 0; i < 41; i++) { stream.read(); } }
@Test public void retrieveResourceAsInputStreamBase64Test() { ResourceResolver resourceResolver = new ResourceResolver(baseUri); InputStream stream = resourceResolver.retrieveResourceAsInputStream(bLogo); Assert.assertNotNull(stream); }
@Test public void retrieveResourceAsInputStreamStyleSheetTest() throws IOException { String fileName = "retrieveStyleSheetTest.css"; InputStream expected = new FileInputStream(baseUri + fileName); ResourceResolver resourceResolver = new ResourceResolver(baseUri); InputStream stream = resourceResolver.retrieveResourceAsInputStream(fileName); Assert.assertNotNull(stream); Assert.assertEquals(expected.read(), stream.read()); }
@Test public void retrieveInputStreamWithRetryRetrieverTest() throws IOException { String fileName = "!invalid! StyleSheetName.css"; ResourceResolver resourceResolver = new ResourceResolver(baseUri, new RetryResourceRetriever(baseUri)); InputStream expected = new FileInputStream(baseUri + "retrieveStyleSheetTest.css"); InputStream stream = resourceResolver.retrieveResourceAsInputStream(fileName); Assert.assertNotNull(stream); Assert.assertEquals(expected.read(), stream.read()); }
@Test public void retrieveResourceAsInputStreamAbsolutePathTest() throws IOException { String fileName = "retrieveStyleSheetTest.css"; String absolutePath = Paths.get(baseUri, fileName).toFile().getAbsolutePath(); ResourceResolver resourceResolver = new ResourceResolver(baseUri); try (InputStream stream = resourceResolver.retrieveResourceAsInputStream(absolutePath); InputStream expected = new FileInputStream(absolutePath);) { Assert.assertNotNull(stream); Assert.assertEquals(expected.read(), stream.read()); } }
@Test public void retrieveResourceAsInputStreamFileUrlTest() throws IOException { String fileName = "retrieveStyleSheetTest.css"; URL url = Paths.get(baseUri, fileName).toUri().toURL(); String fileUrlString = url.toExternalForm(); ResourceResolver resourceResolver = new ResourceResolver(baseUri); try (InputStream stream = resourceResolver.retrieveResourceAsInputStream(fileUrlString); InputStream expected = url.openStream()) { Assert.assertNotNull(stream); Assert.assertEquals(expected.read(), stream.read()); } }