2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.filtervalve.cors.model;
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import static com.google.common.collect.Maps.immutableEntry;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.LinkedHashMap;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.TreeMap;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Match incoming URL with user defined patterns according to servlet specification.
26 * In the Web application deployment descriptor, the following syntax is used to define mappings:
28 * <li>A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.</li>
29 * <li>A string beginning with a ‘*.’ prefix is used as an extension mapping.</li>
30 * <li>All other strings are used for exact matches only.</li>
33 public class UrlMatcher<FILTER> {
34 private static final Logger logger = LoggerFactory.getLogger(UrlMatcher.class);
35 // order index for each FILTER is kept as Entry.value
36 private final Map<String, Entry<FILTER, Integer>> prefixMap = new HashMap<>(); // contains patterns ending with '/*', '*' is stripped from each key
37 private final Map<String, Entry<FILTER, Integer>> suffixMap = new HashMap<>(); // contains patterns starting with '*.' prefix, '*' is stripped from each key
38 private final Map<String, Entry<FILTER, Integer>> exactMatchMap = new HashMap<>(); // contains exact matches only
41 * @param patternMap order preserving map containing path info pattern as key
43 public UrlMatcher(LinkedHashMap<String, FILTER> patternMap) {
45 for (Entry<String, FILTER> entry : patternMap.entrySet()) {
47 String pattern = checkNotNull(entry.getKey());
48 FILTER value = entry.getValue();
49 Entry<FILTER, Integer> valueWithIdx = immutableEntry(value, idx);
50 if (pattern.startsWith("/") && pattern.endsWith("/*")) {
51 pattern = pattern.substring(0, pattern.length() - 1);
52 prefixMap.put(pattern, valueWithIdx);
53 } else if (pattern.startsWith("*.")) {
54 pattern = pattern.substring(1);
55 suffixMap.put(pattern, valueWithIdx);
57 exactMatchMap.put(pattern, valueWithIdx);
63 * Find filters matching path
65 * @param pathInfo as returned by request.getPathInfo()
66 * @return list of matching filters
68 public List<FILTER> findMatchingFilters(String pathInfo) {
69 checkNotNull(pathInfo);
70 TreeMap<Integer, FILTER> sortedMap = new TreeMap<>();
71 // add matching prefixes
72 for (Entry<String, Entry<FILTER, Integer>> prefixEntry : prefixMap.entrySet()) {
73 if (pathInfo.startsWith(prefixEntry.getKey())) {
74 put(sortedMap, prefixEntry.getValue());
77 // add matching suffixes
78 for (Entry<String, Entry<FILTER, Integer>> suffixEntry : suffixMap.entrySet()) {
79 if (pathInfo.endsWith(suffixEntry.getKey())) {
80 put(sortedMap, suffixEntry.getValue());
84 Entry<FILTER, Integer> exactMatch = exactMatchMap.get(pathInfo);
85 if (exactMatch != null) {
86 put(sortedMap, exactMatch);
88 ArrayList<FILTER> filters = new ArrayList<>(sortedMap.values());
89 logger.trace("Matching filters for path {} are {}", pathInfo, filters);
93 private void put(TreeMap<Integer, FILTER> sortedMap, Entry<FILTER, Integer> entry) {
94 sortedMap.put(entry.getValue(), entry.getKey());