001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Thierry Delprat 011 * Florent Guillaume 012 */ 013 014package org.nuxeo.ecm.core.api.blobholder; 015 016import java.io.IOException; 017import java.io.InputStream; 018import java.util.ArrayList; 019import java.util.Calendar; 020import java.util.List; 021 022import org.apache.commons.codec.digest.DigestUtils; 023import org.apache.commons.io.IOUtils; 024import org.nuxeo.ecm.core.api.Blob; 025import org.nuxeo.ecm.core.api.NuxeoException; 026 027/** 028 * Base class for {@link BlobHolder} implementers 029 */ 030public abstract class AbstractBlobHolder implements BlobHolder { 031 032 @Override 033 public abstract Blob getBlob(); 034 035 @Override 036 public void setBlob(Blob blob) { 037 throw new UnsupportedOperationException(); 038 } 039 040 @Override 041 public List<Blob> getBlobs() { 042 List<Blob> blobs = null; 043 044 Blob blob = getBlob(); 045 if (blob != null) { 046 blobs = new ArrayList<Blob>(); 047 blobs.add(blob); 048 } 049 050 return blobs; 051 } 052 053 protected abstract String getBasePath(); 054 055 @Override 056 public String getFilePath() { 057 String path = getBasePath(); 058 059 Blob blob = getBlob(); 060 if (blob != null) { 061 path = path + "/" + blob.getFilename(); 062 } 063 064 return path; 065 } 066 067 @Override 068 public String getHash() { 069 070 Blob blob = getBlob(); 071 if (blob != null) { 072 String h = blob.getDigest(); 073 if (h == null) { 074 h = getMD5Digest(); 075 blob.setDigest(h); 076 } 077 return h; 078 } 079 return "NullBlob"; 080 } 081 082 protected String getMD5Digest() { 083 try (InputStream in = getBlob().getStream()) { 084 return DigestUtils.md5Hex(in); 085 } catch (IOException e) { 086 throw new NuxeoException(e); 087 } 088 } 089 090 @Override 091 public abstract Calendar getModificationDate(); 092 093}