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