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.web.minimize; 017 018import java.net.URISyntaxException; 019import java.util.Optional; 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 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.commons.lang3.StringUtils; 027 028import org.ametys.plugins.core.ui.resources.css.JSASSResourceURI; 029import org.ametys.web.skin.SkinsManager; 030 031/** 032 * JSASS Resource URI for skins URI 033 */ 034public class SkinsJSASSResourceURI implements JSASSResourceURI, Serviceable 035{ 036 private static final Pattern URI_SUPPORTED_PATTERN = Pattern.compile("^skin(?::([^:]+)(?:#([^:]+))?)?://(.*)$"); 037 private static final Pattern PATH_SUPPORTED_PATTERN = Pattern.compile("^/skins/([^/]+)/(?!templates/)(.*)$"); 038 039 private SkinsManager _skinsManager; 040 041 public void service(ServiceManager manager) throws ServiceException 042 { 043 _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 044 } 045 046 @Override 047 public String resolve(String uri) throws URISyntaxException 048 { 049 Matcher matcher = URI_SUPPORTED_PATTERN.matcher(uri); 050 if (matcher.matches()) 051 { 052 String skin = _getSkin(matcher.group(1), matcher.group(2)); 053 if (skin == null) 054 { 055 return null; 056 } 057 058 String path = matcher.group(3); 059 060 StringBuilder relativeSb = new StringBuilder(); 061 relativeSb.append("/skins/"); 062 relativeSb.append(skin); 063 relativeSb.append("/"); 064 relativeSb.append(path); 065 066 return relativeSb.toString(); 067 } 068 return null; 069 } 070 071 /** 072 * Get the skin 073 * @param skin The skin 074 * @param defaultValue default value if no skin specified and no valid skin found 075 * @return The skin name 076 */ 077 protected String _getSkin(String skin, String defaultValue) 078 { 079 String result = skin; 080 if (StringUtils.isEmpty(result)) 081 { 082 result = _skinsManager.getSkinNameFromRequest(); 083 084 if (StringUtils.isEmpty(result)) 085 { 086 result = Optional.ofNullable(defaultValue) 087 .map(val -> _skinsManager.getSkin(val)) 088 .map(s -> s.getId()) 089 .orElse(null); 090 } 091 } 092 093 return skin; 094 } 095 096 @Override 097 public String resolvePath(String path) 098 { 099 Matcher matcher = PATH_SUPPORTED_PATTERN.matcher(path); 100 if (matcher.matches()) 101 { 102 StringBuilder uri = new StringBuilder(); 103 uri.append("skin:"); 104 uri.append(matcher.group(1)); 105 uri.append("://"); 106 uri.append(matcher.group(2)); 107 return uri.toString(); 108 } 109 return null; 110 } 111 112}