AdaptiveImageServlet extends SlingSafeMethodsServlet { @Override protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException { try { RequestPathInfo requestPathInfo = request.getRequestPathInfo(); List<String> selectorList = selectorToList(requestPathInfo.getSelectorString()); String suffix = requestPathInfo.getSuffix(); String imageName = StringUtils.isNotEmpty(suffix) ? FilenameUtils.getName(suffix) : ""; if (StringUtils.isNotEmpty(suffix)) { String suffixExtension = FilenameUtils.getExtension(suffix); if (StringUtils.isNotEmpty(suffixExtension)) { if (!suffixExtension.equals(requestPathInfo.getExtension())) { LOGGER.error("The suffix part defines a different extension than the request: {}.", suffix); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } else { LOGGER.error("Invalid suffix: {}.", suffix); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } Resource component = request.getResource(); ResourceResolver resourceResolver = request.getResourceResolver(); if (!component.isResourceType(IMAGE_RESOURCE_TYPE)) { Resource componentCandidate = null; PageManager pageManager = resourceResolver.adaptTo(PageManager.class); if (pageManager != null) { Page page = pageManager.getContainingPage(component); if (page != null) { Template template = page.getTemplate(); if (template != null) { if (StringUtils.isNotEmpty(suffix)) { long lastModifiedSuffix = getRequestLastModifiedSuffix(suffix); String relativeTemplatePath = lastModifiedSuffix == 0 ? suffix.substring(0, suffix.lastIndexOf('.')) : suffix.substring(0, suffix.lastIndexOf("/" + String.valueOf(lastModifiedSuffix))); String imagePath = ResourceUtil.normalize(template.getPath() + relativeTemplatePath); if (StringUtils.isNotEmpty(imagePath) && !template.getPath().equals(imagePath)) { componentCandidate = resourceResolver.getResource(imagePath); } } } } } if (componentCandidate == null) { LOGGER.error("Unable to retrieve an image from this page's template."); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } component = componentCandidate; } ImageComponent imageComponent = new ImageComponent(component); if (imageComponent.source == Source.NONEXISTING) { LOGGER.error("The image from {} does not have a valid file reference.", component.getPath()); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } ValueMap componentProperties = component.getValueMap(); long lastModifiedEpoch = 0; Calendar lastModifiedDate = componentProperties.get(JcrConstants.JCR_LASTMODIFIED, Calendar.class); if (lastModifiedDate == null) { lastModifiedDate = componentProperties.get(NameConstants.PN_PAGE_LAST_MOD, Calendar.class); } if (lastModifiedDate != null) { lastModifiedEpoch = lastModifiedDate.getTimeInMillis(); } Asset asset = null; if (imageComponent.source == Source.ASSET) { asset = imageComponent.imageResource.adaptTo(Asset.class); if (asset == null) { LOGGER.error("Unable to adapt resource {} used by image {} to an asset.", imageComponent.imageResource.getPath(), component.getPath()); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } long assetLastModifiedEpoch = asset.getLastModified(); if (assetLastModifiedEpoch > lastModifiedEpoch) { lastModifiedEpoch = assetLastModifiedEpoch; } } long requestLastModifiedSuffix = getRequestLastModifiedSuffix(suffix); if (requestLastModifiedSuffix >= 0 && requestLastModifiedSuffix != lastModifiedEpoch) { String redirectLocation = getRedirectLocation(request, lastModifiedEpoch); if (StringUtils.isNotEmpty(redirectLocation)) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", redirectLocation); return; } else { LOGGER.error("Unable to determine correct redirect location."); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } } if (!handleIfModifiedSinceHeader(request, response, lastModifiedEpoch)) { Map<String, Integer> transformationMap = getTransformationMap(selectorList, component); Integer jpegQualityInPercentage = transformationMap.get(SELECTOR_QUALITY_KEY); double quality = jpegQualityInPercentage / 100.0d; int resizeWidth = transformationMap.get(SELECTOR_WIDTH_KEY); String imageType = getImageType(requestPathInfo.getExtension()); if (imageComponent.source == Source.FILE) { transformAndStreamFile(response, componentProperties, resizeWidth, quality, imageComponent.imageResource, imageType, imageName); } else if (imageComponent.source == Source.ASSET) { transformAndStreamAsset(response, componentProperties, resizeWidth, quality, asset, imageType, imageName); } } } catch (IllegalArgumentException e) { LOGGER.error("Invalid image request", e.getMessage()); response.sendError(HttpServletResponse.SC_NOT_FOUND); } } AdaptiveImageServlet(MimeTypeService mimeTypeService, AssetStore assetStore, int defaultResizeWidth, int maxInputWidth);  static final String DEFAULT_SELECTOR; static final String CORE_DEFAULT_SELECTOR; static final int DEFAULT_JPEG_QUALITY; static final int DEFAULT_MAX_SIZE; }
@Test void testRequestWithWidthDesignAllowed() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.90.800", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"600","700","800"}, "jpegQuality", 90); servlet.doGet(request, response); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(byteArrayInputStream); Dimension expectedDimension = new Dimension(800, 800); Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight()); Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size."); Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image."); }
@Test void testRequestWithWidthDesignNotAllowed() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.1000", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"600","700","800"}); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus(), "Expected a 404 response when the design does not allow the requested width to be rendered."); Assertions.assertArrayEquals(new byte[0], response.getOutput(), "Expected an empty response output."); }
@Test void testRequestWithWidthNoDesign() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.800", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus(), "Expected a 404 response when the request contains width information but no content policy has been defined."); Assertions.assertArrayEquals(new byte[0], response.getOutput(), "Expected an empty response output."); }
@Test void testRequestNoWidthWithDesign() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = spy(requestResponsePair.getRight()); servlet.doGet(request, response); verify(response).setContentType("image/png"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(byteArrayInputStream); Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH); Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight()); Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered with the default resize configuration width."); }
@Test void testRequestNoWidthNoDesign() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = spy(requestResponsePair.getRight()); servlet.doGet(request, response); verify(response).setContentType("image/png"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(byteArrayInputStream); Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH); Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight()); Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered with the default resize configuration width."); }
@Test void testWrongNumberOfSelectors() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.1.2.3", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus(), "Expected a 404 response when the request has more selectors than expected."); Assertions.assertArrayEquals(new byte[0], response.getOutput(), "Expected an empty response output."); }
@Test void testInvalidWidthSelector() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.full", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus(), "Expected a 404 response when the request has an invalid width selector."); Assertions.assertArrayEquals(new byte[0], response.getOutput(), "Expected an empty response output."); }
@Test void testWithInvalidDesignWidth() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE1_PATH, "img.700", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"600","700","invalid"}); servlet.doGet(request, response); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(byteArrayInputStream); Dimension expectedDimension = new Dimension(700, 700); Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight()); Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size."); Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image."); MatcherAssert.assertThat(testLogger.getLoggingEvents(), hasItem(warn("One of the configured widths ({}) from the {} content policy is not a " + "valid Integer.", "invalid", "/conf/$aem-mock$/settings/wcm/policies/core/wcm/components/image/v1/image/$mock-policy"))); }
@Test void testWithInvalidFileReference() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE2_PATH, "img", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus(), "Expected a 404 response when the image does not have a valid file reference."); Assertions.assertArrayEquals(new byte[0], response.getOutput(), "Expected an empty response output."); }
@Test void testGIFFileDirectStream() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE5_PATH, 1489998822138L, "img", "gif"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput()); InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.gif"); Assertions.assertTrue(IOUtils.contentEquals(stream, directStream)); }
@Test void testSVGFileDirectStream() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE24_PATH, 1489998822138L, "img", "svg"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput()); InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.svg"); Assertions.assertTrue(IOUtils.contentEquals(stream, directStream)); }
@Test void testGIFFileBrowserCached() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE5_PATH, 1489998822138L, "img", "gif"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); request.addDateHeader("If-Modified-Since", 1489998822137L); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus()); }
@Test void testGIFUploadedToDAM() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE6_PATH, "img", "gif"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput()); InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.gif"); Assertions.assertTrue(IOUtils.contentEquals(stream, directStream)); }
@Test void testSVGUploadedToDAM() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE25_PATH, "img", "svg"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput()); InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.svg"); Assertions.assertTrue(IOUtils.contentEquals(stream, directStream)); }
@Test void testGIFUploadedToDAMBrowserCached() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE6_PATH, "img", "gif"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); request.addDateHeader("If-Modified-Since", 1490005239001L); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus()); }
@Test void testCorrectScalingPNGAssetWidth() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.2000", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"2000"}); servlet.doGet(request, response); InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.png"); ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput()); Assertions.assertTrue(IOUtils.contentEquals(directStream, stream), "Expected to get the original asset back, since the requested width is equal to the image's width."); }
@Test void testDAMFileUpscaledPNG() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.2500", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"2500"}); servlet.doGet(request, response); InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.png"); ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput()); Assertions.assertTrue(IOUtils.contentEquals(directStream, stream), "Expected to get the original asset back, since the requested width would result in upscaling the image."); }
@Test void testPNGFileDirectStream() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE3_PATH, "img.600", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"600"}); servlet.doGet(request, response); ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(stream); Assertions.assertEquals(600, image.getWidth()); Assertions.assertEquals(600, image.getHeight()); }
@Test void testWithNoImageNameResourceTypeImage() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair( IMAGE26_PATH, "coreimg.800", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"800"}); requestPathInfo.setSuffix("/1494867377756.png"); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); Assertions.assertEquals(200, response.getStatus(), "Expected a 200 response code."); }
@Test void testWithNoImageNameFromTemplate() throws IOException { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair( PAGE, "coreimg", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); requestPathInfo.setSuffix(TEMPLATE_IMAGE_PATH.replace(TEMPLATE_PATH, "") + "/1490005239000.png"); servlet.doGet(request, response); Assertions.assertEquals(200, response.getStatus(), "Expected a 200 response code."); }
@Test void testImageWithCorrectLastModifiedSuffix() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE19_PATH, "img.800", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); context.contentPolicyMapping(ImageImpl.RESOURCE_TYPE, "allowedRenditionWidths", new String[] {"800"}); servlet.doGet(request, response); Assertions.assertEquals(200, response.getStatus(), "Expected a 200 response code."); Assertions.assertEquals("Mon, 20 Mar 2017 10:20:39 GMT", response.getHeader(HttpConstants.HEADER_LAST_MODIFIED)); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(byteArrayInputStream); Dimension expectedDimension = new Dimension(800, 800); Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight()); Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size."); Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image."); }
@Test void testImageWithIncorrectLastModifiedSuffix() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE19_PATH, "coreimg.800", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); requestPathInfo.setSuffix("/42.png"); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); Assertions.assertEquals(302, response.getStatus(), "Expected a 302 response code."); Assertions.assertEquals(CONTEXT_PATH + "/content/test/jcr%3acontent/root/image19.coreimg.800.png/1490005239000.png", response.getHeader("Location"), "Expected redirect location with correct last modified suffix"); }
@Test void testImageWithMissingLastModifiedSuffix() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE19_PATH, "coreimg.800", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); requestPathInfo.setSuffix(""); servlet.doGet(request, response); Assertions.assertEquals(302, response.getStatus(), "Expected a 302 response code."); Assertions.assertEquals(CONTEXT_PATH + "/content/test/jcr%3acontent/root/image19.coreimg.800.png/1490005239000.png", response.getHeader("Location"), "Expected redirect location with correct last modified suffix"); }
@Test void testFileReferencePriority() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE20_PATH, "img", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(byteArrayInputStream); Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH); Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight()); Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size."); Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image."); }
@Test void testDifferentSuffixExtension() throws IOException { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE3_PATH, 1490005239000L, "img.600", "png", "jpeg"); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(requestResponsePair.getLeft(), response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); }
@Test void testInvalidSuffix() throws IOException { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE3_PATH, "img.600", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockRequestPathInfo rpi = (MockRequestPathInfo) request.getRequestPathInfo(); rpi.setSuffix("/random"); MockSlingHttpServletResponse response = requestResponsePair.getRight(); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); rpi.setSuffix("/1"); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); rpi.setSuffix("/1."); servlet.doGet(request, response); Assertions.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); }
@Test void testImageFromTemplateStructureNodeNoLastModifiedInfo() throws IOException { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(PAGE, "coreimg", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); requestPathInfo.setSuffix(TEMPLATE_IMAGE_PATH.replace(TEMPLATE_PATH, "") + ".png"); servlet.doGet(request, response); Assertions.assertEquals(302, response.getStatus(), "Expected a 302 response code."); Assertions.assertEquals( CONTEXT_PATH + "/content/test.coreimg.png/structure/jcr%3acontent/root/image_template/1490005239000.png", response.getHeader("Location"), "Expected redirect location with correct last modified suffix"); }
@Test void testImageFromTemplateStructureNodeOldLastModifiedInfo() throws IOException { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(PAGE, "coreimg", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); requestPathInfo.setSuffix(TEMPLATE_IMAGE_PATH.replace(TEMPLATE_PATH, "") + "/1490005238000.png"); servlet.doGet(request, response); Assertions.assertEquals(302, response.getStatus(), "Expected a 302 response code."); Assertions.assertEquals( CONTEXT_PATH + "/content/test.coreimg.png/structure/jcr%3acontent/root/image_template/1490005239000.png", response.getHeader("Location"), "Expected redirect location with correct last modified suffix"); }
@Test void testImageFromTemplateStructureNode() throws IOException { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(PAGE, "img", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); requestPathInfo.setSuffix(TEMPLATE_IMAGE_PATH.replace(TEMPLATE_PATH, "") + "/1490005239000.png"); servlet.doGet(request, response); Assertions.assertEquals(200, response.getStatus(), "Expected a 200 response code."); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput()); BufferedImage image = ImageIO.read(byteArrayInputStream); Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH); Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight()); Assertions.assertEquals(expectedDimension, actualDimension, "Expected image rendered at requested size."); Assertions.assertEquals("image/png", response.getContentType(), "Expected a PNG image."); }
@Test void testImageTooLarge() throws Exception { Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE28_PATH, "img", "png"); MockSlingHttpServletRequest request = requestResponsePair.getLeft(); MockSlingHttpServletResponse response = requestResponsePair.getRight(); Assertions.assertThrows( IOException.class, () -> servlet.doGet(request, response), "Expecting to throw an exception complaining that dimensions are too large"); }