001/*
002 * (C) Copyright 2017 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 *     bdelbosc
018 */
019package org.nuxeo.importer.stream.consumer;
020
021import java.io.File;
022import java.io.IOException;
023import java.nio.file.Files;
024import java.nio.file.Path;
025
026import org.apache.commons.lang3.StringUtils;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
030import org.nuxeo.ecm.core.api.impl.blob.StringBlob;
031import org.nuxeo.ecm.core.blob.BlobInfo;
032import org.nuxeo.ecm.core.blob.BlobManager;
033import org.nuxeo.ecm.core.blob.BlobProvider;
034import org.nuxeo.importer.stream.message.BlobMessage;
035import org.nuxeo.lib.stream.pattern.consumer.AbstractConsumer;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Import BlobMessage into a Nuxeo BlobProvider, persist BlobInformation.
040 *
041 * @since 9.1
042 */
043public class BlobMessageConsumer extends AbstractConsumer<BlobMessage> {
044    private static final org.apache.commons.logging.Log log = LogFactory.getLog(BlobMessageConsumer.class);
045
046    protected BlobProvider blobProvider;
047
048    protected final String blobProviderName;
049
050    protected final BlobInfoWriter blobInfoWriter;
051
052    public BlobMessageConsumer(String consumerId, String blobProviderName, BlobInfoWriter blobInfoWriter) {
053        super(consumerId);
054        this.blobProviderName = blobProviderName;
055        if (!StringUtils.isBlank(blobProviderName)) {
056            blobProvider = Framework.getService(BlobManager.class).getBlobProvider(blobProviderName);
057            if (blobProvider == null) {
058                throw new IllegalArgumentException("Invalid blob provider: " + blobProviderName);
059            }
060        }
061        // when there is no blob provider we don't upload the blobs
062        this.blobInfoWriter = blobInfoWriter;
063    }
064
065    @Override
066    public void begin() {
067        // no batching used
068    }
069
070    @Override
071    public void accept(BlobMessage message) {
072        try (CloseableBlob blob = getBlob(message)) {
073            String digest = null;
074            if (blobProvider != null) {
075                digest = blobProvider.writeBlob(blob.getBlob());
076            }
077            long length = blob.getBlob().getLength();
078            saveBlobInfo(message, digest, length, blob.getBlob().getFile());
079        } catch (IOException e) {
080            throw new IllegalArgumentException("Invalid blob: " + message, e);
081        }
082    }
083
084    protected CloseableBlob getBlob(BlobMessage message) {
085        Blob blob;
086        if (message.getPath() != null) {
087            blob = new FileBlob(new File(message.getPath()));
088        } else {
089            // we don't submit filename or encoding this is not saved in the binary store but in the document
090            blob = new StringBlob(message.getContent(), null, null, null);
091        }
092        return new CloseableBlob(blob);
093    }
094
095    protected void saveBlobInfo(BlobMessage message, String digest, long length, File blobFile) {
096        BlobInfo bi = new BlobInfo();
097        bi.digest = digest;
098        bi.key = blobProviderName + ":" + bi.digest;
099        bi.length = length;
100        if (digest == null) {
101            // the blob is not uploaded use the blob info to pass the file path
102            bi.filename = blobFile.getAbsolutePath();
103        } else {
104            bi.filename = message.getFilename();
105        }
106        bi.mimeType = message.getMimeType();
107        bi.encoding = message.getEncoding();
108        blobInfoWriter.save(null, bi);
109    }
110
111    @Override
112    public void commit() {
113        // no batching used
114    }
115
116    @Override
117    public void rollback() {
118        // no batching used
119    }
120
121    public class CloseableBlob implements AutoCloseable {
122        protected final Blob blob;
123
124        protected final Path fileToDelete;
125
126        public CloseableBlob(Blob blob) {
127            this(blob, null);
128        }
129
130        public CloseableBlob(Blob blob, Path fileToDelete) {
131            this.blob = blob;
132            this.fileToDelete = fileToDelete;
133        }
134
135        public Blob getBlob() {
136            return blob;
137        }
138
139        @Override
140        public void close() {
141            if (fileToDelete != null) {
142                try {
143                    Files.delete(fileToDelete);
144                } catch (IOException e) {
145                    log.warn("Unable to delete file: " + fileToDelete, e);
146                }
147            }
148        }
149    }
150}