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.regex.Matcher;
020import java.util.regex.Pattern;
021
022import org.apache.commons.lang3.StringUtils;
023
024import org.ametys.runtime.plugin.component.PluginAware;
025
026/**
027 * JSASS Resource URI for plugins URI
028 */
029public class PluginsJSASSResourceURI implements JSASSResourceURI, PluginAware
030{
031    private static final Pattern URI_SUPPORTED_PATTERN = Pattern.compile("^plugin(?::([^:]+))?://(resources/.*)$");
032    private static final Pattern PATH_SUPPORTED_PATTERN = Pattern.compile("^/plugins/([^/]+)/(resources/.*)$");
033    private String _pluginName;
034
035    @Override
036    public void setPluginInfo(String pluginName, String featureName, String id)
037    {
038        _pluginName = pluginName;
039    }
040    
041    @Override
042    public String resolve(String uri) throws URISyntaxException
043    {
044        Matcher matcher = URI_SUPPORTED_PATTERN.matcher(uri);
045        if (matcher.matches())
046        {
047            String target = matcher.group(1);
048            if (StringUtils.isEmpty(target))
049            {
050                target = _pluginName;
051            }
052            String path = matcher.group(2);
053            
054            StringBuilder relativeSb = new StringBuilder();
055            relativeSb.append("/plugins/");
056            relativeSb.append(target);
057            relativeSb.append("/");
058            relativeSb.append(path);
059            
060            return relativeSb.toString();
061        }
062        return null;
063    }
064
065    @Override
066    public String resolvePath(String path)
067    {
068        Matcher matcher = PATH_SUPPORTED_PATTERN.matcher(path);
069        if (matcher.matches())
070        {
071            StringBuilder uri = new StringBuilder();
072            uri.append("plugin:");
073            uri.append(matcher.group(1));
074            uri.append("://");
075            uri.append(matcher.group(2));
076            return uri.toString();
077        }
078        return null;
079    }
080
081}