001/* 002 * (C) Copyright 2006-2008 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 * Alexandre Russel 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.annotations.service; 023 024import java.util.List; 025 026/** 027 * @author Alexandre Russel 028 */ 029public class URLPatternFilter { 030 031 private final boolean allowDeny; 032 033 private final List<String> denies; 034 035 private final List<String> allows; 036 037 public URLPatternFilter(boolean allowDeny, List<String> denies, List<String> allows) { 038 this.allowDeny = allowDeny; 039 this.denies = denies; 040 this.allows = allows; 041 } 042 043 public boolean allow(String url) { 044 if (checkFirstPass(url) && !checkSecondPass(url)) { 045 return allowDeny; 046 } 047 return !allowDeny; 048 } 049 050 private boolean checkSecondPass(String url) { 051 return checkMatch(allowDeny ? denies : allows, url); 052 } 053 054 private static boolean checkMatch(List<String> list, String url) { 055 for (String regex : list) { 056 if (url.matches(regex)) { 057 return true; 058 } 059 } 060 return false; 061 } 062 063 private boolean checkFirstPass(String url) { 064 return checkMatch(allowDeny ? allows : denies, url); 065 } 066 067}