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