001/* 002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.web.common.ajax.service; 023 024import java.util.HashMap; 025import java.util.Map; 026import java.util.concurrent.locks.ReentrantReadWriteLock; 027import java.util.regex.Matcher; 028import java.util.regex.Pattern; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.ecm.platform.web.common.requestcontroller.service.LRUCachingMap; 033import org.nuxeo.runtime.model.ComponentInstance; 034import org.nuxeo.runtime.model.DefaultComponent; 035 036/** 037 * Simple Runtime component to manage proxyable url configuration via Extension Points 038 * 039 * @author tiry 040 */ 041public class AjaxProxyComponent extends DefaultComponent implements AjaxProxyService { 042 043 public static final String PROXY_URL_EP = "proxyableURL"; 044 045 protected static final Map<String, ProxyableURLDescriptor> urlDescriptors = new HashMap<String, ProxyableURLDescriptor>(); 046 047 protected static final Map<String, ProxyURLConfigEntry> urlCache = new LRUCachingMap<String, ProxyURLConfigEntry>( 048 250); 049 050 protected static final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock(); 051 052 private static final Log log = LogFactory.getLog(AjaxProxyComponent.class); 053 054 @Override 055 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 056 if (PROXY_URL_EP.equals(extensionPoint)) { 057 ProxyableURLDescriptor desc = (ProxyableURLDescriptor) contribution; 058 registerProxyURL(desc); 059 } else { 060 log.error("Unknown ExtensionPoint " + extensionPoint); 061 } 062 } 063 064 protected void registerProxyURL(ProxyableURLDescriptor desc) { 065 if (urlDescriptors.containsKey(desc.getName())) { 066 urlDescriptors.get(desc.getName()).merge(desc); 067 } else { 068 urlDescriptors.put(desc.getName(), desc); 069 } 070 } 071 072 public ProxyURLConfigEntry getConfigForURL(String targetUrl) { 073 ProxyURLConfigEntry entry = null; 074 075 try { 076 cacheLock.readLock().lock(); 077 entry = urlCache.get(targetUrl); 078 } finally { 079 cacheLock.readLock().unlock(); 080 } 081 if (entry == null) { 082 entry = computeConfigForURL(targetUrl); 083 try { 084 cacheLock.writeLock().lock(); 085 urlCache.put(targetUrl, entry); 086 } finally { 087 cacheLock.writeLock().unlock(); 088 } 089 } 090 return entry; 091 } 092 093 public ProxyURLConfigEntry computeConfigForURL(String targetUrl) { 094 for (ProxyableURLDescriptor desc : urlDescriptors.values()) { 095 if (desc.isEnabled()) { 096 Pattern pat = desc.getCompiledPattern(); 097 Matcher m = pat.matcher(targetUrl); 098 if (m.matches()) { 099 return new ProxyURLConfigEntry(true, desc); 100 } 101 } 102 } 103 // return deny by default 104 return new ProxyURLConfigEntry(); 105 } 106 107}