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