001/* 002 * (C) Copyright 2013 Nuxeo SA (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-2.1.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 * Arnaud Kervern 016 */ 017 018package org.nuxeo.ecm.platform.web.common.requestcontroller.service; 019 020import java.util.HashMap; 021import java.util.Map; 022import java.util.regex.Pattern; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.runtime.model.ContributionFragmentRegistry; 027 028/** 029 * Nuxeo Cors filter description registry model. 030 * 031 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 032 * @since 5.7.2 033 */ 034public class NuxeoCorsFilterDescriptorRegistry extends ContributionFragmentRegistry<NuxeoCorsFilterDescriptor> { 035 036 protected Map<String, NuxeoCorsFilterDescriptor> descs = new HashMap<>(); 037 038 @Override 039 public String getContributionId(NuxeoCorsFilterDescriptor contrib) { 040 return contrib.name; 041 } 042 043 @Override 044 public void contributionUpdated(String id, NuxeoCorsFilterDescriptor contrib, 045 NuxeoCorsFilterDescriptor newOrigContrib) { 046 if (descs.containsKey(id)) { 047 descs.remove(id); 048 } 049 050 if (contrib.enabled) { 051 descs.put(id, contrib); 052 } 053 } 054 055 @Override 056 public void contributionRemoved(String id, NuxeoCorsFilterDescriptor origContrib) { 057 if (descs.containsKey(id)) { 058 descs.remove(id); 059 } 060 } 061 062 @Override 063 public NuxeoCorsFilterDescriptor clone(NuxeoCorsFilterDescriptor orig) { 064 try { 065 return orig.clone(); 066 } catch (CloneNotSupportedException e) { 067 // Should never happens... 068 return null; 069 } 070 } 071 072 @Override 073 public void merge(NuxeoCorsFilterDescriptor src, NuxeoCorsFilterDescriptor dst) { 074 dst.merge(src); 075 } 076 077 public NuxeoCorsFilterDescriptor getFirstMatchingDescriptor(String uri) { 078 for (NuxeoCorsFilterDescriptor filterDesc : descs.values()) { 079 Pattern pattern = Pattern.compile(filterDesc.pattern); 080 if (pattern.matcher(uri).matches()) { 081 return filterDesc; 082 } 083 } 084 return null; 085 } 086}