DummyImageServlet.java

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

import java.awt.Color;
import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;

import com.day.image.Font;
import com.day.image.Layer;
import com.day.image.font.AbstractFont;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.wcm.handler.media.MediaFileType;
import io.wcm.handler.media.spi.MediaHandlerConfig;
import io.wcm.handler.url.suffix.SuffixParser;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.wcm.commons.contenttype.ContentType;
import io.wcm.wcm.commons.contenttype.FileExtension;

/**
 * Servlet which is used to render a dummy image with information about the size of the image and media format.
 * Note: most of the values used for scaling are the result of experiments, how the image looks nice.
 */
@Component(service = Servlet.class, immediate = true, property = {
    "sling.servlet.extensions=" + FileExtension.PNG,
    "sling.servlet.resourceTypes=/apps/wcm-io/handler/media/components/dummyImage"
})
public final class DummyImageServlet extends SlingSafeMethodsServlet {

  private static final long serialVersionUID = 1L;

  /**
   * Dummy image content path
   */
  @SuppressWarnings("java:S1075") // no file path
  public static final @NotNull String PATH = "/apps/wcm-io/handler/media/content/dummyImage";

  /**
   * Suffix for width
   */
  public static final @NotNull String SUFFIX_WIDTH = "width";

  /**
   * Suffix for height
   */
  public static final @NotNull String SUFFIX_HEIGHT = "height";

  /**
   * Suffix for Name of mediaformat (optional)
   */
  public static final @NotNull String SUFFIX_MEDIA_FORMAT_NAME = "mf";


  @Override
  protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response)
      throws ServletException, IOException {

    // detect image type from extension
    MediaFileType type = MediaFileType.getByFileExtensions(request.getRequestPathInfo().getExtension());
    if (type == null || !MediaFileType.isBrowserImage(type.getExtension())) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND, "Image type not supported");
      return;
    }

    // generate dummy image
    Layer layer = createLayer(request);

    // output image binary
    String contentType = type.getContentTypes().stream().findFirst().orElse(ContentType.OCTET_STREAM);
    response.setContentType(contentType);
    MediaHandlerConfig config = AdaptTo.notNull(request, MediaHandlerConfig.class);
    layer.write(type.getExtension(), config.getDefaultImageQuality(contentType), response.getOutputStream());
  }

  @SuppressWarnings("null")
  @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
  protected Layer createLayer(SlingHttpServletRequest request) {
    SuffixParser parser = new SuffixParser(request);
    int width = parser.get(SUFFIX_WIDTH, 0);
    int height = parser.get(SUFFIX_HEIGHT, 0);
    String name = parser.get(SUFFIX_MEDIA_FORMAT_NAME, String.class);

    // validate width/height
    if (width < 1 || height < 1) {
      return new Layer(1, 1, null);
    }

    Layer textLayer = getTextLayer(width, height, name);

    int x = (width - textLayer.getWidth()) / 2;
    int y = (height - textLayer.getHeight()) / 2;

    Layer layer = new Layer(width, height, Color.GRAY);

    // we now build a grey box for the text which is slightly bigger than the text
    int backgroundOffset = width / 120;
    Layer textBackground = new Layer(textLayer.getWidth() + (backgroundOffset * 2), textLayer.getHeight() + (backgroundOffset * 2), Color.DARK_GRAY);
    layer.blit(textBackground, x - backgroundOffset, y - backgroundOffset, textBackground.getWidth(), textBackground.getHeight(), 0, 0);

    // now we can add the text
    layer.blit(textLayer, x, y, textLayer.getWidth(), textLayer.getHeight(), 0, 0);
    return layer;
  }

  private Layer getTextLayer(int width, int height, String name) {
    String text = (StringUtils.isNotEmpty(name) ? name + "\n" : "")
        + width + " x " + height;
    Font font = new Font("Arial", width / 30);
    int align = AbstractFont.ALIGN_BASE | AbstractFont.ALIGN_CENTER | AbstractFont.TTANTIALIASED;
    Layer ret = new Layer(1, 1, null);
    ret.setPaint(Color.WHITE);
    ret.drawText(0, 0, 0, 0, text, font, align, 0, 0);
    return ret;
  }

}