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; 020import java.util.function.Function; 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.commons.lang3.StringUtils; 026 027/** 028 * JSASS Resource URI for context URI 029 */ 030public class ContextJSASSResourceURI implements JSASSResourceURI, Serviceable 031{ 032 private SupportedContextResourceUriExtensionPoint _supportedContextResourceUri; 033 034 public void service(ServiceManager manager) throws ServiceException 035 { 036 _supportedContextResourceUri = (SupportedContextResourceUriExtensionPoint) manager.lookup(SupportedContextResourceUriExtensionPoint.ROLE); 037 } 038 039 @Override 040 public String resolve(String uri) throws URISyntaxException 041 { 042 if (!StringUtils.startsWith(uri, "context://")) 043 { 044 return null; 045 } 046 047 Function<SupportedContextResourceUri, String> resolveUri = ext -> ext.resolve(uri); 048 049 String resolvedUri = _supportedContextResourceUri.getExtensionsIds().stream() 050 .map(this::getExtension) 051 .map(resolveUri) 052 .filter(Objects::nonNull) 053 .findFirst() 054 .orElseThrow(() -> new URISyntaxException(uri, "No supported context:// for this uri, a SupportedContextResourceUri extension is required")); 055 056 return resolvedUri; 057 } 058 059 @Override 060 public String resolvePath(String path) 061 { 062 Function<SupportedContextResourceUri, String> resolvePath = ext -> ext.resolvePath(path); 063 064 String resolvedUri = _supportedContextResourceUri.getExtensionsIds().stream() 065 .map(this::getExtension) 066 .map(resolvePath) 067 .filter(Objects::nonNull) 068 .findFirst() 069 .orElse(null); 070 071 return resolvedUri; 072 } 073 074 private SupportedContextResourceUri getExtension(String id) 075 { 076 return _supportedContextResourceUri.getExtension(id); 077 } 078 079}