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;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.Arrays;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.concurrent.atomic.AtomicLong;
029
030import org.apache.commons.io.IOUtils;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.blob.AbstractBlobProvider;
033import org.nuxeo.ecm.core.blob.BlobInfo;
034import org.nuxeo.ecm.core.blob.ManagedBlob;
035import org.nuxeo.ecm.core.blob.SimpleManagedBlob;
036import org.nuxeo.ecm.core.blob.apps.AppLink;
037
038/**
039 * Dummy storage in memory.
040 */
041public class DummyBlobProvider extends AbstractBlobProvider {
042
043    protected Map<String, byte[]> blobs;
044
045    protected AtomicLong counter;
046
047    @Override
048    public void initialize(String blobProviderId, Map<String, String> properties) throws IOException {
049        super.initialize(blobProviderId, properties);
050        blobs = new HashMap<>();
051        counter = new AtomicLong();
052    }
053
054    @Override
055    public void close() {
056        blobs.clear();
057    }
058
059    @Override
060    public Blob readBlob(BlobInfo blobInfo) {
061        return new SimpleManagedBlob(blobInfo) {
062            private static final long serialVersionUID = 1L;
063
064            @Override
065            public InputStream getStream() throws IOException {
066                int colon = key.indexOf(':');
067                String k = colon < 0 ? key : key.substring(colon + 1);
068                byte[] bytes = blobs.get(k);
069                return new ByteArrayInputStream(bytes);
070            }
071        };
072    }
073
074    @Override
075    public String writeBlob(Blob blob) throws IOException {
076        byte[] bytes;
077        try (InputStream in = blob.getStream()) {
078            bytes = IOUtils.toByteArray(in);
079        }
080        String k = String.valueOf(counter.incrementAndGet());
081        blobs.put(k, bytes);
082        return k;
083    }
084
085    public List<AppLink> getAppLinks(String user, ManagedBlob blob) {
086        AppLink link = new AppLink();
087        link.setAppName("dummyApp");
088        link.setIcon("dummyIcon");
089        link.setLink("dummyLink");
090        return Arrays.asList(link);
091    }
092
093}