All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.http.converter.BufferedImageHttpMessageConverterCustom Maven / Gradle / Ivy

The newest version!
package org.springframework.http.converter;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageWriteParam;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.SystemPropertyUtils;

public class BufferedImageHttpMessageConverterCustom extends BufferedImageHttpMessageConverter {
  private static final Set excludeValues = new HashSet(1);
  static {
    excludeValues.add(new MediaType(MediaType.IMAGE_PNG.getType(), "vnd.wap.wbmp"));
  }
  private final boolean webp = ClassUtils.isPresent("com.luciad.imageio.webp.WebPWriteParam", getClass().getClassLoader())
      && ClassUtils.isPresent("com.luciad.imageio.webp.WebPReadParam", getClass().getClassLoader());

  public BufferedImageHttpMessageConverterCustom() {
    File tmpdir = new File(SystemPropertyUtils.resolvePlaceholders("${java.io.tmpdir}"));
    File cacheDir = new File(SystemPropertyUtils.resolvePlaceholders("${user.home}"), StringUtils.getFilename(tmpdir.getName()));

    Assert.isTrue(cacheDir != null && (cacheDir.mkdirs() || cacheDir.isDirectory()), "Create CacheDir fail..." + cacheDir);
    setCacheDir(cacheDir);

    List readableMediaTypes = new ArrayList(getSupportedMediaTypes());
    for (Iterator iterator = readableMediaTypes.iterator(); iterator.hasNext();) {
      MediaType next = iterator.next();
      if (excludeValues.contains(next)) {
        iterator.remove();
      }
    }

    Field readableMediaTypesField = ReflectionUtils.findField(getClass(), "readableMediaTypes");
    ReflectionUtils.makeAccessible(readableMediaTypesField);
    ReflectionUtils.setField(readableMediaTypesField, this, readableMediaTypes);

    Field defaultContentTypeField = ReflectionUtils.findField(getClass(), "defaultContentType");
    ReflectionUtils.makeAccessible(defaultContentTypeField);
    ReflectionUtils.setField(defaultContentTypeField, this, null);

    String[] writerMediaTypes = ImageIO.getWriterMIMETypes();
    for (String mediaType : writerMediaTypes) {
      if (StringUtils.hasText(mediaType)) {
        MediaType parseMediaType = MediaType.parseMediaType(mediaType);
        if (!excludeValues.contains(parseMediaType)) {
          setDefaultContentType(parseMediaType);
          break;
        }
      }
    }
  }

  @Override
  public boolean canRead(Class clazz, MediaType mediaType) {
    return super.canRead(clazz, mediaType == null ? null : new MediaType(mediaType.getType(), mediaType.getSubtype()));
  }

  @Override
  public boolean canWrite(Class clazz, MediaType mediaType) {
    return super.canWrite(clazz, mediaType == null ? null : new MediaType(mediaType.getType(), mediaType.getSubtype()));
  }

  @Override
  public BufferedImage read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    MediaType mediaType = inputMessage.getHeaders().getContentType();
    if (mediaType != null) {
      inputMessage.getHeaders().setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype()));
    }
    return super.read(clazz, inputMessage);
  }

  /**
   * @see "x-ms-bmp"#equalsIgnoreCase(MediaType#getSubtype())
   */
  @Override
  public void write(BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType mediaType = outputMessage.getHeaders().getContentType();
    if (mediaType != null) {
      contentType = mediaType;
    }
    super.write(image, contentType, outputMessage);
  }

  /**
   * 
    if (irp instanceof com.luciad.imageio.webp.WebPReadParam) {
      ((com.luciad.imageio.webp.WebPReadParam) irp).setBypassFiltering(true);
    }
   * 
   */
  @Override
  protected void process(ImageReadParam irp) {
    if (this.webp) {
      Method method = ReflectionUtils.findMethod(irp.getClass(), "setBypassFiltering", Boolean.class);
      if (method != null) {
        ReflectionUtils.invokeMethod(method, irp, true);
      }
    }
  }

  /**
   * 
    if (iwp instanceof com.luciad.imageio.webp.WebPWriteParam) {
      ((com.luciad.imageio.webp.WebPWriteParam) iwp).setCompressionMode(com.luciad.imageio.webp.WebPWriteParam.MODE_DEFAULT);
    }
   * 
   */
  @Override
  protected void process(ImageWriteParam iwp) {
    if (this.webp) {
      Method method = ReflectionUtils.findMethod(iwp.getClass(), "setCompressionMode", Integer.class);
      if (method != null) {
        ReflectionUtils.invokeMethod(method, iwp, 1);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy