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.binary;
018
019import java.io.IOException;
020import java.util.Map;
021
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.blob.BlobManager;
024import org.nuxeo.ecm.core.blob.BlobManager.BlobInfo;
025import org.nuxeo.ecm.core.blob.BlobProvider;
026import org.nuxeo.ecm.core.model.Document;
027
028/**
029 * Adapter between the {@link BinaryManager} and a {@link BlobProvider} for the {@link BlobManager}.
030 * <p>
031 * Can be used by legacy implementations of a {@link BinaryManager} to provide a {@link BlobProvider} implementation.
032 *
033 * @since 7.3
034 */
035public class BinaryBlobProvider implements BlobProvider {
036
037    protected final BinaryManager binaryManager;
038
039    public BinaryBlobProvider(BinaryManager binaryManager) {
040        this.binaryManager = binaryManager;
041    }
042
043    @Override
044    public void initialize(String blobProviderId, Map<String, String> properties) throws IOException {
045        binaryManager.initialize(blobProviderId, properties);
046    }
047
048    /**
049     * Closes the adapted {@link BinaryManager}.
050     */
051    @Override
052    public void close() {
053        binaryManager.close();
054    }
055
056    @Override
057    public BinaryManager getBinaryManager() {
058        return binaryManager;
059    }
060
061    @Override
062    public Blob readBlob(BlobInfo blobInfo) throws IOException {
063        String digest = blobInfo.key;
064        // strip prefix
065        int colon = digest.indexOf(':');
066        if (colon >= 0) {
067            digest = digest.substring(colon + 1);
068        }
069        Binary binary = binaryManager.getBinary(digest);
070        if (binary == null) {
071            throw new IOException("Unknown binary: " + digest);
072        }
073        return new BinaryBlob(binary, blobInfo.key, blobInfo.filename, blobInfo.mimeType, blobInfo.encoding,
074                blobInfo.digest, binary.getLength()); // use binary length, authoritative
075    }
076
077    @Override
078    public boolean supportsWrite() {
079        return true;
080    }
081
082    @Override
083    public String writeBlob(Blob blob, Document doc) throws IOException {
084        // writes the blob and return its digest
085        return binaryManager.getBinary(blob).getDigest();
086    }
087
088}