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