001/* 002 * (C) Copyright 2015 Nuxeo SA (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-2.1.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 * Florent Guillaume 016 */ 017package org.nuxeo.ecm.core.blob; 018 019import java.io.IOException; 020import java.io.InputStream; 021 022import org.nuxeo.ecm.core.api.impl.blob.AbstractBlob; 023import org.nuxeo.ecm.core.blob.BlobManager.BlobInfo; 024import org.nuxeo.runtime.api.Framework; 025 026/** 027 * Simple managed blob implementation holding just a key and delegating to its provider for implementation. 028 * 029 * @since 7.2 030 */ 031public class SimpleManagedBlob extends AbstractBlob implements ManagedBlob { 032 033 private static final long serialVersionUID = 1L; 034 035 public final String key; 036 037 public Long length; 038 039 public SimpleManagedBlob(BlobInfo blobInfo) { 040 this.key = blobInfo.key; 041 setMimeType(blobInfo.mimeType); 042 setEncoding(blobInfo.encoding); 043 setFilename(blobInfo.filename); 044 setDigest(blobInfo.digest); 045 length = blobInfo.length; 046 } 047 048 @Override 049 public String getKey() { 050 return key; 051 } 052 053 @Override 054 public String getProviderId() { 055 int colon = key.indexOf(':'); 056 if (colon < 0) { 057 // no prefix 058 throw new IllegalArgumentException("Invalid managed blob key: " + key); 059 } 060 return key.substring(0, colon); 061 } 062 063 @Override 064 public InputStream getStream() throws IOException { 065 return Framework.getService(BlobManager.class).getStream(this); 066 } 067 068 @Override 069 public long getLength() { 070 return length == null ? -1 : length.longValue(); 071 } 072 073}