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