DownloadServlet extends SlingAllMethodsServlet { @Override protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws IOException { Asset asset = request.getResource().adaptTo(Asset.class); if (asset == null) { String filename = request.getRequestPathInfo().getSuffix(); Resource downloadDataResource = request.getResource().getChild(JcrConstants.JCR_CONTENT); if (StringUtils.isNotEmpty(filename) && downloadDataResource != null) { sendResource(request, response, filename, downloadDataResource); } } else { if (isUnchanged(asset.getLastModified(), request)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } Optional<InputStream> inputStream = Optional.empty(); try { inputStream = getInputStream(asset); if (!inputStream.isPresent()) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } response.setStatus(HttpServletResponse.SC_OK); sendResponse(inputStream.get(), asset.getOriginal().getSize(), asset.getMimeType(), asset.getName(), asset.getLastModified(), response, checkForInlineSelector(request)); } finally { if (inputStream.isPresent()) { inputStream.get().close(); } } } }   static final String SELECTOR; static final String INLINE_SELECTOR; }
@Test void testAttachmentAssetDownload() throws Exception { context.currentResource(PDF_ASSET_PATH); context.request().setHeader("If-Modified-Since", ""); context.requestPathInfo().setSelectorString(DownloadServlet.SELECTOR); context.requestPathInfo().setExtension("pdf"); downloadServlet.doGet(context.request(), context.response()); assertTrue(context.response().containsHeader("Content-Disposition")); assertEquals("attachment; filename=\"Download_Test_PDF.pdf\"", context.response().getHeader("Content-Disposition")); assertEquals(8192, context.response().getBufferSize()); }
@Test void testAttachmentFileDownload() throws Exception { context.currentResource(PDF_FILE_PATH + "/file"); context.request().setHeader("If-Modified-Since", ""); context.requestPathInfo().setSelectorString(DownloadServlet.SELECTOR); context.requestPathInfo().setExtension("pdf"); context.requestPathInfo().setSuffix("Download_Test_PDF.pdf"); downloadServlet.doGet(context.request(), context.response()); assertTrue(context.response().containsHeader("Content-Disposition")); assertEquals("attachment; filename=\"Download_Test_PDF.pdf\"", context.response().getHeader("Content-Disposition")); assertEquals(8192, context.response().getBufferSize()); }
@Test void testInlineAssetDownload() throws Exception { context.currentResource(PDF_ASSET_PATH); context.request().setHeader("If-Modified-Since", ""); context.requestPathInfo().setSelectorString(DownloadServlet.SELECTOR + "." + DownloadServlet.INLINE_SELECTOR); context.requestPathInfo().setExtension("pdf"); downloadServlet.doGet(context.request(), context.response()); assertTrue(context.response().containsHeader("Content-Disposition")); assertEquals("inline", context.response().getHeader("Content-Disposition")); assertEquals(8192, context.response().getBufferSize()); }
@Test void testNotModifiedResponse() throws Exception { context.currentResource(PDF_ASSET_PATH); context.request().setHeader("If-Modified-Since", "Fri, 19 Oct 2018 19:24:07 GMT"); downloadServlet.doGet(context.request(), context.response()); assertEquals(304, context.response().getStatus()); }
@Test void tesNotModifiedResponseForResource() throws Exception { MockSlingHttpServletRequest request = context.request(); MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo(); requestPathInfo.setSuffix("Download_Test_PDF.pdf"); context.currentResource(PDF_FILE_PATH + "/file"); request.setHeader("If-Modified-Since", "Fri, 19 Oct 2018 19:24:07 GMT"); downloadServlet.doGet(request, context.response()); assertEquals(304, context.response().getStatus()); }