001/*
002 *  Copyright 2018 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.core.ui.resources.css;
017
018import java.net.URISyntaxException;
019import java.util.Objects;
020
021import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
022
023/**
024 * Extension point for resource URIs supported by JSASS
025 */
026public class JSASSResourceURIExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<JSASSResourceURI> 
027{
028    /** avalon role */
029    public static final String ROLE = JSASSResourceURIExtensionPoint.class.getName();
030    
031    /**
032     * Resolve the URI, or returns the original uri if it is not supported
033     * @param uri The URI
034     * @return The resolved URI
035     * @throws URISyntaxException If the URI is not supported
036     */
037    public String resolve(String uri) throws URISyntaxException
038    {
039        for (String extensionId : getExtensionsIds())
040        {
041            JSASSResourceURI extension = getExtension(extensionId);
042            String resolvedUri = extension.resolve(uri);
043            if (resolvedUri != null)
044            {
045                return resolvedUri;
046            }
047        }
048        return uri;
049    }
050    
051    /**
052     * Resolve a path and return the URI corresponding, or null if the path is not supported
053     * @param path The path
054     * @return The URI
055     */
056    public String resolvePath(String path)
057    {
058        String result = getExtensionsIds()
059                .stream()
060                .map(this::getExtension)
061                .map(ext -> ext.resolvePath(path))
062                .filter(Objects::nonNull)
063                .findFirst()
064                .orElse(null);
065        if (result != null)
066        {
067            return result;
068        }
069        return path.contains("://") ? path : "cocoon:/" + path;
070    }
071    
072}