001/*
002 *  Copyright 2020 Anyware Services
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 */
016package org.ametys.plugins.linkdirectory;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.avalon.framework.thread.ThreadSafe;
026import org.apache.excalibur.source.Source;
027import org.apache.excalibur.source.SourceFactory;
028
029import org.ametys.cms.data.Binary;
030import org.ametys.cms.data.BinarySource;
031import org.ametys.core.util.FilenameUtils;
032import org.ametys.plugins.linkdirectory.repository.DefaultLink;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036/**
037 * {@link SourceFactory} for link data.
038 */
039public class LinkSourceFactory extends AbstractLogEnabled implements SourceFactory, ThreadSafe, Serviceable
040{
041    /** source scheme */
042    public static final String SCHEME = "link";
043    
044    private AmetysObjectResolver _resolver;
045    
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    public Source getSource(String location, Map parameters) throws IOException, MalformedURLException
052    {
053        String uri = location.substring((SCHEME + "://").length());
054        
055        // uri are like <contentPath>|<attributePath>;<filename>
056        int i = uri.indexOf('|');
057        int j = uri.indexOf(';', i);
058        String path = uri.substring(0, i);
059        String attribute = uri.substring(i + 1, j);
060        String filename = uri.substring(j + 1);
061        
062        try
063        {
064            DefaultLink link = _resolver.resolveByPath(FilenameUtils.decode(path));
065            
066            Binary binary = link.getValue(attribute);
067    
068            return new BinarySource(binary, FilenameUtils.decode(filename), location, SCHEME);
069        }
070        catch (Exception e)
071        {
072            getLogger().error("Unable to resolve binary attribute for uri " + location, e);
073            
074            // returns an unexisting Source
075            return new BinarySource(null, FilenameUtils.decode(filename), location, SCHEME);
076        }
077    }
078
079    public void release(Source source)
080    {
081        // nothing to do
082    }
083}