001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 * 019 */ 020package org.nuxeo.ecm.platform.pictures.tiles.api.imageresource; 021 022import java.io.IOException; 023import java.io.InputStream; 024import java.util.Calendar; 025 026import org.apache.commons.codec.digest.DigestUtils; 027import org.apache.commons.io.IOUtils; 028import org.nuxeo.ecm.core.api.Blob; 029 030/** 031 * Blob based implementation of the ImageResource Because ImageResource will be cached this Implementation is not 032 * optimal (Blob digest is not compulsory and the modification date is not set). 033 * <p> 034 * This implementation is mainly used for unit testing. 035 * 036 * @author tiry 037 */ 038public class BlobResource implements ImageResource { 039 040 private static final long serialVersionUID = 1L; 041 042 protected Blob blob; 043 044 protected String hash; 045 046 protected Calendar modified; 047 048 public BlobResource(Blob blob) { 049 this.blob = blob; 050 if (blob.getDigest() != null) { 051 hash = blob.getDigest(); 052 } else { 053 hash = getMD5Digest(); 054 } 055 056 modified = Calendar.getInstance(); 057 } 058 059 public Blob getBlob() { 060 return blob; 061 } 062 063 public String getHash() { 064 return hash; 065 } 066 067 public Calendar getModificationDate() { 068 return modified; 069 } 070 071 private String getMD5Digest() { 072 try (InputStream in = blob.getStream()) { 073 return DigestUtils.md5Hex(in); 074 } catch (IOException e) { 075 return blob.hashCode() + "fakeHash"; 076 } 077 } 078 079}