@Test public void testInvalid() throws IOException, ServletException { log.info("testContentTypes"); context.currentResource("/content/apache/sling-apache-org/index/apache.png"); context.requestPathInfo().setSuffix("/test2.png"); context.requestPathInfo().setExtension("transform"); ts.doGet(context.request(), context.response()); assertEquals(400, context.response().getStatus()); }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { log.trace("doGet"); String name = StringUtils.substringBeforeLast(request.getRequestPathInfo().getSuffix(), "."); response.setHeader("Content-Disposition", "filename=" + request.getResource().getName()); String format = StringUtils.substringAfterLast(request.getRequestPathInfo().getSuffix(), "."); log.debug("Transforming resource: {} with transformation: {} to {}", request.getResource(), name, format); String original = response.getContentType(); try (ResourceResolver serviceResolver = transformationServiceUser.getTransformationServiceUser()) { Transformation transformation = findTransformation(serviceResolver, name); if (transformation != null) { response.setContentType(OutputFileFormat.forRequest(request).getMimeType()); String expectedPath = "jcr:content/renditions/" + name + "." + format; Resource rendition = request.getResource().getChild(expectedPath); if (rendition != null) { log.debug("Using existing rendition {}", name); IOUtils.copy(rendition.adaptTo(InputStream.class), response.getOutputStream()); } else { log.debug("Creating new rendition {}", name); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(request.getResource(), transformation, OutputFileFormat.valueOf(format.toUpperCase()), baos); IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream()); if (CMSConstants.NT_FILE.equals(request.getResource().getResourceType())) { Resource file = ResourceUtil.getOrCreateResource(serviceResolver, request.getResource().getPath() + "/" + expectedPath, Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_FILE), JcrConstants.NT_UNSTRUCTURED, false); Map<String, Object> properties = new HashMap<>(); properties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED); properties.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(baos.toByteArray())); ResourceUtil.getOrCreateResource(serviceResolver, file.getPath() + "/" + JcrConstants.JCR_CONTENT, properties, JcrConstants.NT_UNSTRUCTURED, true); } } } else { log.error("Exception transforming image: {} with transformation: {}", request.getResource(), name); response.setContentType(original); response.sendError(400, "Could not transform image with transformation: " + name); } } catch (Exception e) { log.error("Exception rendering transformed resource", e); response.setStatus(500); RequestDispatcherOptions op = new RequestDispatcherOptions(); op.setReplaceSuffix(config.errorSuffix()); op.setReplaceSelectors("transform"); RequestDispatcher disp = request.getRequestDispatcher(config.errorResourcePath(), op); disp.forward(request, response); } }
TransformServlet extends SlingSafeMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { log.trace("doGet"); String name = StringUtils.substringBeforeLast(request.getRequestPathInfo().getSuffix(), "."); response.setHeader("Content-Disposition", "filename=" + request.getResource().getName()); String format = StringUtils.substringAfterLast(request.getRequestPathInfo().getSuffix(), "."); log.debug("Transforming resource: {} with transformation: {} to {}", request.getResource(), name, format); String original = response.getContentType(); try (ResourceResolver serviceResolver = transformationServiceUser.getTransformationServiceUser()) { Transformation transformation = findTransformation(serviceResolver, name); if (transformation != null) { response.setContentType(OutputFileFormat.forRequest(request).getMimeType()); String expectedPath = "jcr:content/renditions/" + name + "." + format; Resource rendition = request.getResource().getChild(expectedPath); if (rendition != null) { log.debug("Using existing rendition {}", name); IOUtils.copy(rendition.adaptTo(InputStream.class), response.getOutputStream()); } else { log.debug("Creating new rendition {}", name); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(request.getResource(), transformation, OutputFileFormat.valueOf(format.toUpperCase()), baos); IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream()); if (CMSConstants.NT_FILE.equals(request.getResource().getResourceType())) { Resource file = ResourceUtil.getOrCreateResource(serviceResolver, request.getResource().getPath() + "/" + expectedPath, Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_FILE), JcrConstants.NT_UNSTRUCTURED, false); Map<String, Object> properties = new HashMap<>(); properties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED); properties.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(baos.toByteArray())); ResourceUtil.getOrCreateResource(serviceResolver, file.getPath() + "/" + JcrConstants.JCR_CONTENT, properties, JcrConstants.NT_UNSTRUCTURED, true); } } } else { log.error("Exception transforming image: {} with transformation: {}", request.getResource(), name); response.setContentType(original); response.sendError(400, "Could not transform image with transformation: " + name); } } catch (Exception e) { log.error("Exception rendering transformed resource", e); response.setStatus(500); RequestDispatcherOptions op = new RequestDispatcherOptions(); op.setReplaceSuffix(config.errorSuffix()); op.setReplaceSelectors("transform"); RequestDispatcher disp = request.getRequestDispatcher(config.errorResourcePath(), op); disp.forward(request, response); } } }
TransformServlet extends SlingSafeMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { log.trace("doGet"); String name = StringUtils.substringBeforeLast(request.getRequestPathInfo().getSuffix(), "."); response.setHeader("Content-Disposition", "filename=" + request.getResource().getName()); String format = StringUtils.substringAfterLast(request.getRequestPathInfo().getSuffix(), "."); log.debug("Transforming resource: {} with transformation: {} to {}", request.getResource(), name, format); String original = response.getContentType(); try (ResourceResolver serviceResolver = transformationServiceUser.getTransformationServiceUser()) { Transformation transformation = findTransformation(serviceResolver, name); if (transformation != null) { response.setContentType(OutputFileFormat.forRequest(request).getMimeType()); String expectedPath = "jcr:content/renditions/" + name + "." + format; Resource rendition = request.getResource().getChild(expectedPath); if (rendition != null) { log.debug("Using existing rendition {}", name); IOUtils.copy(rendition.adaptTo(InputStream.class), response.getOutputStream()); } else { log.debug("Creating new rendition {}", name); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(request.getResource(), transformation, OutputFileFormat.valueOf(format.toUpperCase()), baos); IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream()); if (CMSConstants.NT_FILE.equals(request.getResource().getResourceType())) { Resource file = ResourceUtil.getOrCreateResource(serviceResolver, request.getResource().getPath() + "/" + expectedPath, Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_FILE), JcrConstants.NT_UNSTRUCTURED, false); Map<String, Object> properties = new HashMap<>(); properties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED); properties.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(baos.toByteArray())); ResourceUtil.getOrCreateResource(serviceResolver, file.getPath() + "/" + JcrConstants.JCR_CONTENT, properties, JcrConstants.NT_UNSTRUCTURED, true); } } } else { log.error("Exception transforming image: {} with transformation: {}", request.getResource(), name); response.setContentType(original); response.sendError(400, "Could not transform image with transformation: " + name); } } catch (Exception e) { log.error("Exception rendering transformed resource", e); response.setStatus(500); RequestDispatcherOptions op = new RequestDispatcherOptions(); op.setReplaceSuffix(config.errorSuffix()); op.setReplaceSelectors("transform"); RequestDispatcher disp = request.getRequestDispatcher(config.errorResourcePath(), op); disp.forward(request, response); } }  }
TransformServlet extends SlingSafeMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { log.trace("doGet"); String name = StringUtils.substringBeforeLast(request.getRequestPathInfo().getSuffix(), "."); response.setHeader("Content-Disposition", "filename=" + request.getResource().getName()); String format = StringUtils.substringAfterLast(request.getRequestPathInfo().getSuffix(), "."); log.debug("Transforming resource: {} with transformation: {} to {}", request.getResource(), name, format); String original = response.getContentType(); try (ResourceResolver serviceResolver = transformationServiceUser.getTransformationServiceUser()) { Transformation transformation = findTransformation(serviceResolver, name); if (transformation != null) { response.setContentType(OutputFileFormat.forRequest(request).getMimeType()); String expectedPath = "jcr:content/renditions/" + name + "." + format; Resource rendition = request.getResource().getChild(expectedPath); if (rendition != null) { log.debug("Using existing rendition {}", name); IOUtils.copy(rendition.adaptTo(InputStream.class), response.getOutputStream()); } else { log.debug("Creating new rendition {}", name); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(request.getResource(), transformation, OutputFileFormat.valueOf(format.toUpperCase()), baos); IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream()); if (CMSConstants.NT_FILE.equals(request.getResource().getResourceType())) { Resource file = ResourceUtil.getOrCreateResource(serviceResolver, request.getResource().getPath() + "/" + expectedPath, Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_FILE), JcrConstants.NT_UNSTRUCTURED, false); Map<String, Object> properties = new HashMap<>(); properties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED); properties.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(baos.toByteArray())); ResourceUtil.getOrCreateResource(serviceResolver, file.getPath() + "/" + JcrConstants.JCR_CONTENT, properties, JcrConstants.NT_UNSTRUCTURED, true); } } } else { log.error("Exception transforming image: {} with transformation: {}", request.getResource(), name); response.setContentType(original); response.sendError(400, "Could not transform image with transformation: " + name); } } catch (Exception e) { log.error("Exception rendering transformed resource", e); response.setStatus(500); RequestDispatcherOptions op = new RequestDispatcherOptions(); op.setReplaceSuffix(config.errorSuffix()); op.setReplaceSelectors("transform"); RequestDispatcher disp = request.getRequestDispatcher(config.errorResourcePath(), op); disp.forward(request, response); } }  @Activate void activate(TransformServletConfig config); @Reference void setTransformationServiceUser(TransformationServiceUser transformationServiceUser); @Reference void setTransformer(Transformer transformer); }
TransformServlet extends SlingSafeMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { log.trace("doGet"); String name = StringUtils.substringBeforeLast(request.getRequestPathInfo().getSuffix(), "."); response.setHeader("Content-Disposition", "filename=" + request.getResource().getName()); String format = StringUtils.substringAfterLast(request.getRequestPathInfo().getSuffix(), "."); log.debug("Transforming resource: {} with transformation: {} to {}", request.getResource(), name, format); String original = response.getContentType(); try (ResourceResolver serviceResolver = transformationServiceUser.getTransformationServiceUser()) { Transformation transformation = findTransformation(serviceResolver, name); if (transformation != null) { response.setContentType(OutputFileFormat.forRequest(request).getMimeType()); String expectedPath = "jcr:content/renditions/" + name + "." + format; Resource rendition = request.getResource().getChild(expectedPath); if (rendition != null) { log.debug("Using existing rendition {}", name); IOUtils.copy(rendition.adaptTo(InputStream.class), response.getOutputStream()); } else { log.debug("Creating new rendition {}", name); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(request.getResource(), transformation, OutputFileFormat.valueOf(format.toUpperCase()), baos); IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream()); if (CMSConstants.NT_FILE.equals(request.getResource().getResourceType())) { Resource file = ResourceUtil.getOrCreateResource(serviceResolver, request.getResource().getPath() + "/" + expectedPath, Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_FILE), JcrConstants.NT_UNSTRUCTURED, false); Map<String, Object> properties = new HashMap<>(); properties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED); properties.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(baos.toByteArray())); ResourceUtil.getOrCreateResource(serviceResolver, file.getPath() + "/" + JcrConstants.JCR_CONTENT, properties, JcrConstants.NT_UNSTRUCTURED, true); } } } else { log.error("Exception transforming image: {} with transformation: {}", request.getResource(), name); response.setContentType(original); response.sendError(400, "Could not transform image with transformation: " + name); } } catch (Exception e) { log.error("Exception rendering transformed resource", e); response.setStatus(500); RequestDispatcherOptions op = new RequestDispatcherOptions(); op.setReplaceSuffix(config.errorSuffix()); op.setReplaceSelectors("transform"); RequestDispatcher disp = request.getRequestDispatcher(config.errorResourcePath(), op); disp.forward(request, response); } }  @Activate void activate(TransformServletConfig config); @Reference void setTransformationServiceUser(TransformationServiceUser transformationServiceUser); @Reference void setTransformer(Transformer transformer); static final String SERVICE_USER; }