001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.web.common.ajax.service;
021
022import java.util.HashMap;
023import java.util.Map;
024import java.util.concurrent.locks.ReentrantReadWriteLock;
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.platform.web.common.requestcontroller.service.LRUCachingMap;
031import org.nuxeo.runtime.model.ComponentInstance;
032import org.nuxeo.runtime.model.DefaultComponent;
033
034/**
035 * Simple Runtime component to manage proxyable url configuration via Extension Points
036 *
037 * @author tiry
038 */
039public class AjaxProxyComponent extends DefaultComponent implements AjaxProxyService {
040
041    public static final String PROXY_URL_EP = "proxyableURL";
042
043    protected static final Map<String, ProxyableURLDescriptor> urlDescriptors = new HashMap<String, ProxyableURLDescriptor>();
044
045    protected static final Map<String, ProxyURLConfigEntry> urlCache = new LRUCachingMap<String, ProxyURLConfigEntry>(
046            250);
047
048    protected static final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock();
049
050    private static final Log log = LogFactory.getLog(AjaxProxyComponent.class);
051
052    @Override
053    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
054        if (PROXY_URL_EP.equals(extensionPoint)) {
055            ProxyableURLDescriptor desc = (ProxyableURLDescriptor) contribution;
056            registerProxyURL(desc);
057        } else {
058            log.error("Unknown ExtensionPoint " + extensionPoint);
059        }
060    }
061
062    protected void registerProxyURL(ProxyableURLDescriptor desc) {
063        if (urlDescriptors.containsKey(desc.getName())) {
064            urlDescriptors.get(desc.getName()).merge(desc);
065        } else {
066            urlDescriptors.put(desc.getName(), desc);
067        }
068    }
069
070    public ProxyURLConfigEntry getConfigForURL(String targetUrl) {
071        ProxyURLConfigEntry entry = null;
072
073        try {
074            cacheLock.readLock().lock();
075            entry = urlCache.get(targetUrl);
076        } finally {
077            cacheLock.readLock().unlock();
078        }
079        if (entry == null) {
080            entry = computeConfigForURL(targetUrl);
081            try {
082                cacheLock.writeLock().lock();
083                urlCache.put(targetUrl, entry);
084            } finally {
085                cacheLock.writeLock().unlock();
086            }
087        }
088        return entry;
089    }
090
091    public ProxyURLConfigEntry computeConfigForURL(String targetUrl) {
092        for (ProxyableURLDescriptor desc : urlDescriptors.values()) {
093            if (desc.isEnabled()) {
094                Pattern pat = desc.getCompiledPattern();
095                Matcher m = pat.matcher(targetUrl);
096                if (m.matches()) {
097                    return new ProxyURLConfigEntry(true, desc);
098                }
099            }
100        }
101        // return deny by default
102        return new ProxyURLConfigEntry();
103    }
104
105}