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