001/*
002 *  Copyright 2014 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 */
016
017package org.ametys.odf.source;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.excalibur.source.Source;
027import org.apache.excalibur.source.SourceResolver;
028
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030
031/**
032 * Default implementation of {@link ODFViewSelector}.
033 * First looks the wanted file in context://WEB-INF/stylesheets/path_to_file
034 * If it's not present, it will look in the ODF plugin.
035 */
036public class DefaultODFView extends AbstractLogEnabled implements ODFViewSelector, Serviceable
037{
038    
039    /** The source resolver */
040    protected SourceResolver _resolver;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
046    }
047    
048    @Override
049    public Source getSource(String pluginName, String filePath) throws IOException
050    {
051        for (String location : getLocations(pluginName, filePath))
052        {
053            try
054            {
055                Source source = _resolver.resolveURI(location);
056                if (!source.exists())
057                {
058                    getLogger().debug("Failed to find a stylesheet at '{}'.", location);
059                }
060                else
061                {
062                    getLogger().debug("Using source located at '{}'.", location);
063                    return source;
064                }
065            }
066            catch (IOException e)
067            {
068                getLogger().debug("Resolving protocol failed for resolving '{}'.", location);
069            }
070        }
071        
072        // Should never occur because of the default stylesheet.
073        throw new IOException("Can't find a stylesheet for: " + filePath);
074    }
075    
076    /**
077     * Returns the ordered list of URI to be tested to find the corresponding view.
078     * @param pluginName the plugin name
079     * @param filePath the requested location.
080     * @return a list of possible URIs for the resource.
081     */
082    protected List<String> getLocations(String pluginName, String filePath)
083    {
084        ArrayList<String> locations = new ArrayList<>();
085        
086        // First search in the WEB-INF folder.
087        locations.add("context://WEB-INF/" + filePath);
088        
089        // Then look in the current plugin if not null
090        if (pluginName != null)
091        {
092            locations.add("plugin:" + pluginName + "://" + filePath);
093        }
094        
095        locations.addAll(_getDefaultSourceURIs(pluginName, filePath));
096        
097        return locations;
098    }
099    
100    /**
101     * Get the default URIs
102     * @param pluginName The current plugin name
103     * @param filePath the requested location.
104     * @return a list of URIs
105     */
106    protected List<String> _getDefaultSourceURIs(String pluginName, String filePath)
107    {
108        List<String> locations = new ArrayList<>();
109        
110        // For legacy, look in CMS plugin
111        if (!"odf".equals(pluginName))
112        {
113            locations.add("plugin:odf://" + filePath);
114        }
115        
116        return locations;
117    }
118}