DocumentHTMLProcessor { public InputStream process(InputStream inputStream) { File tempFile = null; InputStream processedInputStream = null; String replacement = ""; try (Scanner scanner = new Scanner(inputStream)) { scanner.useDelimiter(">"); tempFile = FileUtil.createTempFile(); String imageRequestToken = ImageRequestTokenUtil.createToken( PrincipalThreadLocal.getUserId()); while (scanner.hasNext()) { String token = scanner.next(); if (Validator.isNotNull(token)) { token += ">"; replacement = token.replaceAll( _DOCUMENTS_REGEX, "$1&auth_token=" + imageRequestToken + "$3"); replacement = replacement.replaceAll( _IMAGE_REGEX, "$1&auth_token=" + imageRequestToken + "$3"); replacement = replacement.replaceAll( _PORTLET_FILE_ENTRY_REGEX, "$1?auth_token=" + imageRequestToken + "$3"); replacement = replacement.replaceAll( _WIKI_PAGE_ATTACHMENT_REGEX, "$1$3&auth_token=" + imageRequestToken + "$6"); FileUtil.write(tempFile, replacement, true, true); } } processedInputStream = new AutoDeleteFileInputStream(tempFile); } catch (Exception exception) { _log.error(exception, exception); } return processedInputStream; }  InputStream process(InputStream inputStream);  }
@Test public void testProcessDocumentURLWithThumbnailParameter() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/documents/29543/100903188/how-long/4e69-b2cc-e6ef21c10?t=1513212", "&imageThumbnail=1\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/documents/29543/100903188/how-long/4e69-b2cc-e6ef21c10?t=1513212", "&imageThumbnail=1&auth_token=authtoken\"/></body></html>"); Assert.assertEquals(expectedHTML, processedHTML); }
@Test public void testProcessImageURLWithThumbnailParameter() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/image/image_gallery?uuid=f17b2a6b-70ee-4121-ae6e-61c22ff47", "&groupId=807138&t=12798459506&imageThumbnail=1\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/image/image_gallery?uuid=f17b2a6b-70ee-4121-ae6e-61c22ff47", "&groupId=807138&t=12798459506&imageThumbnail=1", "&auth_token=authtoken\"/></body></html>"); Assert.assertEquals(expectedHTML, processedHTML); }
@Test public void testProcessImgTagWithAttributesAndSimpleDocumentURL() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body>", "<img class=\"test\" src=\"/documents/29543/100903188/how-long", "/4e69-b2cc-e6ef21c10?t=1513212\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body>", "<img class=\"test\" src=\"/documents/29543/100903188/how-long", "/4e69-b2cc-e6ef21c10?t=1513212&auth_token=authtoken\"/></body>", "</html>"); Assert.assertEquals(expectedHTML, processedHTML); }
@Test public void testProcessImgTagWithAttributesAndSimpleImageURL() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body>", "<img class=\"test\" src=\"/image", "/image_gallery?uuid=f17b2a6b-70ee-4121-ae6e-61c22ff47", "&groupId=807138&t=12798459506\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body>", "<img class=\"test\" src=\"/image", "/image_gallery?uuid=f17b2a6b-70ee-4121-ae6e-61c22ff47", "&groupId=807138&t=12798459506&auth_token=authtoken\"/>", "</body></html>"); Assert.assertEquals(expectedHTML, processedHTML); }
@Test public void testProcessImgTagWithAttributesAndSimplePortletFileEntryURL() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body>", "<img class=\"test\" src=\"/documents/portlet_file_entry/10766", "/test-title/f17b2a6b-ae6e-61cf\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body>", "<img class=\"test\" src=\"/documents/portlet_file_entry/10766", "/test-title/f17b2a6b-ae6e-61cf?auth_token=authtoken\"/></body>", "</html>"); Assert.assertEquals(expectedHTML, processedHTML); }
@Test public void testProcessSimpleDocumentURL() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/documents/29543/100903188/how-long/4e69-b2cc-e6ef21c10?t=1513212", "\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/documents/29543/100903188/how-long/4e69-b2cc-e6ef21c10?t=1513212", "&auth_token=authtoken\"/></body></html>"); Assert.assertEquals(expectedHTML, processedHTML); }
@Test public void testProcessSimpleImageURL() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/image/image_gallery?uuid=f17b2a6b-70ee-4121-ae6e-61c22ff47", "&groupId=807138&t=12798459506\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/image/image_gallery?uuid=f17b2a6b-70ee-4121-ae6e-61c22ff47", "&groupId=807138&t=12798459506&auth_token=authtoken\"/>", "</body></html>"); Assert.assertEquals(expectedHTML, processedHTML); }
@Test public void testProcessSimplePortletFileEntryURL() throws Exception { DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor(); String originalHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/documents/portlet_file_entry/10766/test-title/f17b2a6b-ae6e-61cf", "\"/></body></html>"); InputStream originalInputStream = new ByteArrayInputStream( originalHTML.getBytes()); InputStream processedInputStream = documentHTMLProcessor.process( originalInputStream); String processedHTML = IOUtils.toString(processedInputStream, "UTF-8"); String expectedHTML = StringBundler.concat( "<html><head><title>test-title</title></head><body><img src=\"", "/documents/portlet_file_entry/10766/test-title/f17b2a6b-ae6e-61cf", "?auth_token=authtoken\"/></body></html>"); Assert.assertEquals(expectedHTML, processedHTML); }