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    protected String prefix;
038
039    protected Map<String, String> properties;
040
041    @Override
042    public String getPrefix() {
043        return prefix;
044    }
045
046    @Override
047    public void setPrefix(String prefix) {
048        this.prefix = prefix;
049    }
050
051    @Override
052    public Map<String, String> getProperties() {
053        // de-reference
054        if (properties == null) {
055            return Collections.emptyMap();
056        }
057        return Collections.unmodifiableMap(properties);
058    }
059
060    @Override
061    public String getProperty(String name) {
062        Map<String, String> props = getProperties();
063        String prop = props.get(name);
064        prop = prop.trim();
065        return prop;
066    }
067
068    @Override
069    public void setProperties(Map<String, String> properties) {
070        this.properties = properties;
071    }
072
073    public String getLocalName(String uri) throws PropertyException {
074        String prefix = getPrefix();
075        if (prefix == null) {
076            throw new PropertyException(String.format("Null prefix on external blob adapter with class '%s'",
077                    getClass().getName()));
078        }
079        if (uri == null || !uri.startsWith(prefix + ExternalBlobAdapter.PREFIX_SEPARATOR)) {
080            throw new PropertyException(String.format(
081                    "Invalid uri '%s' for this adapter: expected to start with prefix '%s'", uri, prefix));
082        }
083        return uri.substring(prefix.length() + 1);
084    }
085}