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

gu.dtalk.engine.ImageServe Maven / Gradle / Ivy

package gu.dtalk.engine;

import java.io.IOException;

import org.nanohttpd.protocols.http.response.Status;

import com.gitee.l0km.com4j.base.BaseVolatile;
import com.gitee.l0km.com4j.base.BinaryUtils;
import com.gitee.l0km.com4j.base.ILazyInitVariable;
import com.google.common.base.Function;
import gu.dtalk.engine.DtalkHttpServer.Body;

/**
 * 实现GET方法获取图像请求
 * 如果没有找到图像(返回null),则返回默认的404图像 
 * @author guyadong
 *
 */
public class ImageServe extends RESTfulServe {
	private static final ILazyInitVariable ERROR_IMAGE = new BaseVolatile(){

		@Override
		protected Body doGet() {
			try {
				return new Body(Status.NOT_FOUND, 
					"image/jpeg", 
					BinaryUtils.getBytesNotEmpty(ImageServe.class.getResourceAsStream("/web/images/404.jpg")));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}};

	public ImageServe() {
		super(ImageServe.class.getSimpleName());
	}

	public ImageServe(String pathPrefix) {
		super(pathPrefix);
	}

	public ImageServe(String pathPrefix, Function bodyGetter) {
		super(pathPrefix, bodyGetter);
	}
	@Override
	protected Body nullBodyInstead(){
		return ERROR_IMAGE.get();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy