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 *     Anahide Tchertchian
011 */
012package org.nuxeo.ecm.core.api.externalblob;
013
014import java.util.Collections;
015import java.util.Map;
016
017import org.nuxeo.ecm.core.api.PropertyException;
018
019/**
020 * Abstract class for external blob adapters.
021 * <p>
022 * Provides generic methods
023 * <p>
024 * Extend this class if you want your contributions to be robust to interface changes.
025 *
026 * @author Anahide Tchertchian
027 */
028public abstract class AbstractExternalBlobAdapter implements ExternalBlobAdapter {
029
030    protected String prefix;
031
032    protected Map<String, String> properties;
033
034    @Override
035    public String getPrefix() {
036        return prefix;
037    }
038
039    @Override
040    public void setPrefix(String prefix) {
041        this.prefix = prefix;
042    }
043
044    @Override
045    public Map<String, String> getProperties() {
046        // de-reference
047        if (properties == null) {
048            return Collections.emptyMap();
049        }
050        return Collections.unmodifiableMap(properties);
051    }
052
053    @Override
054    public String getProperty(String name) {
055        Map<String, String> props = getProperties();
056        String prop = props.get(name);
057        prop = prop.trim();
058        return prop;
059    }
060
061    @Override
062    public void setProperties(Map<String, String> properties) {
063        this.properties = properties;
064    }
065
066    public String getLocalName(String uri) throws PropertyException {
067        String prefix = getPrefix();
068        if (prefix == null) {
069            throw new PropertyException(String.format("Null prefix on external blob adapter with class '%s'",
070                    getClass().getName()));
071        }
072        if (uri == null || !uri.startsWith(prefix + ExternalBlobAdapter.PREFIX_SEPARATOR)) {
073            throw new PropertyException(String.format(
074                    "Invalid uri '%s' for this adapter: expected to start with prefix '%s'", uri, prefix));
075        }
076        return uri.substring(prefix.length() + 1);
077    }
078}