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 org.nuxeo.ecm.core.api.impl.blob.AbstractBlob;
022
023import java.io.File;
024
025/**
026 * Simple managed blob implementation holding just a key and delegating to its provider for implementation.
027 *
028 * @since 7.2
029 */
030public class SimpleManagedBlob extends AbstractBlob implements ManagedBlob {
031
032    private static final long serialVersionUID = 1L;
033
034    public final String blobProviderId;
035
036    public final String key;
037
038    public Long length;
039
040    public SimpleManagedBlob(String blobProviderId, BlobInfo blobInfo) {
041        this.blobProviderId = blobProviderId;
042        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    public SimpleManagedBlob(BlobInfo blobInfo) {
051        this(blobProviderIdFromKey(blobInfo.key), blobInfo);
052    }
053
054    @Override
055    public String getProviderId() {
056        return blobProviderId;
057    }
058
059    @Override
060    public String getKey() {
061        return key;
062    }
063
064    protected static String blobProviderIdFromKey(String key) {
065        int colon = key.indexOf(':');
066        if (colon < 0) {
067            // no prefix
068            throw new IllegalArgumentException("Invalid managed blob key: " + key);
069        }
070        return key.substring(0, colon);
071    }
072
073    @Override
074    public File getFile() {
075        return ManagedBlob.super.getFile();
076    }
077
078    @Override
079    public long getLength() {
080        return length == null ? -1 : length.longValue();
081    }
082
083}