001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.blob; 020 021import java.io.IOException; 022import java.io.InputStream; 023 024import org.nuxeo.ecm.core.api.impl.blob.AbstractBlob; 025import org.nuxeo.ecm.core.blob.BlobManager.BlobInfo; 026import org.nuxeo.runtime.api.Framework; 027 028/** 029 * Simple managed blob implementation holding just a key and delegating to its provider for implementation. 030 * 031 * @since 7.2 032 */ 033public class SimpleManagedBlob extends AbstractBlob implements ManagedBlob { 034 035 private static final long serialVersionUID = 1L; 036 037 public final String key; 038 039 public Long length; 040 041 public SimpleManagedBlob(BlobInfo blobInfo) { 042 this.key = blobInfo.key; 043 setMimeType(blobInfo.mimeType); 044 setEncoding(blobInfo.encoding); 045 setFilename(blobInfo.filename); 046 setDigest(blobInfo.digest); 047 length = blobInfo.length; 048 } 049 050 @Override 051 public String getKey() { 052 return key; 053 } 054 055 @Override 056 public String getProviderId() { 057 int colon = key.indexOf(':'); 058 if (colon < 0) { 059 // no prefix 060 throw new IllegalArgumentException("Invalid managed blob key: " + key); 061 } 062 return key.substring(0, colon); 063 } 064 065 @Override 066 public InputStream getStream() throws IOException { 067 return Framework.getService(BlobManager.class).getStream(this); 068 } 069 070 @Override 071 public long getLength() { 072 return length == null ? -1 : length.longValue(); 073 } 074 075}