1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.hc.core5.http.protocol; 29 30 import java.util.HashSet; 31 import java.util.LinkedHashMap; 32 import java.util.Map; 33 import java.util.Map.Entry; 34 import java.util.Set; 35 36 import org.apache.hc.core5.annotation.Contract; 37 import org.apache.hc.core5.annotation.ThreadingBehavior; 38 import org.apache.hc.core5.util.Args; 39 40 /** 41 * Maintains a map of objects keyed by a request URI pattern. 42 * <p> 43 * Patterns may have three formats: 44 * </p> 45 * <ul> 46 * <li>{@code *}</li> 47 * <li>{@code *<uri>}</li> 48 * <li>{@code <uri>*}</li> 49 * </ul> 50 * <p> 51 * This class can be used to resolve an object matching a particular request 52 * URI. 53 * </p> 54 * 55 * @param <T> The type of registered objects. 56 * @since 5.0 57 */ 58 @Contract(threading = ThreadingBehavior.SAFE) 59 public class UriPatternOrderedMatcher<T> implements LookupRegistry<T> { 60 61 private final Map<String, T> map; 62 63 public UriPatternOrderedMatcher() { 64 super(); 65 this.map = new LinkedHashMap<>(); 66 } 67 68 /** 69 * Returns a {@link Set} view of the mappings contained in this matcher. 70 * 71 * @return a set view of the mappings contained in this matcher. 72 * 73 * @see Map#entrySet() 74 * @since 4.4.9 75 */ 76 public synchronized Set<Entry<String, T>> entrySet() { 77 return new HashSet<>(map.entrySet()); 78 } 79 80 /** 81 * Registers the given object for URIs matching the given pattern. 82 * 83 * @param pattern the pattern to register the handler for. 84 * @param obj the object. 85 */ 86 @Override 87 public synchronized void register(final String pattern, final T obj) { 88 Args.notNull(pattern, "URI request pattern"); 89 this.map.put(pattern, obj); 90 } 91 92 /** 93 * Removes registered object, if exists, for the given pattern. 94 * 95 * @param pattern the pattern to unregister. 96 */ 97 @Override 98 public synchronized void unregister(final String pattern) { 99 if (pattern == null) { 100 return; 101 } 102 this.map.remove(pattern); 103 } 104 105 /** 106 * Looks up an object matching the given request path. 107 * 108 * @param path the request path 109 * @return object or {@code null} if no match is found. 110 */ 111 @Override 112 public synchronized T lookup(final String path) { 113 Args.notNull(path, "Request path"); 114 for (final Entry<String, T> entry : this.map.entrySet()) { 115 final String pattern = entry.getKey(); 116 if (path.equals(pattern)) { 117 return entry.getValue(); 118 } 119 if (matchUriRequestPattern(pattern, path)) { 120 return this.map.get(pattern); 121 } 122 } 123 return null; 124 } 125 126 /** 127 * Tests if the given request path matches the given pattern. 128 * 129 * @param pattern the pattern 130 * @param path the request path 131 * @return {@code true} if the request URI matches the pattern, {@code false} 132 * otherwise. 133 */ 134 protected boolean matchUriRequestPattern(final String pattern, final String path) { 135 if (pattern.equals("*")) { 136 return true; 137 } 138 return (pattern.endsWith("*") && path.startsWith(pattern.substring(0, pattern.length() - 1))) 139 || (pattern.startsWith("*") && path.endsWith(pattern.substring(1))); 140 } 141 142 @Override 143 public String toString() { 144 return this.map.toString(); 145 } 146 147 }