MediaFileServlet.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.media.impl;

  21. import javax.servlet.Servlet;

  22. import org.apache.sling.api.SlingHttpServletResponse;
  23. import org.apache.sling.api.servlets.HttpConstants;
  24. import org.jetbrains.annotations.NotNull;
  25. import org.osgi.service.component.annotations.Activate;
  26. import org.osgi.service.component.annotations.Component;
  27. import org.osgi.service.metatype.annotations.AttributeDefinition;
  28. import org.osgi.service.metatype.annotations.Designate;
  29. import org.osgi.service.metatype.annotations.ObjectClassDefinition;

  30. import com.day.cq.commons.jcr.JcrConstants;

  31. /**
  32.  * Stream binary data stored in a nt:file or nt:resource node.
  33.  * Optional support for Content-Disposition header ("download_attachment").
  34.  */
  35. @Component(service = Servlet.class, immediate = true, property = {
  36.     "sling.servlet.extensions=" + MediaFileServletConstants.EXTENSION,
  37.     "sling.servlet.selectors=" + MediaFileServletConstants.SELECTOR,
  38.     "sling.servlet.resourceTypes=" + JcrConstants.NT_FILE,
  39.     "sling.servlet.resourceTypes=" + JcrConstants.NT_RESOURCE,
  40.     "sling.servlet.methods=" + HttpConstants.METHOD_GET
  41. })
  42. @Designate(ocd = MediaFileServlet.Config.class)
  43. public final class MediaFileServlet extends AbstractMediaFileServlet {
  44.   private static final long serialVersionUID = 1L;

  45.   private boolean svgContentSecurityPolicy;

  46.   @ObjectClassDefinition(
  47.       name = "wcm.io Media Handler Media File Servlet",
  48.       description = "Configures delivery of media file binaries.")
  49.   @interface Config {

  50.     @AttributeDefinition(
  51.         name = "SVG Content Security Policy",
  52.         description = "Apply XSS protection when serving SVG files by setting Content-Security-Policy to 'sandbox'.")
  53.     boolean svgContentSecurityPolicy() default true;

  54.   }

  55.   @Activate
  56.   private void activate(Config config) {
  57.     this.svgContentSecurityPolicy = config.svgContentSecurityPolicy();
  58.   }

  59.   @Override
  60.   protected void setSVGContentSecurityPolicy(@NotNull SlingHttpServletResponse response) {
  61.     if (this.svgContentSecurityPolicy) {
  62.       super.setSVGContentSecurityPolicy(response);
  63.     }
  64.   }

  65. }