001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 */
013package org.nuxeo.ecm.core.api.blobholder;
014
015import java.io.IOException;
016import java.util.HashMap;
017import java.util.Map;
018import java.util.Set;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.PropertyException;
025import org.nuxeo.ecm.core.api.adapter.DocumentAdapterFactory;
026import org.nuxeo.ecm.core.api.externalblob.ExternalBlobAdapter;
027import org.nuxeo.ecm.core.api.externalblob.ExternalBlobAdapterDescriptor;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.model.ComponentInstance;
030import org.nuxeo.runtime.model.DefaultComponent;
031
032/**
033 * Runtime component to manage the pluggable factory for {@link DocumentAdapterFactory}.
034 * <p>
035 * Also provides the service interface {@link BlobHolderAdapterService}
036 *
037 * @author tiry
038 */
039public class BlobHolderAdapterComponent extends DefaultComponent implements BlobHolderAdapterService {
040
041    private static final Log log = LogFactory.getLog(BlobHolderAdapterComponent.class);
042
043    public static final String BLOBHOLDERFACTORY_EP = "BlobHolderFactory";
044
045    public static final String EXTERNALBLOB_ADAPTER_EP = "ExternalBlobAdapter";
046
047    protected final Map<String, BlobHolderFactory> factories = new HashMap<String, BlobHolderFactory>();
048
049    protected Map<String, BlobHolderFactory> factoriesByFacets = new HashMap<String, BlobHolderFactory>();
050
051    protected static final Map<String, ExternalBlobAdapter> externalBlobAdapters = new HashMap<String, ExternalBlobAdapter>();
052
053    @Override
054    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
055
056        if (BLOBHOLDERFACTORY_EP.equals(extensionPoint)) {
057            BlobHolderFactoryDescriptor desc = (BlobHolderFactoryDescriptor) contribution;
058            String docType = desc.getDocType();
059            if (docType != null) {
060                factories.put(docType, desc.getFactory());
061            }
062            String facet = desc.getFacet();
063            if (facet != null) {
064                factoriesByFacets.put(facet, desc.getFactory());
065            }
066        } else if (EXTERNALBLOB_ADAPTER_EP.equals(extensionPoint)) {
067            ExternalBlobAdapterDescriptor desc = (ExternalBlobAdapterDescriptor) contribution;
068            ExternalBlobAdapter adapter = desc.getAdapter();
069            String prefix = desc.getPrefix();
070            if (externalBlobAdapters.containsKey(prefix)) {
071                log.info(String.format("Overriding external blob adapter with prefix '%s'", prefix));
072                externalBlobAdapters.remove(prefix);
073            }
074            adapter.setPrefix(desc.getPrefix());
075            adapter.setProperties(desc.getProperties());
076            externalBlobAdapters.put(desc.getPrefix(), adapter);
077            log.info(String.format("Registered external blob adapter with prefix '%s'", prefix));
078        } else {
079            log.error("Unknown extension point " + extensionPoint);
080        }
081    }
082
083    @Override
084    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
085    }
086
087    /* for test */
088
089    public static Set<String> getFactoryNames() {
090        return ((BlobHolderAdapterComponent) Framework.getLocalService(BlobHolderAdapterService.class)).factories.keySet();
091    }
092
093    /* Service Interface */
094
095    @Override
096    public ExternalBlobAdapter getExternalBlobAdapterForPrefix(String prefix) {
097        return externalBlobAdapters.get(prefix);
098    }
099
100    @Override
101    public ExternalBlobAdapter getExternalBlobAdapterForUri(String uri) {
102        if (uri != null && uri.contains(ExternalBlobAdapter.PREFIX_SEPARATOR)) {
103            String prefix = uri.substring(0, uri.indexOf(ExternalBlobAdapter.PREFIX_SEPARATOR));
104            return getExternalBlobAdapterForPrefix(prefix);
105        }
106        return null;
107    }
108
109    @Override
110    public Blob getExternalBlobForUri(String uri) throws PropertyException, IOException {
111        ExternalBlobAdapter adapter = getExternalBlobAdapterForUri(uri);
112        if (adapter == null) {
113            throw new PropertyException(String.format("No external blob adapter found for uri '%s'", uri));
114        }
115        return adapter.getBlob(uri);
116    }
117
118    @Override
119    public BlobHolder getBlobHolderAdapter(DocumentModel doc) {
120        if (factories.containsKey(doc.getType())) {
121            BlobHolderFactory factory = factories.get(doc.getType());
122            return factory.getBlobHolder(doc);
123        }
124
125        for (Map.Entry<String, BlobHolderFactory> entry : factoriesByFacets.entrySet()) {
126            if (doc.hasFacet(entry.getKey())) {
127                return entry.getValue().getBlobHolder(doc);
128            }
129        }
130
131        if (doc.hasSchema("file")) {
132            return new DocumentBlobHolder(doc, "file:content", "file:filename");
133        } else if (doc.hasSchema("note")) {
134            String mt = null;
135            try {
136                mt = (String) doc.getPropertyValue("note:mime_type");
137                if (mt == null) {
138                    String note = (String) doc.getPropertyValue("note:note");
139                    if (note != null && !"".equals(note)) {
140                        mt = "text/plain"; // BBB
141                    }
142                }
143            } catch (PropertyException e) {
144                // mt = null;
145            }
146            return new DocumentStringBlobHolder(doc, "note:note", mt);
147        }
148        return null;
149    }
150
151}