MimeType.java

  1. package org.flasby.thymeleaf;

  2. import java.net.URI;
  3. import java.util.HashMap;
  4. import java.util.Map;

  5. import lombok.extern.log4j.Log4j2;

  6. @Log4j2
  7. public class MimeType {
  8.   public static final String TEXT = "text/html";
  9.   public static final String CSS = "text/css";
  10.   public static final String JS = "text/javascript";
  11.   public static final String ICON = "image/x-icon";
  12.   private static Map<String, String> suffixToMime = new HashMap<>();

  13.   static {
  14.     suffixToMime.put(".html", TEXT);
  15.     suffixToMime.put(".css", CSS);
  16.     suffixToMime.put(".js", JS);
  17.     suffixToMime.put(".ico", ICON);
  18.   }

  19.   public static String getMimeTypeFor(String fileSuffix) {
  20.     return suffixToMime.getOrDefault(fileSuffix, TEXT);
  21.   }

  22.   public static String getMimeTypeFor(URI request) {
  23.     if (!request.getPath().contains(".")) {
  24.       return TEXT;
  25.     }
  26.     String suffix = request.getPath().substring(request.getPath().lastIndexOf('.'));
  27.     String mimetype = getMimeTypeFor(suffix);
  28.     LOG.debug("Getting mime type for {} : {}",request.getPath(), mimetype);
  29.     return mimetype;
  30.   }
  31. }