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