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.cms.explorer;
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.commons.lang3.StringUtils;
027import org.apache.excalibur.source.Source;
028import org.apache.excalibur.source.SourceFactory;
029
030import org.ametys.core.util.FilenameUtils;
031import org.ametys.plugins.explorer.resources.Resource;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036/**
037 * {@link SourceFactory} for {@link Resource}.
038 */
039public class AmetysResourceSourceFactory extends AbstractLogEnabled implements SourceFactory, ThreadSafe, Serviceable
040{
041    /** source scheme */
042    public static final String RESOURCE_SCHEME = "ametys-resource";
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((RESOURCE_SCHEME + "://").length());
054        
055        // uri are like <version>@<resourcePath>
056        int i = uri.indexOf('@');
057        String version = i == -1 ? null : uri.substring(0, i);
058        String path = uri.substring(i + 1);
059        
060        path = FilenameUtils.decode(path);
061        String filename = org.apache.commons.io.FilenameUtils.getName(path);
062        
063        try
064        {
065            Resource resource = _resolver.resolveByPath(path);
066            
067            if (!StringUtils.isEmpty(version))
068            {
069                ((VersionAwareAmetysObject) resource).switchToRevision(version);
070            }
071    
072            return new AmetysResourceSource(resource, filename, location);
073        }
074        catch (Exception e)
075        {
076            getLogger().debug("Unable to resolve resource for uri {}", location, e);
077            
078            // returns an unexisting Source
079            return new AmetysResourceSource(null, filename, location);
080        }
081    }
082
083    public void release(Source source)
084    {
085        // nothing to do
086    }
087}